-
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
Implicit returns and missing semicolons after return
might cause a different drop order
#131313
Comments
This is documented, so at the very least changing this will be a breaking change. @rustbot label -C-bug +C-discussion |
I think, I get it. It's about this note:
But I'm not sure if this explains why I assume it's because if there is a semicolon at the end, then the final expression is implicitly |
Yes.
No. The only statement Rust has is |
I guess, this issue can be closed then, and |
There are a bunch of (rare) cases where rustfmt can change the meaning of a program. I do not think that means it should not add a semicolon, but removing the |
A fix is being proposed for stabilization in Rust 2024. Adding
|
I tried this code:
I expected to see this happen:
All these functions should do exactly the same. The parameters would be dropped in opposite initialization order.
To my understanding of Rust, implicitly and explicitly returning the last argument should do the same.
Also the semicolon after a
return
shouldn't make a difference.And binding the result of something to a variable and then returning it, should also do the same.
Instead, this happened:
f1
andf3
drop parameters in initialization order whilef2
,f4
andf5
drop parameters in opposite initialization order.It's especially weird that the semicolon after the return has any effect.
cargo fmt
will add semicolons afterreturn
.So if this is intentional, there's a bug in
cargo fmt
. Formatting should never do a semantic change.So when reproducing this bug, be sure to turn off automatic formatting if you have it enabled.
I also had some discussion with somebody on Reddit.
It seems to be necessary for lifetimes of temporary values to work correctly.
But I still think, this is confusing. So at least the
return
case should be fixed. The compiler could just implicitly add a semicolon after the return.And for implicit return values, even if it's just the implicit return value of a subscope, the
let
transformation (like inf5
) should fix the issue.And since it's possible to generally fiix this with code, I'm sure this can also be fixed in the compiler.
Meta
rustc --version --verbose
:(no backtrace available, since it doesn't crash)
The text was updated successfully, but these errors were encountered: