-
Notifications
You must be signed in to change notification settings - Fork 13.4k
let _ = foo()
runs the destructor at the wrong time
#16526
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
This seems to originate from febd7ee. |
@nikomatsakis in #10488 you argued for the current behavior. Have semantics change since then so that the other behavior is preferable? Esp. regarding #10488 (comment) where you say that there's no equivalent and that it is an important capability. |
@dotdash I am saying that the current behavior does not reflect what I was arguing for and what I believe we agreed to. |
If you treat let _tmp = foo();
let _ = _tmp; doesn't that mean that the value is actually bound? How does that fit with the "Don't bind it" semantics described in #10488? In fact code like that was given as an example for the "Bind it" semantics. What am I missing? |
@dotdash at the time I wrote "this will depend on the precise temporary rules". What i'm saying is that the way we wound up defining the temporary rules, as I understand it, is supposed to mean that
|
Actually, re-reading the code, I think I may be misremembering, and the current behavior is a logical outcome of the rules. That is, the lifetime of the temporary that is introduced is confined to the statement itself, because the pattern does not clearly take a ref. |
internal: Cleanup 🧹
I believe that the semantics of
let _ = foo()
should be that the destructor for the value returned byfoo()
runs at the end of the block. The reason for this is a consequence of a couple of other rules that I worked out as part of the temporary lifetime stuff.In general,
let <pat> = <rvalue>
where<pat>
is not a single identifier is equivalent to:In this case, that means that
let _ = <rvalue>
should be:and since the
_
pattern just ignores the thing it matches against, this means that_tmp
is unaffected and drops at the end of the block.However, the following test case suggests that this is not what we do today:
This should print
Hi1
and thenHi2
but in fact it prints the opposite. This is probably a hold-over from the special code that was in trans to special case the treatment oflet _
vs other patterns, but I could be wrong.cc @pcwalton @pnkfelix
The text was updated successfully, but these errors were encountered: