1 //! This file declares the `ScopeTree` type, which describes
2 //! the parent links in the region hierarchy.
4 //! For more information about how MIR-based region-checking works,
5 //! see the [rustc guide].
7 //! [rustc guide]: https://rust-lang.github.io/rustc-guide/mir/borrowck.html
9 use crate::ich::{NodeIdHashingMode, StableHashingContext};
10 use crate::ty::{self, DefIdTree, TyCtxt};
12 use rustc_hir::def_id::DefId;
15 use rustc_data_structures::fx::FxHashMap;
16 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
17 use rustc_macros::HashStable;
18 use rustc_span::{Span, DUMMY_SP};
22 /// Represents a statically-describable scope that can be used to
23 /// bound the lifetime/region for values.
25 /// `Node(node_id)`: Any AST node that has any scope at all has the
26 /// `Node(node_id)` scope. Other variants represent special cases not
27 /// immediately derivable from the abstract syntax tree structure.
29 /// `DestructionScope(node_id)` represents the scope of destructors
30 /// implicitly-attached to `node_id` that run immediately after the
31 /// expression for `node_id` itself. Not every AST node carries a
32 /// `DestructionScope`, but those that are `terminating_scopes` do;
33 /// see discussion with `ScopeTree`.
35 /// `Remainder { block, statement_index }` represents
36 /// the scope of user code running immediately after the initializer
37 /// expression for the indexed statement, until the end of the block.
39 /// So: the following code can be broken down into the scopes beneath:
42 /// let a = f().g( 'b: { let x = d(); let y = d(); x.h(y) } ) ;
46 /// +---------+ (R10.)
48 /// +----------+ (M8.)
49 /// +----------------------+ (R7.)
51 /// +----------+ (M5.)
52 /// +-----------------------------------+ (M4.)
53 /// +--------------------------------------------------+ (M3.)
55 /// +-----------------------------------------------------------+ (M1.)
57 /// (M1.): Node scope of the whole `let a = ...;` statement.
58 /// (M2.): Node scope of the `f()` expression.
59 /// (M3.): Node scope of the `f().g(..)` expression.
60 /// (M4.): Node scope of the block labeled `'b:`.
61 /// (M5.): Node scope of the `let x = d();` statement
62 /// (D6.): DestructionScope for temporaries created during M5.
63 /// (R7.): Remainder scope for block `'b:`, stmt 0 (let x = ...).
64 /// (M8.): Node scope of the `let y = d();` statement.
65 /// (D9.): DestructionScope for temporaries created during M8.
66 /// (R10.): Remainder scope for block `'b:`, stmt 1 (let y = ...).
67 /// (D11.): DestructionScope for temporaries and bindings from block `'b:`.
68 /// (D12.): DestructionScope for temporaries created during M1 (e.g., f()).
71 /// Note that while the above picture shows the destruction scopes
72 /// as following their corresponding node scopes, in the internal
73 /// data structures of the compiler the destruction scopes are
74 /// represented as enclosing parents. This is sound because we use the
75 /// enclosing parent relationship just to ensure that referenced
76 /// values live long enough; phrased another way, the starting point
77 /// of each range is not really the important thing in the above
78 /// picture, but rather the ending point.
80 // FIXME(pnkfelix): this currently derives `PartialOrd` and `Ord` to
81 // placate the same deriving in `ty::FreeRegion`, but we may want to
82 // actually attach a more meaningful ordering to scopes than the one
83 // generated via deriving here.
97 pub id: hir::ItemLocalId,
101 impl fmt::Debug for Scope {
102 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
104 ScopeData::Node => write!(fmt, "Node({:?})", self.id),
105 ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.id),
106 ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.id),
107 ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.id),
108 ScopeData::Remainder(fsi) => write!(
110 "Remainder {{ block: {:?}, first_statement_index: {}}}",
134 /// Scope of the call-site for a function or closure
135 /// (outlives the arguments as well as the body).
138 /// Scope of arguments passed to a function or closure
139 /// (they outlive its body).
142 /// Scope of destructors for temporaries of node-id.
145 /// Scope following a `let id = expr;` binding in a block.
146 Remainder(FirstStatementIndex),
149 rustc_index::newtype_index! {
150 /// Represents a subscope of `block` for a binding that is introduced
151 /// by `block.stmts[first_statement_index]`. Such subscopes represent
152 /// a suffix of the block. Note that each subscope does not include
153 /// the initializer expression, if any, for the statement indexed by
154 /// `first_statement_index`.
156 /// For example, given `{ let (a, b) = EXPR_1; let c = EXPR_2; ... }`:
158 /// * The subscope with `first_statement_index == 0` is scope of both
159 /// `a` and `b`; it does not include EXPR_1, but does include
160 /// everything after that first `let`. (If you want a scope that
161 /// includes EXPR_1 as well, then do not use `Scope::Remainder`,
162 /// but instead another `Scope` that encompasses the whole block,
163 /// e.g., `Scope::Node`.
165 /// * The subscope with `first_statement_index == 1` is scope of `c`,
166 /// and thus does not include EXPR_2, but covers the `...`.
167 pub struct FirstStatementIndex {
172 // compilation error if size of `ScopeData` is not the same as a `u32`
173 static_assert_size!(ScopeData, 4);
176 /// Returns a item-local ID associated with this scope.
178 /// N.B., likely to be replaced as API is refined; e.g., pnkfelix
179 /// anticipates `fn entry_node_id` and `fn each_exit_node_id`.
180 pub fn item_local_id(&self) -> hir::ItemLocalId {
184 pub fn hir_id(&self, scope_tree: &ScopeTree) -> hir::HirId {
185 match scope_tree.root_body {
186 Some(hir_id) => hir::HirId { owner: hir_id.owner, local_id: self.item_local_id() },
187 None => hir::DUMMY_HIR_ID,
191 /// Returns the span of this `Scope`. Note that in general the
192 /// returned span may not correspond to the span of any `NodeId` in
194 pub fn span(&self, tcx: TyCtxt<'_>, scope_tree: &ScopeTree) -> Span {
195 let hir_id = self.hir_id(scope_tree);
196 if hir_id == hir::DUMMY_HIR_ID {
199 let span = tcx.hir().span(hir_id);
200 if let ScopeData::Remainder(first_statement_index) = self.data {
201 if let Node::Block(ref blk) = tcx.hir().get(hir_id) {
202 // Want span for scope starting after the
203 // indexed statement and ending at end of
204 // `blk`; reuse span of `blk` and shift `lo`
205 // forward to end of indexed statement.
207 // (This is the special case aluded to in the
208 // doc-comment for this method)
210 let stmt_span = blk.stmts[first_statement_index.index()].span;
212 // To avoid issues with macro-generated spans, the span
213 // of the statement must be nested in that of the block.
214 if span.lo() <= stmt_span.lo() && stmt_span.lo() <= span.hi() {
215 return Span::new(stmt_span.lo(), span.hi(), span.ctxt());
223 pub type ScopeDepth = u32;
225 /// The region scope tree encodes information about region relationships.
226 #[derive(Default, Debug)]
227 pub struct ScopeTree {
228 /// If not empty, this body is the root of this region hierarchy.
229 pub root_body: Option<hir::HirId>,
231 /// The parent of the root body owner, if the latter is an
232 /// an associated const or method, as impls/traits can also
233 /// have lifetime parameters free in this body.
234 pub root_parent: Option<hir::HirId>,
236 /// Maps from a scope ID to the enclosing scope id;
237 /// this is usually corresponding to the lexical nesting, though
238 /// in the case of closures the parent scope is the innermost
239 /// conditional expression or repeating block. (Note that the
240 /// enclosing scope ID for the block associated with a closure is
241 /// the closure itself.)
242 pub parent_map: FxHashMap<Scope, (Scope, ScopeDepth)>,
244 /// Maps from a variable or binding ID to the block in which that
245 /// variable is declared.
246 var_map: FxHashMap<hir::ItemLocalId, Scope>,
248 /// Maps from a `NodeId` to the associated destruction scope (if any).
249 destruction_scopes: FxHashMap<hir::ItemLocalId, Scope>,
251 /// `rvalue_scopes` includes entries for those expressions whose
252 /// cleanup scope is larger than the default. The map goes from the
253 /// expression ID to the cleanup scope id. For rvalues not present in
254 /// this table, the appropriate cleanup scope is the innermost
255 /// enclosing statement, conditional expression, or repeating
256 /// block (see `terminating_scopes`).
257 /// In constants, None is used to indicate that certain expressions
258 /// escape into 'static and should have no local cleanup scope.
259 rvalue_scopes: FxHashMap<hir::ItemLocalId, Option<Scope>>,
261 /// Encodes the hierarchy of fn bodies. Every fn body (including
262 /// closures) forms its own distinct region hierarchy, rooted in
263 /// the block that is the fn body. This map points from the ID of
264 /// that root block to the ID of the root block for the enclosing
265 /// fn, if any. Thus the map structures the fn bodies into a
266 /// hierarchy based on their lexical mapping. This is used to
267 /// handle the relationships between regions in a fn and in a
268 /// closure defined by that fn. See the "Modeling closures"
269 /// section of the README in infer::region_constraints for
271 closure_tree: FxHashMap<hir::ItemLocalId, hir::ItemLocalId>,
273 /// If there are any `yield` nested within a scope, this map
274 /// stores the `Span` of the last one and its index in the
275 /// postorder of the Visitor traversal on the HIR.
277 /// HIR Visitor postorder indexes might seem like a peculiar
278 /// thing to care about. but it turns out that HIR bindings
279 /// and the temporary results of HIR expressions are never
280 /// storage-live at the end of HIR nodes with postorder indexes
281 /// lower than theirs, and therefore don't need to be suspended
282 /// at yield-points at these indexes.
284 /// For an example, suppose we have some code such as:
285 /// ```rust,ignore (example)
286 /// foo(f(), yield y, bar(g()))
289 /// With the HIR tree (calls numbered for expository purposes)
291 /// Call#0(foo, [Call#1(f), Yield(y), Call#2(bar, Call#3(g))])
294 /// Obviously, the result of `f()` was created before the yield
295 /// (and therefore needs to be kept valid over the yield) while
296 /// the result of `g()` occurs after the yield (and therefore
297 /// doesn't). If we want to infer that, we can look at the
298 /// postorder traversal:
300 /// `foo` `f` Call#1 `y` Yield `bar` `g` Call#3 Call#2 Call#0
303 /// In which we can easily see that `Call#1` occurs before the yield,
304 /// and `Call#3` after it.
306 /// To see that this method works, consider:
308 /// Let `D` be our binding/temporary and `U` be our other HIR node, with
309 /// `HIR-postorder(U) < HIR-postorder(D)` (in our example, U would be
310 /// the yield and D would be one of the calls). Let's show that
311 /// `D` is storage-dead at `U`.
313 /// Remember that storage-live/storage-dead refers to the state of
314 /// the *storage*, and does not consider moves/drop flags.
317 /// 1. From the ordering guarantee of HIR visitors (see
318 /// `rustc::hir::intravisit`), `D` does not dominate `U`.
319 /// 2. Therefore, `D` is *potentially* storage-dead at `U` (because
320 /// we might visit `U` without ever getting to `D`).
321 /// 3. However, we guarantee that at each HIR point, each
322 /// binding/temporary is always either always storage-live
323 /// or always storage-dead. This is what is being guaranteed
324 /// by `terminating_scopes` including all blocks where the
325 /// count of executions is not guaranteed.
326 /// 4. By `2.` and `3.`, `D` is *statically* storage-dead at `U`,
329 /// This property ought to not on (3) in an essential way -- it
330 /// is probably still correct even if we have "unrestricted" terminating
331 /// scopes. However, why use the complicated proof when a simple one
334 /// A subtle thing: `box` expressions, such as `box (&x, yield 2, &y)`. It
335 /// might seem that a `box` expression creates a `Box<T>` temporary
336 /// when it *starts* executing, at `HIR-preorder(BOX-EXPR)`. That might
337 /// be true in the MIR desugaring, but it is not important in the semantics.
339 /// The reason is that semantically, until the `box` expression returns,
340 /// the values are still owned by their containing expressions. So
341 /// we'll see that `&x`.
342 pub yield_in_scope: FxHashMap<Scope, YieldData>,
344 /// The number of visit_expr and visit_pat calls done in the body.
345 /// Used to sanity check visit_expr/visit_pat call count when
346 /// calculating generator interiors.
347 pub body_expr_count: FxHashMap<hir::BodyId, usize>,
350 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
351 pub struct YieldData {
352 /// The `Span` of the yield.
354 /// The number of expressions and patterns appearing before the `yield` in the body plus one.
355 pub expr_and_pat_count: usize,
356 pub source: hir::YieldSource,
359 impl<'tcx> ScopeTree {
360 pub fn record_scope_parent(&mut self, child: Scope, parent: Option<(Scope, ScopeDepth)>) {
361 debug!("{:?}.parent = {:?}", child, parent);
363 if let Some(p) = parent {
364 let prev = self.parent_map.insert(child, p);
365 assert!(prev.is_none());
368 // Record the destruction scopes for later so we can query them.
369 if let ScopeData::Destruction = child.data {
370 self.destruction_scopes.insert(child.item_local_id(), child);
374 pub fn each_encl_scope<E>(&self, mut e: E)
376 E: FnMut(Scope, Scope),
378 for (&child, &parent) in &self.parent_map {
383 pub fn each_var_scope<E>(&self, mut e: E)
385 E: FnMut(&hir::ItemLocalId, Scope),
387 for (child, &parent) in self.var_map.iter() {
392 pub fn opt_destruction_scope(&self, n: hir::ItemLocalId) -> Option<Scope> {
393 self.destruction_scopes.get(&n).cloned()
396 /// Records that `sub_closure` is defined within `sup_closure`. These IDs
397 /// should be the ID of the block that is the fn body, which is
398 /// also the root of the region hierarchy for that fn.
399 pub fn record_closure_parent(
401 sub_closure: hir::ItemLocalId,
402 sup_closure: hir::ItemLocalId,
405 "record_closure_parent(sub_closure={:?}, sup_closure={:?})",
406 sub_closure, sup_closure
408 assert!(sub_closure != sup_closure);
409 let previous = self.closure_tree.insert(sub_closure, sup_closure);
410 assert!(previous.is_none());
413 pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
414 debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
415 assert!(var != lifetime.item_local_id());
416 self.var_map.insert(var, lifetime);
419 pub fn record_rvalue_scope(&mut self, var: hir::ItemLocalId, lifetime: Option<Scope>) {
420 debug!("record_rvalue_scope(sub={:?}, sup={:?})", var, lifetime);
421 if let Some(lifetime) = lifetime {
422 assert!(var != lifetime.item_local_id());
424 self.rvalue_scopes.insert(var, lifetime);
427 /// Returns the narrowest scope that encloses `id`, if any.
428 pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
429 self.parent_map.get(&id).cloned().map(|(p, _)| p)
432 /// Returns the narrowest scope that encloses `id`, if any.
433 #[allow(dead_code)] // used in cfg
434 pub fn encl_scope(&self, id: Scope) -> Scope {
435 self.opt_encl_scope(id).unwrap()
438 /// Returns the lifetime of the local variable `var_id`
439 pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Scope {
443 .unwrap_or_else(|| bug!("no enclosing scope for id {:?}", var_id))
446 /// Returns the scope when the temp created by `expr_id` will be cleaned up.
447 pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> Option<Scope> {
448 // Check for a designated rvalue scope.
449 if let Some(&s) = self.rvalue_scopes.get(&expr_id) {
450 debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s);
454 // Otherwise, locate the innermost terminating scope
455 // if there's one. Static items, for instance, won't
456 // have an enclosing scope, hence no scope will be
458 let mut id = Scope { id: expr_id, data: ScopeData::Node };
460 while let Some(&(p, _)) = self.parent_map.get(&id) {
462 ScopeData::Destruction => {
463 debug!("temporary_scope({:?}) = {:?} [enclosing]", expr_id, id);
470 debug!("temporary_scope({:?}) = None", expr_id);
474 /// Returns the lifetime of the variable `id`.
475 pub fn var_region(&self, id: hir::ItemLocalId) -> ty::RegionKind {
476 let scope = ty::ReScope(self.var_scope(id));
477 debug!("var_region({:?}) = {:?}", id, scope);
481 pub fn scopes_intersect(&self, scope1: Scope, scope2: Scope) -> bool {
482 self.is_subscope_of(scope1, scope2) || self.is_subscope_of(scope2, scope1)
485 /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and
486 /// `false` otherwise.
487 pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool {
488 let mut s = subscope;
489 debug!("is_subscope_of({:?}, {:?})", subscope, superscope);
490 while superscope != s {
491 match self.opt_encl_scope(s) {
493 debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
496 Some(scope) => s = scope,
500 debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
505 /// Returns the ID of the innermost containing body.
506 pub fn containing_body(&self, mut scope: Scope) -> Option<hir::ItemLocalId> {
508 if let ScopeData::CallSite = scope.data {
509 return Some(scope.item_local_id());
512 scope = self.opt_encl_scope(scope)?;
516 /// Finds the nearest common ancestor of two scopes. That is, finds the
517 /// smallest scope which is greater than or equal to both `scope_a` and
519 pub fn nearest_common_ancestor(&self, scope_a: Scope, scope_b: Scope) -> Scope {
520 if scope_a == scope_b {
527 // Get the depth of each scope's parent. If either scope has no parent,
528 // it must be the root, which means we can stop immediately because the
529 // root must be the nearest common ancestor. (In practice, this is
530 // moderately common.)
531 let (parent_a, parent_a_depth) = match self.parent_map.get(&a) {
535 let (parent_b, parent_b_depth) = match self.parent_map.get(&b) {
540 if parent_a_depth > parent_b_depth {
541 // `a` is lower than `b`. Move `a` up until it's at the same depth
542 // as `b`. The first move up is trivial because we already found
543 // `parent_a` above; the loop does the remaining N-1 moves.
545 for _ in 0..(parent_a_depth - parent_b_depth - 1) {
546 a = self.parent_map.get(&a).unwrap().0;
548 } else if parent_b_depth > parent_a_depth {
549 // `b` is lower than `a`.
551 for _ in 0..(parent_b_depth - parent_a_depth - 1) {
552 b = self.parent_map.get(&b).unwrap().0;
555 // Both scopes are at the same depth, and we know they're not equal
556 // because that case was tested for at the top of this function. So
557 // we can trivially move them both up one level now.
558 assert!(parent_a_depth != 0);
563 // Now both scopes are at the same level. We move upwards in lockstep
564 // until they match. In practice, this loop is almost always executed
565 // zero times because `a` is almost always a direct ancestor of `b` or
568 a = self.parent_map.get(&a).unwrap().0;
569 b = self.parent_map.get(&b).unwrap().0;
575 /// Assuming that the provided region was defined within this `ScopeTree`,
576 /// returns the outermost `Scope` that the region outlives.
577 pub fn early_free_scope(&self, tcx: TyCtxt<'tcx>, br: &ty::EarlyBoundRegion) -> Scope {
578 let param_owner = tcx.parent(br.def_id).unwrap();
580 let param_owner_id = tcx.hir().as_local_hir_id(param_owner).unwrap();
583 .maybe_body_owned_by(param_owner_id)
584 .map(|body_id| tcx.hir().body(body_id).value.hir_id.local_id)
586 // The lifetime was defined on node that doesn't own a body,
587 // which in practice can only mean a trait or an impl, that
588 // is the parent of a method, and that is enforced below.
589 if Some(param_owner_id) != self.root_parent {
590 tcx.sess.delay_span_bug(
593 "free_scope: {:?} not recognized by the \
594 region scope tree for {:?} / {:?}",
596 self.root_parent.map(|id| tcx.hir().local_def_id(id)),
597 self.root_body.map(|hir_id| DefId::local(hir_id.owner))
602 // The trait/impl lifetime is in scope for the method's body.
603 self.root_body.unwrap().local_id
606 Scope { id: scope, data: ScopeData::CallSite }
609 /// Assuming that the provided region was defined within this `ScopeTree`,
610 /// returns the outermost `Scope` that the region outlives.
611 pub fn free_scope(&self, tcx: TyCtxt<'tcx>, fr: &ty::FreeRegion) -> Scope {
612 let param_owner = match fr.bound_region {
613 ty::BoundRegion::BrNamed(def_id, _) => tcx.parent(def_id).unwrap(),
617 // Ensure that the named late-bound lifetimes were defined
618 // on the same function that they ended up being freed in.
619 assert_eq!(param_owner, fr.scope);
621 let param_owner_id = tcx.hir().as_local_hir_id(param_owner).unwrap();
622 let body_id = tcx.hir().body_owned_by(param_owner_id);
623 Scope { id: tcx.hir().body(body_id).value.hir_id.local_id, data: ScopeData::CallSite }
626 /// Checks whether the given scope contains a `yield`. If so,
627 /// returns `Some((span, expr_count))` with the span of a yield we found and
628 /// the number of expressions and patterns appearing before the `yield` in the body + 1.
629 /// If there a are multiple yields in a scope, the one with the highest number is returned.
630 pub fn yield_in_scope(&self, scope: Scope) -> Option<YieldData> {
631 self.yield_in_scope.get(&scope).cloned()
634 /// Gives the number of expressions visited in a body.
635 /// Used to sanity check visit_expr call count when
636 /// calculating generator interiors.
637 pub fn body_expr_count(&self, body_id: hir::BodyId) -> Option<usize> {
638 self.body_expr_count.get(&body_id).map(|r| *r)
642 impl<'a> HashStable<StableHashingContext<'a>> for ScopeTree {
643 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
650 ref destruction_scopes,
656 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
657 root_body.hash_stable(hcx, hasher);
658 root_parent.hash_stable(hcx, hasher);
661 body_expr_count.hash_stable(hcx, hasher);
662 parent_map.hash_stable(hcx, hasher);
663 var_map.hash_stable(hcx, hasher);
664 destruction_scopes.hash_stable(hcx, hasher);
665 rvalue_scopes.hash_stable(hcx, hasher);
666 closure_tree.hash_stable(hcx, hasher);
667 yield_in_scope.hash_stable(hcx, hasher);