Skip to content

Commit 444eb4c

Browse files
committed
assert that non-extended temporaries have scopes
1 parent d773bd0 commit 444eb4c

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,8 @@ fn resolve_local<'tcx>(
498498
// Iterate up to the enclosing destruction scope to find the same scope that will also
499499
// be used for the result of the block itself.
500500
if let Some(inner_scope) = visitor.cx.var_parent {
501-
(visitor.cx.var_parent, _) = visitor.scope_tree.default_temporary_scope(inner_scope)
501+
visitor.cx.var_parent =
502+
Some(visitor.scope_tree.default_temporary_scope(inner_scope).0)
502503
}
503504
// Don't lifetime-extend child `super let`s or block tail expressions' temporaries in
504505
// the initializer when this `super let` is not itself extended by a parent `let`
@@ -752,10 +753,10 @@ impl<'tcx> Visitor<'tcx> for ScopeResolutionVisitor<'tcx> {
752753
// The body of the every fn is a root scope.
753754
resolve_expr(this, body.value, true);
754755
} else {
755-
// Only functions have an outer terminating (drop) scope, while
756-
// temporaries in constant initializers may be 'static, but only
757-
// according to rvalue lifetime semantics, using the same
758-
// syntactical rules used for let initializers.
756+
// All bodies have an outer temporary drop scope, but temporaries
757+
// and `super let` bindings in constant initializers may be extended
758+
// to have 'static lifetimes, using the same syntactical rules used
759+
// for `let` initializers.
759760
//
760761
// e.g., in `let x = &f();`, the temporary holding the result from
761762
// the `f()` call lives for the entirety of the surrounding block.

compiler/rustc_middle/src/middle/region.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
1616
use rustc_span::{DUMMY_SP, Span};
1717
use tracing::debug;
1818

19-
use crate::ty::TyCtxt;
19+
use crate::ty::{self, TyCtxt};
2020

2121
/// Represents a statically-describable scope that can be used to
2222
/// bound the lifetime/region for values.
@@ -302,20 +302,20 @@ impl ScopeTree {
302302

303303
/// Returns the scope of non-lifetime-extended temporaries within a given scope, as well as
304304
/// whether we've recorded a potential backwards-incompatible change to lint on.
305-
/// Returns `None` when no enclosing temporary scope is found, such as for static items.
306-
pub fn default_temporary_scope(&self, inner: Scope) -> (Option<Scope>, Option<Scope>) {
305+
/// Panics if no enclosing temporary scope is found.
306+
pub fn default_temporary_scope(&self, inner: Scope) -> (Scope, Option<Scope>) {
307307
let mut id = inner;
308308
let mut backwards_incompatible = None;
309309

310310
while let Some(&p) = self.parent_map.get(&id) {
311311
match p.data {
312312
ScopeData::Destruction => {
313313
debug!("temporary_scope({inner:?}) = {id:?} [enclosing]");
314-
return (Some(id), backwards_incompatible);
314+
return (id, backwards_incompatible);
315315
}
316316
ScopeData::IfThenRescope | ScopeData::MatchGuard => {
317317
debug!("temporary_scope({inner:?}) = {p:?} [enclosing]");
318-
return (Some(p), backwards_incompatible);
318+
return (p, backwards_incompatible);
319319
}
320320
ScopeData::Node
321321
| ScopeData::CallSite
@@ -335,7 +335,6 @@ impl ScopeTree {
335335
}
336336
}
337337

338-
debug!("temporary_scope({inner:?}) = None");
339-
(None, backwards_incompatible)
338+
span_bug!(ty::tls::with(|tcx| inner.span(tcx, self)), "no enclosing temporary scope")
340339
}
341340
}

compiler/rustc_middle/src/ty/rvalue_scopes.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ impl RvalueScopes {
3131
return (s, None);
3232
}
3333

34-
// Otherwise, locate the innermost terminating scope
35-
// if there's one. Static items, for instance, won't
36-
// have an enclosing scope, hence no scope will be
37-
// returned.
38-
region_scope_tree
39-
.default_temporary_scope(Scope { local_id: expr_id, data: ScopeData::Node })
34+
// Otherwise, locate the innermost terminating scope.
35+
let (scope, backward_incompatible) = region_scope_tree
36+
.default_temporary_scope(Scope { local_id: expr_id, data: ScopeData::Node });
37+
(Some(scope), backward_incompatible)
4038
}
4139

4240
/// Make an association between a sub-expression and an extended lifetime

0 commit comments

Comments
 (0)