Unexpectedly long lifetime of values created in let bindings #98052
Labels
A-control-flow
Area: Control flow
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Today I stumbled over an unexpected deadlock related to lifetime scoping of values inside a let binding.
It seems like values created in a let binding are kept alive until the end of the inner block, even if the value is surrounded by a block.
I'm not sure if this is the expected behaviour, but it is counter to my expectation of "values live until the end of the surrounding block".
To illustrate:
My expectation was that that the lock would go out of scope at the beginning of the inner block, but it does not and is kept alive until the inner block ends, leading to the deadlock - as can be verified by looking at the MIR:
MIR Output
In contrast, this code works as expected:
if let Some(x) = { let value = m.lock().unwrap().clone(); value } {
This seems very counter-intuitive if your assumption is that block = lifetime scope.
Maybe it's not treated as a block because it's a single expression?
rustc 1.63.0-nightly (e09449220 2022-05-31)
The text was updated successfully, but these errors were encountered: