Fixes the free variable problem on closure conversion#299
Merged
rodrigogribeiro merged 9 commits intomainfrom Jan 14, 2026
Merged
Fixes the free variable problem on closure conversion#299rodrigogribeiro merged 9 commits intomainfrom
rodrigogribeiro merged 9 commits intomainfrom
Conversation
Collaborator
rodrigogribeiro
commented
Dec 30, 2025
- Fixes the free variable problem on closure conversion
- Adds test cases.
mbenke
requested changes
Jan 7, 2026
Collaborator
mbenke
left a comment
There was a problem hiding this comment.
Free variable calculation for blocks should be corrected.
Crashing example (tries to add z to the closure in makeClosure; see also comments in code)
function addW (l: word, r: word) -> word {
let rw : word;
assembly {
rw := add(l,r);
}
return rw;
}
contract Bug {
function main() -> word {
return makeClosure(42);
}
function makeClosure(e : word) -> word {
let f = lam (x : word) {
let y: word;
if (false) {
let z = 7;
y = z;
} else {
y = e;
}
return addW(e,x); // this works
};
return f(1);
}
}
|
|
||
| instance Vars a => Vars [a] where | ||
| vars = foldr (union . vars) [] | ||
| free = foldr (union . free) [] |
Collaborator
There was a problem hiding this comment.
This may be slightly misleading, for a statement list like [let y = 42; return y], the free will include y even though it's bound locally. It is compensated for in closureConversion but I think it merits at least a comment in the class definition.
This approach also may result in incorrect closure - see comment about If statement
| free (StmtExp e) = free e | ||
| free (Return e) = free e | ||
| free (Match e eqns) = free e `union` free eqns | ||
| free (If e blk1 blk2) = free e `union` free blk1 `union` free blk2 |
Collaborator
There was a problem hiding this comment.
For let y:word;if b then { let z = 7; y := z; } else ... we have z in free and not in bound. Then closure conversion tries to close over z.
Co-authored-by: Marcin Benke <marcin@benke.org>
Co-authored-by: Marcin Benke <marcin@benke.org>
Co-authored-by: Marcin Benke <marcin@benke.org>
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.