Fix a variable capture during optimization. #5744
Merged
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.
As part of optimizing code to get handlers in shape for the affine handler transformation, we do inlining. One of these is a somewhat special inlining, where we take handler code, which looks like this:
And we (effectively) inline the body of the let rec into the recursive reference to
handler
inside ofmatcher
. This avoids indirection and makes it easier to recognize things.However, the variable
matcher
is free in the body, and cannot easily be renamed. So, it is important thatmatcher
is not captured by the context into which it's inlined.However, the variable names aren't really this distinct. They are like
_anf5
, and such variables are also used when normalizing terms to have no complex subexpressions (etc.). If the recursivehandler
call is not direct, but via another function that gets inlined, it may be that that other function uses redundant variables, because the_anf
numbering starts at 0 for each term.So, this change uses a beefed up form of renaming when doing inlining, which also freshens the term to avoid binding certain variables. This is used to ensure that the inlining, never captures the
matcher
variable.Fixes #5737