Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid folded mutable constant value #250

Merged
merged 1 commit into from
Jan 22, 2025
Merged

Conversation

katat
Copy link
Collaborator

@katat katat commented Jan 4, 2025

When mutable constant variable is modified in a for loop, it should be marked as a non constant field.
Otherwise the MAST phase will try to constant fold that variable into wrong constant value, because we don't support unrolling in for loop.

@katat katat force-pushed the fix/mut-cst-folding-loop branch from ef81a46 to 0b91748 Compare January 4, 2025 02:28
for ii in 0..3 {
loopvar = loopvar + 1;
}
let arr = gen(loopvar);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why this code shouldn't compile

Copy link
Collaborator Author

@katat katat Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we don't support for loop unrolling, so the loopvar won't end up with the correct constant value during MAST.

With the wrong constant value for the loopvar, the generic function will be instantiated with wrong generic parameter.

So loopvar should be marked as a non constant field.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would it take to make this code work though? I feel like we should support simple constant folding like this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as long as the gen(loopvar) is not accepting the loopvar as its const generic parameter, it would work.

Copy link
Contributor

@mimoo mimoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe let's create an issue to fix that? I don't think we should prevent this kind of code :o

@katat katat merged commit 4596f2b into main Jan 22, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants