You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Azvanzed
changed the title
If and If-Else doesn't produce same result which creates UB.
If-Let-Else and Let-Else doesn't produce same result which creates UB.
Dec 25, 2024
traviscross
added
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
and removed
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
C-bug
Category: This is a bug.
labels
Dec 25, 2024
This isn't a bug in rustc. In your code, since you didn't separately assign the mutex guard to some binding, it's just a temporary, and those don't generally outlive the statement in which they are created (with some caveats about tail expressions). See, e.g.:
That said, we agree this is an unfortunate discrepancy between if-let and let-else, and it's something we're thinking about. If we were to change it (over an edition), it would likely be in the direction of making if-let more like let-else, i.e. continuing the direction we took in Rust 2024 to shorten the scope of temporaries.
In any event, it's probably advisable in cases like this to assign the mutex guard to a binding so as to be more explicit about the intended scope.
I tried this code:
I expected to see this happen:
A. calls the value function then the mutex unlocks. (IDA decompilation)
Instead, this happened:
B. mutex unlocks then calls the value function. (IDA decompilation)
Meta
The bug does occur in nightly.
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: