-
Notifications
You must be signed in to change notification settings - Fork 13.7k
temporary lifetime extension for block tail expressions #146098
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
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: Travis Cross <[email protected]>
They now use the enclosing temporary scope as their scope, regardless of which `ScopeData` was used to mark it.
Co-authored-by: Travis Cross <[email protected]>
This comment has been minimized.
This comment has been minimized.
78995b7
to
2e55e56
Compare
@rustbot label -stable-nominated I'm not intending to stable-nominate this, at least. Someone else can, but I don't expect it's needed or that it would be accepted. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been minimized.
This comment has been minimized.
2e55e56
to
eafdca9
Compare
Does this only affect code in Rust 2024, or would you expect any visible difference in earlier editions? |
It should only be visible in Rust 2024. The only extending expressions that introduce temporary drop scopes are Rust 2024 block tail expressions. Suppose we have a macro Or to generalize this, the aim of this PR is that in a non-extending context, If new expressions are added to Rust that are both extending and temporary scopes, I'd want this behavior to apply to them as well. |
Since this would effectively reduce the scope of the Rust 2024 tail expression temporary scope change, we'd also want to be sure to reflect that in the behavior of the |
I haven't done extensive testing, but see this test diff for that lint: lint-tail-expr-drop-order-borrowck.rs. I'm applying the lifetime extension rules on all editions, and lifetime extension prevents the temporary scope from being registered as potentially forwards-incompatible (even though the extended scopes are technically the same as the old scopes in old editions). Though I think I've convinced myself at this point that lifetime extension doesn't need to be applied to block tails of non-extending old-edition blocks1, so potentially the lint change could be implemented in some other way instead. Footnotes
|
For convenience of implementation and testing, I've based this on #145838 with #145342's commits cherry-picked in, plus some slight tweaks and additional tests (#145838 (comment)). The real change this PR makes is the final commit.
This implements the temporary lifetime extension semantics I suggested in #145838 (comment), with the goal of making temporary lifetimes and drop order more consistent between extending and non-extending blocks. As a side-effect, this fixes #145784 with hopefully milder regressions than #145838 (in exchange for having much broader surface area).
Roughly, this subjects extending borrows (and
super let
s) in block tails to extended scopes, using the same rules aslet
statement initializers. Under this PR,now extends the lifetime of
temp()
to outlive the block tail in Rust 2024 regardless of whether the block is an extending expression in alet
statement initializer (in which context it was already extended to outlive the block before this PR). The scoping rules for tails of extending blocks remain the same: extending subexpressions' temporary scopes are extended based on the source of the lifetime extension (e.g. to match the scope of a parentlet
statement's bindings). For blocks not extended by any other source, extending borrows in the tail expression now share a temporary scope with the result of the block. This can in turn extend nested blocks within blocks' tail expressions:Since this uses the same rules as
let
, it only applies to extending sub-expressions.I'm opening this as a draft for now to have a separate place to test and discuss it. Before the implementation can be properly reviewed, I think it should have more tests, it should be optimized, it should have a Reference PR to cross-reference, and the commit history should be cleaned up.
@rustbot label +T-lang