-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Scrutinee dropped after if-let body #133677
Comments
This is working as intended. For related discussion, see https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-if-let-scope.html and #131154 |
No. Allow me to elucidate. Temporaries in the scrutinee are not the same as temporaries inside a block expression in the scrutinee. Take a look at: fn main() {
struct S(i32);
impl Drop for S {
fn drop(&mut self) {
println!("Dropped S {}", self.0)
}
}
println!("--- test 4");
{
println!("Before if-let");
if let _ = { S(4).0 } {
println!("Inside body");
}
}
println!("--- test 5");
{
println!("Before if-let");
if let _ = { let x = S(5).0; x } {
println!("Inside body");
}
}
} This prints
, it should print
|
@rustbot labels +needs-triage |
Everything works they way you expect in the 2024 edition: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=c7245eb79cc6379d3cc19875401925ad So this is indeed fixed by https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-if-let-scope.html |
It does work, but it's not supposed to according to this description:
Maybe block expressions in the scrutinee were fixed by accident in the 2024 edition? Also, it isn't fixed by that, it just so happens to be fixed for some reason. Again, temporaries in the scrutinee are unrelated to temporaries in block expressions in the scrutinee. |
I think both https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-if-let-scope.html and https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-tail-expr-scope.html individually fixed this. Before the latter change all temporaries of the tail expression of a block had their lifetime extended to match the context within which the block appeared, so before https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-tail-expr-scope.html |
Objects inside block expressions in the scrutinee of an if-let statement are not dropped before the body.
This is inconsistent both with declaring a temporary variable, e.g.
x
in test #2, and let-else behavior in test #3.I tried this code:
I expected to see this happen:
Instead, this happened:
Meta
Same behavior on both stable and nightly.
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: