-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Temporary lifetime extension for blocks #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: main
Are you sure you want to change the base?
Conversation
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? |
|
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
|
|
☔ The latest upstream changes (presumably #146666) made this pull request unmergeable. Please resolve the merge conflicts. |
eafdca9 to
e649bbe
Compare
|
I've made some revisions. This should now properly handle I think the implementation will likely need optimization and cleanup, but it might take a bit of refactoring to get it to a good place, so I'd like to get a vibe check on the design first, if there's room for it in a lang team meeting. @rustbot label +I-lang-nominated |
|
r? @nnethercote rustbot has assigned @nnethercote. Use |
cleanup: merge `RvalueScopes` into `ScopeTree` This gets rid of `RvalueCandidate`, inlines the definition of `RvalueScopes` into `ScopeTree`, and removes two `RvalueScopes`-specific modules, consolidating the scoping logic a bit. Removing the extra step of going from `RvalueCandidate`s to `RvalueScopes` and removing the duplication between them should also hopefully improve perf. I've also taken the liberty of doing a bit of renaming and comment updates, changing some "rvalue scope"s to "extended temporary scope"s. This is a bit closer to the Reference's terminology and makes it clearer that it's specific to temporary lifetime extension. This isn't comprehensive. In particular, I've left `record_rvalue_scope_if_borrow_expr` untouched since rust-lang/rust#146098 gets rid of it. Pulled out from rust-lang/rust#146098. r? BoxyUwU as the reviewer of rust-lang/rust#146098 (though feel free to reassign/claim! this is just cleanup) cc `@dingxiangfei2009`
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
Footnotes
|
|
The ryanpbrewster.actix-interrupt and ncomm-executors test failures both look nondeterministic. The failure in ryanpbrewster.actix-interrupt was a test that sleeps for a long time, which made it prone to timing out, and the failure in ncomm-executors was a test that measures timing. |
1df1551 to
8c84070
Compare
This comment has been minimized.
This comment has been minimized.
…llot cleanup: merge `RvalueScopes` into `ScopeTree` This gets rid of `RvalueCandidate`, inlines the definition of `RvalueScopes` into `ScopeTree`, and removes two `RvalueScopes`-specific modules, consolidating the scoping logic a bit. Removing the extra step of going from `RvalueCandidate`s to `RvalueScopes` and removing the duplication between them should also hopefully improve perf. I've also taken the liberty of doing a bit of renaming and comment updates, changing some "rvalue scope"s to "extended temporary scope"s. This is a bit closer to the Reference's terminology and makes it clearer that it's specific to temporary lifetime extension. This isn't comprehensive. In particular, I've left `record_rvalue_scope_if_borrow_expr` untouched since rust-lang#146098 gets rid of it. Pulled out from rust-lang#146098. r? BoxyUwU as the reviewer of rust-lang#146098 (though feel free to reassign/claim! this is just cleanup) cc `@dingxiangfei2009`
cleanup: merge `RvalueScopes` into `ScopeTree` This gets rid of `RvalueCandidate`, inlines the definition of `RvalueScopes` into `ScopeTree`, and removes two `RvalueScopes`-specific modules, consolidating the scoping logic a bit. Removing the extra step of going from `RvalueCandidate`s to `RvalueScopes` and removing the duplication between them should also hopefully improve perf. I've also taken the liberty of doing a bit of renaming and comment updates, changing some "rvalue scope"s to "extended temporary scope"s. This is a bit closer to the Reference's terminology and makes it clearer that it's specific to temporary lifetime extension. This isn't comprehensive. In particular, I've left `record_rvalue_scope_if_borrow_expr` untouched since rust-lang/rust#146098 gets rid of it. Pulled out from rust-lang/rust#146098. r? BoxyUwU as the reviewer of rust-lang/rust#146098 (though feel free to reassign/claim! this is just cleanup) cc `@dingxiangfei2009`
cleanup: merge `RvalueScopes` into `ScopeTree` This gets rid of `RvalueCandidate`, inlines the definition of `RvalueScopes` into `ScopeTree`, and removes two `RvalueScopes`-specific modules, consolidating the scoping logic a bit. Removing the extra step of going from `RvalueCandidate`s to `RvalueScopes` and removing the duplication between them should also hopefully improve perf. I've also taken the liberty of doing a bit of renaming and comment updates, changing some "rvalue scope"s to "extended temporary scope"s. This is a bit closer to the Reference's terminology and makes it clearer that it's specific to temporary lifetime extension. This isn't comprehensive. In particular, I've left `record_rvalue_scope_if_borrow_expr` untouched since rust-lang/rust#146098 gets rid of it. Pulled out from rust-lang/rust#146098. r? BoxyUwU as the reviewer of rust-lang/rust#146098 (though feel free to reassign/claim! this is just cleanup) cc `@dingxiangfei2009`
| if node_info.drop_temps { | ||
| // If this scope corresponds to an extending subexpression, we can extend certain | ||
| // temporaries' scopes through it. | ||
| if node_info.extending && self.cx.extended_parent.is_none() { | ||
| self.cx.extended_parent = Some(ExtendedScope::ThroughExpression( | ||
| self.cx.parent.expect("extending subexpressions should have parent scopes"), | ||
| )); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected that whenever we enter an extending node we update the extended_parent. Why do we only set an extended parent if the scope drops temps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to avoid HashMap insertions by only setting the extended_parent when it will give extendible temporaries non-default temporary scopes. The result should be the same in either case, but when the extended_parent is set we record borrow expressions' operands' temporary scopes in a table, which is probably best to avoid. I'll add a comment with a better explanation.
As for why it's the same, consider the context of a borrow expression in the HIR; we need to check the extended_parent to determine its operand's temporary scope. It'll have some (possibly empty) chain of extending: true nodes directly above it.
- If the chain leads all the way to a
letstatement, const, or static, theextended_parentwill have been set already byresolve_local; the borrow operand will have an extended temporary scope. - Otherwise, if there's no
drop_temps: truenode in theextending: truechain, the borrow operand's temporary scope will be its default temporary scope. Setting theextended_parentto the node above the chain won't change this, since there's nodrop_temps: truenodes in it that would affect the default temporary scope. As such, we can leave it unset. - If there is a
drop_temps: truenode in theextending: truechain, we should make sure the borrow operand lives past it; that's the change this PR makes. To do that, we use the default temporary scope at whatever expression started theextending: truechain. e.g. inf({ &temp() }), the outermost node in the extending chain is the block tail subexpression, so we use the default temporary scope at the block expression for the borrow operand. The trick here is the same as in the previous case: the only thing that will change the default temporary scope isdrop_temps: truenodes, so we can put off actually setting theextended_parentuntil we hit one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option here would be to get rid of the ScopeTree altogether (#97426). It's effectively a second copy of each HIR body but stored in an IndexMap. We could probably save a lot of work (and reallocations) by not building it. That'd be a bigger refactor, but it's one I've been thinking about anyway. The way I imagine it, without the ScopeTree, we also wouldn't have a special table for lifetime-extended temporaries, so we wouldn't need to worry about minimizing insertions into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the comment to add some more detail: (diff)
| /// ``` | ||
| /// | ||
| /// Note: ET is intended to match "rvalues or places based on rvalues". | ||
| fn record_subexpr_extended_temp_scopes( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I struggled a bit to understand this function due to it using a loop instead of being a recursive fn. Do you know if there's a reason why it's not like that? If it can be a recursive fn want to open a separate PR changing that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not actually sure why it's a loop. I'll look into it. I have a suspicion it used to be recursive; it was previously named record_rvalue_scope_rec and it was called by a function named record_rvalue_scope, so there's probably some justification in a commit message or PR description somewhere. I'd also find recursion more readable, so I'll see if that justification still applies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| /// Determines the scopes of subexpressions' temporaries. | ||
| #[derive(Debug, Copy, Clone)] | ||
| struct NodeInfo { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How much of this PR is actually user-facing. Is it theoretically possible to split out like 99% of this PR but with blocks as having extending: false and then this PR is just flipping a bool?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual change this PR makes is the addition of this conditional assignment. e037e93 is a self-contained refactor that helps make it expressible, and 8c84070 is the less self-contained stuff, including the actual change. I could split things apart further if that would help.
As for blocks, they should already be extending: true in stable. The change this PR makes is in what extending: true means:
- Currently (and in that first commit), if you have an unbroken chain of
extending: truesubexpressions starting at the initializer of aletstatement1 and going to an expression with a lifetime-extendible temporary2, that expression's lifetime-extendible temporaries are lifetime-extended to match scope of theletstatement's bindings. This means thetemp()in{ &temp() }only outlives the block tail if the block appears in a lifetime-extending context. - After this PR's change, I have a different mental model: the lifetime-extendible temporaries of an
extending: truesubexpression are treated as lifetime-extendible temporaries of the parent expression (and if they become lifetime-extendible temporaries of aletstatement initializer, they're lifetime-extended to match the scope of its bindings). This means thetemp()in{ &temp() }always outlives the block tail, but how far it lives past that depends on the context the block is in.
Footnotes
-
The body expressions of static and constant items and the tail expressions of const blocks are treated like
letstatement initializers for this purpose. ↩ -
Lifetime-extendible temporaries are produced by borrow operators (which lifetime-extend their operands if they become temporaries) and blocks with
super letstatements (which lifetime-extend theirsuper lets). ↩
8c84070 to
d1395be
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
d1395be to
b94561a
Compare
…ewrite, r=BoxyUwU `region_scope_tree`: Rewrite a `loop` as tail recursion This addresses rust-lang#146098 (comment), hopefully making `record_subexpr_extended_temp_scopes` a bit more legible. I removed a `debug!` in the process, since the function it calls every iteration, [`ScopeTree::record_extended_temp_scope`](https://github.com/rust-lang/rust/blob/2848c2ebe9a8a604cd63455263299d7258bc8252/compiler/rustc_middle/src/middle/region.rs#L264-L271), does the same `debug!`. Keeping the behavior of having an additional `debug!` for the top-level expression would hurt legibility, and having a duplicate `debug!` at each iteration seems excessive. No information is lost from the debug output, so I think it's fine to just have the inner `debug!`s. r? BoxyUwU or anyone
cleanup: merge `RvalueScopes` into `ScopeTree` This gets rid of `RvalueCandidate`, inlines the definition of `RvalueScopes` into `ScopeTree`, and removes two `RvalueScopes`-specific modules, consolidating the scoping logic a bit. Removing the extra step of going from `RvalueCandidate`s to `RvalueScopes` and removing the duplication between them should also hopefully improve perf. I've also taken the liberty of doing a bit of renaming and comment updates, changing some "rvalue scope"s to "extended temporary scope"s. This is a bit closer to the Reference's terminology and makes it clearer that it's specific to temporary lifetime extension. This isn't comprehensive. In particular, I've left `record_rvalue_scope_if_borrow_expr` untouched since rust-lang/rust#146098 gets rid of it. Pulled out from rust-lang/rust#146098. r? BoxyUwU as the reviewer of rust-lang/rust#146098 (though feel free to reassign/claim! this is just cleanup) cc `@dingxiangfei2009`
Rollup merge of #150548 - dianne:temp-scope-extension-loop-rewrite, r=BoxyUwU `region_scope_tree`: Rewrite a `loop` as tail recursion This addresses #146098 (comment), hopefully making `record_subexpr_extended_temp_scopes` a bit more legible. I removed a `debug!` in the process, since the function it calls every iteration, [`ScopeTree::record_extended_temp_scope`](https://github.com/rust-lang/rust/blob/2848c2ebe9a8a604cd63455263299d7258bc8252/compiler/rustc_middle/src/middle/region.rs#L264-L271), does the same `debug!`. Keeping the behavior of having an additional `debug!` for the top-level expression would hurt legibility, and having a duplicate `debug!` at each iteration seems excessive. No information is lost from the debug output, so I think it's fine to just have the inner `debug!`s. r? BoxyUwU or anyone
|
☔ The latest upstream changes (presumably #150559) made this pull request unmergeable. Please resolve the merge conflicts. |
b94561a to
d79798b
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This implements a revised version of 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 consequence, this undoes the breaking change introduced by #145838 (but in exchange has a much larger surface area).
The change this PR hopes to enforce is a general rule: any expression's temporaries should have the same relative drop order regardless of whether the expression is in an extending context or not:
let _ = $expr;anddrop($expr);should have the same drop order. To achieve that, this PR applies lifetime extension rules bottom-up, starting from borrow expressions andsuper let, rather than top-down fromletstatements andconsts/statics. For example:now extends the lifetime of
temp()to outlive the block tail in Rust 2024 regardless of whether the block is an extending expression in aletstatement 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 parentletstatement'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.This also applies to
ifexpressions' blocks and to match arms in all editions, since lifetime extension applies to both of them as well and they both drop their non-extended temporaries. This is where breakage from #145838 was observed:now extends
temp()to have the same temporary scope as the result of theifexpression.As a further consequence, this makes
super letinifexpressions' blocks more consistent with block expressions:previously only worked in extending contexts (since the
super lets would be extended), and now it works everywhere.Reference PR: rust-lang/reference#2051
Edition Guide PR: rust-lang/edition-guide#379
@rustbot label +T-lang