fix(compiler): Correct mutation bug in dead branch optimization - #2400
Open
spotandjake wants to merge 1 commit into
Open
fix(compiler): Correct mutation bug in dead branch optimization#2400spotandjake wants to merge 1 commit into
spotandjake wants to merge 1 commit into
Conversation
This pr corrects a small bug related to our dead branch optimization. Programs that contained a `let` bind inside of constant `if` statement, inside of another `let mut` bind would lose the mut_flag of the outer binding. This pr corrects that by properly propagating the mut_flag. Issue: grain-lang#2399
spotandjake
requested review from
alex-snezhko,
marcusroberts,
ospencer,
peblair and
phated
as code owners
August 16, 2026 02:12
Kara-Zor-El
suggested changes
Aug 16, 2026
Comment on lines
53
to
62
| @@ -59,7 +58,7 @@ module BranchArg: Anf_mapper.MapArgument = { | |||
| recursive, | |||
| mutable_, | |||
| binds, | |||
| relinearize(id, global, body, cont), | |||
| relinearize(id, global, body, mut_flag, cont), | |||
| ), | |||
Contributor
There was a problem hiding this comment.
Tested and it seems to fix the issue reported. However, turns out that there's another where:
let mut x = if (true) {
let mut y = 1
y
} else {
2
}
let z = () => x
x = 3
print(z())
produces 1 instead of 3.
This should fix be:
- | AELet(global, recursive, mutable_, binds, body) => {
+ | AELet(inner_global, recursive, mutable_, binds, body) => {
...a,
anf_desc:
AELet(
- global,
+ inner_global,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pr corrects a small bug related to our dead branch optimization.
Programs that contained a
letbind inside of constantifstatement, inside of anotherlet mutbind would lose the mut_flag of the outer binding.This pr corrects that by properly propagating the mut_flag.
Issue: #2399
Thanks to @Kara-Zor-El for the help.