Drop temporaries created in a condition, even if it's a let chain#102998
Merged
Conversation
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.
Fixes #100513.
During the lowering from AST to HIR we wrap expressions acting as conditions in a
DropTempsexpression so that any temporaries created in the condition are dropped after the condition is executed. Effectively this means we transforminto (roughly)
so that if we create any temporaries, they're lifted into the new scope surrounding the condition, so for example something along the lines of
Before this PR, if the condition contained any let expressions we would not introduce that new scope, instead leaving the condition alone. This meant that in a let-chain like
the temporary created for
get_drop("first")would be lifted into the surrounding block, which caused it to be dropped after the execution of the entireifexpression.After this PR, we wrap everything but the
letexpression in terminating scopes. The upside to this solution is that it's minimally invasive, but the downside is that in the worst case, an expression withletexprs interspersed likegets multiple new scopes, roughly
so instead of all of the temporaries being dropped at the end of the entire condition, they will be dropped right after they're evaluated (before the subsequent
letexpr). So while I'd say the drop behavior around let-chains is less surprising after this PR, it still might not exactly match what people might expect.For tests, I've just extended the drop order tests added in #100526. I'm not sure if that's the best way to go about it, though, so suggestions are welcome.