-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
if let should pre-drop wrt else #103108
Comments
This is probably a leftover from the time when Dropping them early would be more consistent with if (it cannot be quite consistent as if let needs to keep them alive during the if block) but less consistent with match. Since if let is closer to if than match, I agree that it should behave more like it. But I don't think we can change this. It could break code in extremely subtle undetectable cases (think of some kind of lock being held in the condition which is then required in the else branch). This also makes it hardly editionable, from a migration and also from a future rustc maintenance perspective. |
Good point about the consistency. There are analogous drop order optimization for matches too: You can drop the scrutinee temporaries before entering the body of any arm that is binding free and only followed by other binding-free arms, so cases like: match foo {
Enum::ValA(a) => {},
Enum::ValB => {},
_ => {},
} for example, you would drop the temporaries before entering the bodies of the But even without that extension, I'd be okay with the inconsistency with You are right, code might depend on the current behaviour and it might cause slight bugs. OTOH, one could also argue that if you rely on that specific quirk of drop order, then maybe your program was buggy from the start. |
…r=jieyouxu Rescope temp lifetime in if-let into IfElse with migration lint Tracking issue rust-lang#124085 This PR shortens the temporary lifetime to cover only the pattern matching and consequent branch of a `if let`. At the expression location, means that the lifetime is shortened from previously the deepest enclosing block or statement in Edition 2021. This warrants an Edition change. Coming with the Edition change, this patch also implements an edition lint to warn about the change and a safe rewrite suggestion to preserve the 2021 semantics in most cases. Related to rust-lang#103108. Related crater runs: rust-lang#129466.
…r=jieyouxu Rescope temp lifetime in if-let into IfElse with migration lint Tracking issue rust-lang#124085 This PR shortens the temporary lifetime to cover only the pattern matching and consequent branch of a `if let`. At the expression location, means that the lifetime is shortened from previously the deepest enclosing block or statement in Edition 2021. This warrants an Edition change. Coming with the Edition change, this patch also implements an edition lint to warn about the change and a safe rewrite suggestion to preserve the 2021 semantics in most cases. Related to rust-lang#103108. Related crater runs: rust-lang#129466.
Consider this snippet:
Here,
if let
complains about an access offoo
in the else branch, whileif
is okay with this.This is due to
if let
dropping its temporaries after theelse
block. This is not neccessary, it could also drop them before.Similar to #103107
cc @Nilstrieb
@rustbot label T-lang
The text was updated successfully, but these errors were encountered: