Skip to content

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Oct 8, 2025

This PR clarifies a point of confusion in the compiler: all bodies have an outer temporary drop scope, including static and const item bodies1. Whenever a temporary should be dropped in its enclosing temporary scope, it should have a temporary scope to be dropped in so that its drop can be scheduled2. As such, I've updated some relevant comments and made ScopeTree::default_temporary_scope and RvalueScopes::temporary_scope panic when an enclosing temporary scope isn't found instead of allowing potential bugs where potentially-drop-sensitive temporaries are effectively given static lifetimes.

Since non-extended super let bindings are dropped in their block's enclosing temporary scope, this applies to them as well: the enclosing temporary scope should exist.

Footnotes

  1. See https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir_analysis/src/check/region.rs#L773-L778 for non-fn/closure bodies. The this.cx.var_parent = None; enables lifetime extension to 'static lifetimes and the ScopeData::Destruction scope ensures non-extended temporaries are dropped in the body expression's scope.

  2. For certain borrowed temporaries, drops that don't require running destructors may later be removed by constant promotion. That is unrelated to this PR.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 8, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 8, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Comment on lines 765 to 768
// Similarly, `const X: ... = &f();` would have the result of `f()`
// live for `'static`, implying (if Drop restrictions on constants
// ever get lifted) that the value *could* have a destructor, but
// it'd get leaked instead of the destructor running during the
// evaluation of `X` (if at all allowed by CTFE).
Copy link
Contributor Author

@dianne dianne Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't tell if the remarks on on Drop restrictions are out of date or just worded a bit confusingly. I'm also not sure whether this PR is the right place to update that.

@dianne dianne force-pushed the assert-temporary-scope branch from af03a85 to 444eb4c Compare October 8, 2025 13:14
@theemathas
Copy link
Contributor

The only way a temporary should be extended to have a 'static lifetime is through lifetime extension

I'm not sure what this means. But I just wanted to note that constant-promotion in consts/statics can have a similar effect to lifetime extension, and has different behavior from constant-promotion outside of consts/statics. See #124328

@dianne
Copy link
Contributor Author

dianne commented Oct 8, 2025

I've clarified the PR description; thanks. Does it read better now? This is only about the temporary scopes assigned to expressions before PromoteTemps runs and is of most concern for non-promotable expressions (e.g. those that produce drop-sensitive temporaries).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants