-
Notifications
You must be signed in to change notification settings - Fork 13.5k
add a scope for if let
guard temporaries and bindings
#143376
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
base: master
Are you sure you want to change the base?
Conversation
|
Some changes occurred in match lowering cc @Nadrieril |
All sounds reasonable to me. Let's see what perf thinks. @bors2 try |
This comment has been minimized.
This comment has been minimized.
add a scope for `if let` guard temporaries and bindings This fixes my concern with `if let` guard drop order, namely that the guard's bindings and temporaries were being dropped after their arm's pattern's bindings, instead of before (#141295 (comment)). The guard's bindings and temporaries now live in a new scope, which extends until (but not past) the end of the arm, guaranteeing they're dropped before the arm's pattern's bindings. So far, this is the only way I've thought of to achieve this without explicitly rescheduling guards' drops to move them after the arm's pattern's. I'm not sure this should be merged as-is. It's a little hacky and it introduces a new scope for *all* match arms rather than just those with `if let` guards. However, since I'm looking for feedback on the approach, I figured this is a relatively simple way to present it. As I mention in a FIXME comment, something like this will be needed for guard patterns (#129967) too[^1], so I think the final version should maybe only add these scopes as needed. That'll be better for perf too. Tracking issue for `if_let_guard`: #51114 Tests are adapted from examples by `@traviscross,` `@est31,` and myself on #141295. cc, as I'd like your input on this. I'm not entirely sure who to request for scoping changes, but let's start with r? `@Nadrieril` since this relates to guard patterns, we talked about it recently, and rustbot's going to ping you anyway. Feel free to reassign! [^1]: e.g., new scopes are needed to keep failed guards inside `let` chain patterns from dropping existing bindings/temporaries; something like this could give a way of doing that without needing to reschedule drops. Unfortunately it may not help keep failed guards in `let` statement patterns from dropping the `let` statement's initializer, so it isn't a complete solution. I'm still not sure how to do that without rescheduling drops, changing how `let` statements' scopes work, or restricting the functionality of guard patterns in `let` statements (including `let`-`else`).
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (43cc1e1): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 2.0%, secondary 2.8%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -3.1%, secondary -0.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary -1.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 461.296s -> 461.485s (0.04%) |
Looks like noise. If there was a change, it would be a regression in |
I approve of the idea! Can't speak of the impl, I'm not familiar with MIR scopes. r? compiler |
tests/ui/drop/if-let-guards.rs
Outdated
@@ -0,0 +1,87 @@ | |||
//! Tests drop order for `if let` guard bindings and temporaries. This is for behavior specific to | |||
//! `match` expressions, whereas `tests/ui/drop/drop-order-comparisons.rs` compares `let` chains in | |||
//! guards to `let` chains in `if` expressions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a comment that we don't expect any edition differences here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added: diff. I noticed a comment I wrote about the lifetime of a match
scrutinee could be misleading, so I've amended that as well. It's still vague, but I think where exactly the scrutinee is dropped in that example is technically Edition-dependent (just not in a way that affects these tests).
This ensures `if let` guard temporaries and bindings are dropped before the match arm's pattern's bindings.
This fixes my concern with
if let
guard drop order, namely that the guard's bindings and temporaries were being dropped after their arm's pattern's bindings, instead of before (#141295 (comment)). The guard's bindings and temporaries now live in a new scope, which extends until (but not past) the end of the arm, guaranteeing they're dropped before the arm's pattern's bindings. So far, this is the only way I've thought of to achieve this without explicitly rescheduling guards' drops to move them after the arm's pattern's.I'm not sure this should be merged as-is. It's a little hacky and it introduces a new scope for all match arms rather than just those with
if let
guards. However, since I'm looking for feedback on the approach, I figured this is a relatively simple way to present it. As I mention in a FIXME comment, something like this will be needed for guard patterns (#129967) too1, so I think the final version should maybe only add these scopes as needed. That'll be better for perf too.Tracking issue for
if_let_guard
: #51114Tests are adapted from examples by @traviscross, @est31, and myself on #141295. cc, as I'd like your input on this.
I'm not entirely sure who to request for scoping changes, but let's start with r? @Nadrieril since this relates to guard patterns, we talked about it recently, and rustbot's going to ping you anyway. Feel free to reassign!
Footnotes
e.g., new scopes are needed to keep failed guards inside
let
chain patterns from dropping existing bindings/temporaries; something like this could give a way of doing that without needing to reschedule drops. Unfortunately it may not help keep failed guards inlet
statement patterns from dropping thelet
statement's initializer, so it isn't a complete solution. I'm still not sure how to do that without rescheduling drops, changing howlet
statements' scopes work, or restricting the functionality of guard patterns inlet
statements (includinglet
-else
). ↩