rust/compiler/rustc_middle/src
Matthias Krüger 2a10082bcc
Rollup merge of #147471 - dianne:assert-temporary-scope, r=nnethercote
Assert that non-extended temporaries and `super let` bindings have scopes

This PR clarifies a point of confusion in the compiler: all bodies have an outer temporary drop scope, including `static` and `const` item bodies[^1]. 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 scheduled[^2]. 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.

[^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](https://doc.rust-lang.org/stable/reference/destructors.html#r-destructors.scope.lifetime-extension.static) 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.
2025-10-13 16:54:12 +02:00
..
dep_graph Reuse metadata file from work products. 2025-07-04 14:02:17 +00:00
hir Rollup merge of #146694 - camsteffen:impl-subject, r=compiler-errors 2025-09-18 11:48:52 +10:00
hooks Make def_path_hash_to_def_id not panic when passed an invalid hash 2025-09-26 18:36:15 +02:00
infer inline CanonicalTyVarKind 2025-09-08 14:17:56 +02:00
middle assert that non-extended temporaries have scopes 2025-10-08 06:12:09 -07:00
mir Auto merge of #146096 - adwinwhite:handle_normalization_overflow_in_mono1, r=saethlin 2025-10-13 00:20:10 +00:00
query Auto merge of #146096 - adwinwhite:handle_normalization_overflow_in_mono1, r=saethlin 2025-10-13 00:20:10 +00:00
thir use ty::Value instead of manual pairs of types and valtrees 2025-08-14 09:44:22 +02:00
traits allow method calls on opaques 2025-09-26 16:33:15 +02:00
ty Rollup merge of #147471 - dianne:assert-temporary-scope, r=nnethercote 2025-10-13 16:54:12 +02:00
util Remove attribute #[rustc_error] 2025-03-30 01:32:21 +03:00
arena.rs more rename 2025-09-29 16:06:25 +01:00
error.rs Rollup merge of #144197 - KMJ-007:type-tree, r=ZuseZ4 2025-09-28 18:13:11 +02:00
lib.rs Apply cfg(bootstrap) replacement 2025-09-26 19:09:23 -04:00
lint.rs mention lint group in default level lint note 2025-08-19 21:27:10 +02:00
macros.rs Fold visit into ty 2025-03-15 06:34:36 +00:00
metadata.rs Re-export more rustc_span::symbol things from rustc_span. 2024-12-18 13:38:53 +11:00
tests.rs Give an item related to issue 27438 a more meaningful name 2024-04-30 22:27:19 +02:00
thir.rs Introduce VarBindingIntroduction. 2025-10-11 20:50:20 +00:00
values.rs prefer to use repeat_n over repeat and take 2025-10-09 01:24:55 +08:00