]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/middle/region.rs
f02d8fe8ad6018278a050cbefcb487c5b4ed942a
[rust.git] / src / librustc_middle / middle / region.rs
1 //! This file declares the `ScopeTree` type, which describes
2 //! the parent links in the region hierarchy.
3 //!
4 //! For more information about how MIR-based region-checking works,
5 //! see the [rustc dev guide].
6 //!
7 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/borrow_check.html
8
9 use crate::ich::{NodeIdHashingMode, StableHashingContext};
10 use crate::ty::{self, DefIdTree, TyCtxt};
11 use rustc_hir as hir;
12 use rustc_hir::Node;
13
14 use rustc_data_structures::fx::FxHashMap;
15 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
16 use rustc_macros::HashStable;
17 use rustc_span::{Span, DUMMY_SP};
18
19 use std::fmt;
20
21 /// Represents a statically-describable scope that can be used to
22 /// bound the lifetime/region for values.
23 ///
24 /// `Node(node_id)`: Any AST node that has any scope at all has the
25 /// `Node(node_id)` scope. Other variants represent special cases not
26 /// immediately derivable from the abstract syntax tree structure.
27 ///
28 /// `DestructionScope(node_id)` represents the scope of destructors
29 /// implicitly-attached to `node_id` that run immediately after the
30 /// expression for `node_id` itself. Not every AST node carries a
31 /// `DestructionScope`, but those that are `terminating_scopes` do;
32 /// see discussion with `ScopeTree`.
33 ///
34 /// `Remainder { block, statement_index }` represents
35 /// the scope of user code running immediately after the initializer
36 /// expression for the indexed statement, until the end of the block.
37 ///
38 /// So: the following code can be broken down into the scopes beneath:
39 ///
40 /// ```text
41 /// let a = f().g( 'b: { let x = d(); let y = d(); x.h(y)  }   ) ;
42 ///
43 ///                                                              +-+ (D12.)
44 ///                                                        +-+       (D11.)
45 ///                                              +---------+         (R10.)
46 ///                                              +-+                  (D9.)
47 ///                                   +----------+                    (M8.)
48 ///                                 +----------------------+          (R7.)
49 ///                                 +-+                               (D6.)
50 ///                      +----------+                                 (M5.)
51 ///                    +-----------------------------------+          (M4.)
52 ///         +--------------------------------------------------+      (M3.)
53 ///         +--+                                                      (M2.)
54 /// +-----------------------------------------------------------+     (M1.)
55 ///
56 ///  (M1.): Node scope of the whole `let a = ...;` statement.
57 ///  (M2.): Node scope of the `f()` expression.
58 ///  (M3.): Node scope of the `f().g(..)` expression.
59 ///  (M4.): Node scope of the block labeled `'b:`.
60 ///  (M5.): Node scope of the `let x = d();` statement
61 ///  (D6.): DestructionScope for temporaries created during M5.
62 ///  (R7.): Remainder scope for block `'b:`, stmt 0 (let x = ...).
63 ///  (M8.): Node scope of the `let y = d();` statement.
64 ///  (D9.): DestructionScope for temporaries created during M8.
65 /// (R10.): Remainder scope for block `'b:`, stmt 1 (let y = ...).
66 /// (D11.): DestructionScope for temporaries and bindings from block `'b:`.
67 /// (D12.): DestructionScope for temporaries created during M1 (e.g., f()).
68 /// ```
69 ///
70 /// Note that while the above picture shows the destruction scopes
71 /// as following their corresponding node scopes, in the internal
72 /// data structures of the compiler the destruction scopes are
73 /// represented as enclosing parents. This is sound because we use the
74 /// enclosing parent relationship just to ensure that referenced
75 /// values live long enough; phrased another way, the starting point
76 /// of each range is not really the important thing in the above
77 /// picture, but rather the ending point.
78 //
79 // FIXME(pnkfelix): this currently derives `PartialOrd` and `Ord` to
80 // placate the same deriving in `ty::FreeRegion`, but we may want to
81 // actually attach a more meaningful ordering to scopes than the one
82 // generated via deriving here.
83 #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Copy, RustcEncodable, RustcDecodable)]
84 #[derive(HashStable)]
85 pub struct Scope {
86     pub id: hir::ItemLocalId,
87     pub data: ScopeData,
88 }
89
90 impl fmt::Debug for Scope {
91     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
92         match self.data {
93             ScopeData::Node => write!(fmt, "Node({:?})", self.id),
94             ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.id),
95             ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.id),
96             ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.id),
97             ScopeData::Remainder(fsi) => write!(
98                 fmt,
99                 "Remainder {{ block: {:?}, first_statement_index: {}}}",
100                 self.id,
101                 fsi.as_u32(),
102             ),
103         }
104     }
105 }
106
107 #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, RustcEncodable, RustcDecodable)]
108 #[derive(HashStable)]
109 pub enum ScopeData {
110     Node,
111
112     /// Scope of the call-site for a function or closure
113     /// (outlives the arguments as well as the body).
114     CallSite,
115
116     /// Scope of arguments passed to a function or closure
117     /// (they outlive its body).
118     Arguments,
119
120     /// Scope of destructors for temporaries of node-id.
121     Destruction,
122
123     /// Scope following a `let id = expr;` binding in a block.
124     Remainder(FirstStatementIndex),
125 }
126
127 rustc_index::newtype_index! {
128     /// Represents a subscope of `block` for a binding that is introduced
129     /// by `block.stmts[first_statement_index]`. Such subscopes represent
130     /// a suffix of the block. Note that each subscope does not include
131     /// the initializer expression, if any, for the statement indexed by
132     /// `first_statement_index`.
133     ///
134     /// For example, given `{ let (a, b) = EXPR_1; let c = EXPR_2; ... }`:
135     ///
136     /// * The subscope with `first_statement_index == 0` is scope of both
137     ///   `a` and `b`; it does not include EXPR_1, but does include
138     ///   everything after that first `let`. (If you want a scope that
139     ///   includes EXPR_1 as well, then do not use `Scope::Remainder`,
140     ///   but instead another `Scope` that encompasses the whole block,
141     ///   e.g., `Scope::Node`.
142     ///
143     /// * The subscope with `first_statement_index == 1` is scope of `c`,
144     ///   and thus does not include EXPR_2, but covers the `...`.
145     pub struct FirstStatementIndex {
146         derive [HashStable]
147     }
148 }
149
150 // compilation error if size of `ScopeData` is not the same as a `u32`
151 static_assert_size!(ScopeData, 4);
152
153 impl Scope {
154     /// Returns a item-local ID associated with this scope.
155     ///
156     /// N.B., likely to be replaced as API is refined; e.g., pnkfelix
157     /// anticipates `fn entry_node_id` and `fn each_exit_node_id`.
158     pub fn item_local_id(&self) -> hir::ItemLocalId {
159         self.id
160     }
161
162     pub fn hir_id(&self, scope_tree: &ScopeTree) -> Option<hir::HirId> {
163         scope_tree
164             .root_body
165             .map(|hir_id| hir::HirId { owner: hir_id.owner, local_id: self.item_local_id() })
166     }
167
168     /// Returns the span of this `Scope`. Note that in general the
169     /// returned span may not correspond to the span of any `NodeId` in
170     /// the AST.
171     pub fn span(&self, tcx: TyCtxt<'_>, scope_tree: &ScopeTree) -> Span {
172         let hir_id = match self.hir_id(scope_tree) {
173             Some(hir_id) => hir_id,
174             None => return DUMMY_SP,
175         };
176         let span = tcx.hir().span(hir_id);
177         if let ScopeData::Remainder(first_statement_index) = self.data {
178             if let Node::Block(ref blk) = tcx.hir().get(hir_id) {
179                 // Want span for scope starting after the
180                 // indexed statement and ending at end of
181                 // `blk`; reuse span of `blk` and shift `lo`
182                 // forward to end of indexed statement.
183                 //
184                 // (This is the special case alluded to in the
185                 // doc-comment for this method)
186
187                 let stmt_span = blk.stmts[first_statement_index.index()].span;
188
189                 // To avoid issues with macro-generated spans, the span
190                 // of the statement must be nested in that of the block.
191                 if span.lo() <= stmt_span.lo() && stmt_span.lo() <= span.hi() {
192                     return Span::new(stmt_span.lo(), span.hi(), span.ctxt());
193                 }
194             }
195         }
196         span
197     }
198 }
199
200 pub type ScopeDepth = u32;
201
202 /// The region scope tree encodes information about region relationships.
203 #[derive(Default, Debug)]
204 pub struct ScopeTree {
205     /// If not empty, this body is the root of this region hierarchy.
206     pub root_body: Option<hir::HirId>,
207
208     /// The parent of the root body owner, if the latter is an
209     /// an associated const or method, as impls/traits can also
210     /// have lifetime parameters free in this body.
211     pub root_parent: Option<hir::HirId>,
212
213     /// Maps from a scope ID to the enclosing scope id;
214     /// this is usually corresponding to the lexical nesting, though
215     /// in the case of closures the parent scope is the innermost
216     /// conditional expression or repeating block. (Note that the
217     /// enclosing scope ID for the block associated with a closure is
218     /// the closure itself.)
219     pub parent_map: FxHashMap<Scope, (Scope, ScopeDepth)>,
220
221     /// Maps from a variable or binding ID to the block in which that
222     /// variable is declared.
223     var_map: FxHashMap<hir::ItemLocalId, Scope>,
224
225     /// Maps from a `NodeId` to the associated destruction scope (if any).
226     destruction_scopes: FxHashMap<hir::ItemLocalId, Scope>,
227
228     /// `rvalue_scopes` includes entries for those expressions whose
229     /// cleanup scope is larger than the default. The map goes from the
230     /// expression ID to the cleanup scope id. For rvalues not present in
231     /// this table, the appropriate cleanup scope is the innermost
232     /// enclosing statement, conditional expression, or repeating
233     /// block (see `terminating_scopes`).
234     /// In constants, None is used to indicate that certain expressions
235     /// escape into 'static and should have no local cleanup scope.
236     rvalue_scopes: FxHashMap<hir::ItemLocalId, Option<Scope>>,
237
238     /// Encodes the hierarchy of fn bodies. Every fn body (including
239     /// closures) forms its own distinct region hierarchy, rooted in
240     /// the block that is the fn body. This map points from the ID of
241     /// that root block to the ID of the root block for the enclosing
242     /// fn, if any. Thus the map structures the fn bodies into a
243     /// hierarchy based on their lexical mapping. This is used to
244     /// handle the relationships between regions in a fn and in a
245     /// closure defined by that fn. See the "Modeling closures"
246     /// section of the README in infer::region_constraints for
247     /// more details.
248     closure_tree: FxHashMap<hir::ItemLocalId, hir::ItemLocalId>,
249
250     /// If there are any `yield` nested within a scope, this map
251     /// stores the `Span` of the last one and its index in the
252     /// postorder of the Visitor traversal on the HIR.
253     ///
254     /// HIR Visitor postorder indexes might seem like a peculiar
255     /// thing to care about. but it turns out that HIR bindings
256     /// and the temporary results of HIR expressions are never
257     /// storage-live at the end of HIR nodes with postorder indexes
258     /// lower than theirs, and therefore don't need to be suspended
259     /// at yield-points at these indexes.
260     ///
261     /// For an example, suppose we have some code such as:
262     /// ```rust,ignore (example)
263     ///     foo(f(), yield y, bar(g()))
264     /// ```
265     ///
266     /// With the HIR tree (calls numbered for expository purposes)
267     /// ```
268     ///     Call#0(foo, [Call#1(f), Yield(y), Call#2(bar, Call#3(g))])
269     /// ```
270     ///
271     /// Obviously, the result of `f()` was created before the yield
272     /// (and therefore needs to be kept valid over the yield) while
273     /// the result of `g()` occurs after the yield (and therefore
274     /// doesn't). If we want to infer that, we can look at the
275     /// postorder traversal:
276     /// ```plain,ignore
277     ///     `foo` `f` Call#1 `y` Yield `bar` `g` Call#3 Call#2 Call#0
278     /// ```
279     ///
280     /// In which we can easily see that `Call#1` occurs before the yield,
281     /// and `Call#3` after it.
282     ///
283     /// To see that this method works, consider:
284     ///
285     /// Let `D` be our binding/temporary and `U` be our other HIR node, with
286     /// `HIR-postorder(U) < HIR-postorder(D)` (in our example, U would be
287     /// the yield and D would be one of the calls). Let's show that
288     /// `D` is storage-dead at `U`.
289     ///
290     /// Remember that storage-live/storage-dead refers to the state of
291     /// the *storage*, and does not consider moves/drop flags.
292     ///
293     /// Then:
294     ///     1. From the ordering guarantee of HIR visitors (see
295     ///     `rustc_hir::intravisit`), `D` does not dominate `U`.
296     ///     2. Therefore, `D` is *potentially* storage-dead at `U` (because
297     ///     we might visit `U` without ever getting to `D`).
298     ///     3. However, we guarantee that at each HIR point, each
299     ///     binding/temporary is always either always storage-live
300     ///     or always storage-dead. This is what is being guaranteed
301     ///     by `terminating_scopes` including all blocks where the
302     ///     count of executions is not guaranteed.
303     ///     4. By `2.` and `3.`, `D` is *statically* storage-dead at `U`,
304     ///     QED.
305     ///
306     /// This property ought to not on (3) in an essential way -- it
307     /// is probably still correct even if we have "unrestricted" terminating
308     /// scopes. However, why use the complicated proof when a simple one
309     /// works?
310     ///
311     /// A subtle thing: `box` expressions, such as `box (&x, yield 2, &y)`. It
312     /// might seem that a `box` expression creates a `Box<T>` temporary
313     /// when it *starts* executing, at `HIR-preorder(BOX-EXPR)`. That might
314     /// be true in the MIR desugaring, but it is not important in the semantics.
315     ///
316     /// The reason is that semantically, until the `box` expression returns,
317     /// the values are still owned by their containing expressions. So
318     /// we'll see that `&x`.
319     pub yield_in_scope: FxHashMap<Scope, YieldData>,
320
321     /// The number of visit_expr and visit_pat calls done in the body.
322     /// Used to sanity check visit_expr/visit_pat call count when
323     /// calculating generator interiors.
324     pub body_expr_count: FxHashMap<hir::BodyId, usize>,
325 }
326
327 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
328 pub struct YieldData {
329     /// The `Span` of the yield.
330     pub span: Span,
331     /// The number of expressions and patterns appearing before the `yield` in the body plus one.
332     pub expr_and_pat_count: usize,
333     pub source: hir::YieldSource,
334 }
335
336 impl<'tcx> ScopeTree {
337     pub fn record_scope_parent(&mut self, child: Scope, parent: Option<(Scope, ScopeDepth)>) {
338         debug!("{:?}.parent = {:?}", child, parent);
339
340         if let Some(p) = parent {
341             let prev = self.parent_map.insert(child, p);
342             assert!(prev.is_none());
343         }
344
345         // Record the destruction scopes for later so we can query them.
346         if let ScopeData::Destruction = child.data {
347             self.destruction_scopes.insert(child.item_local_id(), child);
348         }
349     }
350
351     pub fn each_encl_scope<E>(&self, mut e: E)
352     where
353         E: FnMut(Scope, Scope),
354     {
355         for (&child, &parent) in &self.parent_map {
356             e(child, parent.0)
357         }
358     }
359
360     pub fn each_var_scope<E>(&self, mut e: E)
361     where
362         E: FnMut(&hir::ItemLocalId, Scope),
363     {
364         for (child, &parent) in self.var_map.iter() {
365             e(child, parent)
366         }
367     }
368
369     pub fn opt_destruction_scope(&self, n: hir::ItemLocalId) -> Option<Scope> {
370         self.destruction_scopes.get(&n).cloned()
371     }
372
373     /// Records that `sub_closure` is defined within `sup_closure`. These IDs
374     /// should be the ID of the block that is the fn body, which is
375     /// also the root of the region hierarchy for that fn.
376     pub fn record_closure_parent(
377         &mut self,
378         sub_closure: hir::ItemLocalId,
379         sup_closure: hir::ItemLocalId,
380     ) {
381         debug!(
382             "record_closure_parent(sub_closure={:?}, sup_closure={:?})",
383             sub_closure, sup_closure
384         );
385         assert!(sub_closure != sup_closure);
386         let previous = self.closure_tree.insert(sub_closure, sup_closure);
387         assert!(previous.is_none());
388     }
389
390     pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
391         debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
392         assert!(var != lifetime.item_local_id());
393         self.var_map.insert(var, lifetime);
394     }
395
396     pub fn record_rvalue_scope(&mut self, var: hir::ItemLocalId, lifetime: Option<Scope>) {
397         debug!("record_rvalue_scope(sub={:?}, sup={:?})", var, lifetime);
398         if let Some(lifetime) = lifetime {
399             assert!(var != lifetime.item_local_id());
400         }
401         self.rvalue_scopes.insert(var, lifetime);
402     }
403
404     /// Returns the narrowest scope that encloses `id`, if any.
405     pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
406         self.parent_map.get(&id).cloned().map(|(p, _)| p)
407     }
408
409     /// Returns the narrowest scope that encloses `id`, if any.
410     #[allow(dead_code)] // used in cfg
411     pub fn encl_scope(&self, id: Scope) -> Scope {
412         self.opt_encl_scope(id).unwrap()
413     }
414
415     /// Returns the lifetime of the local variable `var_id`
416     pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Scope {
417         self.var_map
418             .get(&var_id)
419             .cloned()
420             .unwrap_or_else(|| bug!("no enclosing scope for id {:?}", var_id))
421     }
422
423     /// Returns the scope when the temp created by `expr_id` will be cleaned up.
424     pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> Option<Scope> {
425         // Check for a designated rvalue scope.
426         if let Some(&s) = self.rvalue_scopes.get(&expr_id) {
427             debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s);
428             return s;
429         }
430
431         // Otherwise, locate the innermost terminating scope
432         // if there's one. Static items, for instance, won't
433         // have an enclosing scope, hence no scope will be
434         // returned.
435         let mut id = Scope { id: expr_id, data: ScopeData::Node };
436
437         while let Some(&(p, _)) = self.parent_map.get(&id) {
438             match p.data {
439                 ScopeData::Destruction => {
440                     debug!("temporary_scope({:?}) = {:?} [enclosing]", expr_id, id);
441                     return Some(id);
442                 }
443                 _ => id = p,
444             }
445         }
446
447         debug!("temporary_scope({:?}) = None", expr_id);
448         None
449     }
450
451     /// Returns the lifetime of the variable `id`.
452     pub fn var_region(&self, id: hir::ItemLocalId) -> ty::RegionKind {
453         let scope = ty::ReScope(self.var_scope(id));
454         debug!("var_region({:?}) = {:?}", id, scope);
455         scope
456     }
457
458     pub fn scopes_intersect(&self, scope1: Scope, scope2: Scope) -> bool {
459         self.is_subscope_of(scope1, scope2) || self.is_subscope_of(scope2, scope1)
460     }
461
462     /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and
463     /// `false` otherwise.
464     pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool {
465         let mut s = subscope;
466         debug!("is_subscope_of({:?}, {:?})", subscope, superscope);
467         while superscope != s {
468             match self.opt_encl_scope(s) {
469                 None => {
470                     debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
471                     return false;
472                 }
473                 Some(scope) => s = scope,
474             }
475         }
476
477         debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
478
479         true
480     }
481
482     /// Returns the ID of the innermost containing body.
483     pub fn containing_body(&self, mut scope: Scope) -> Option<hir::ItemLocalId> {
484         loop {
485             if let ScopeData::CallSite = scope.data {
486                 return Some(scope.item_local_id());
487             }
488
489             scope = self.opt_encl_scope(scope)?;
490         }
491     }
492
493     /// Finds the nearest common ancestor of two scopes. That is, finds the
494     /// smallest scope which is greater than or equal to both `scope_a` and
495     /// `scope_b`.
496     pub fn nearest_common_ancestor(&self, scope_a: Scope, scope_b: Scope) -> Scope {
497         if scope_a == scope_b {
498             return scope_a;
499         }
500
501         let mut a = scope_a;
502         let mut b = scope_b;
503
504         // Get the depth of each scope's parent. If either scope has no parent,
505         // it must be the root, which means we can stop immediately because the
506         // root must be the nearest common ancestor. (In practice, this is
507         // moderately common.)
508         let (parent_a, parent_a_depth) = match self.parent_map.get(&a) {
509             Some(pd) => *pd,
510             None => return a,
511         };
512         let (parent_b, parent_b_depth) = match self.parent_map.get(&b) {
513             Some(pd) => *pd,
514             None => return b,
515         };
516
517         if parent_a_depth > parent_b_depth {
518             // `a` is lower than `b`. Move `a` up until it's at the same depth
519             // as `b`. The first move up is trivial because we already found
520             // `parent_a` above; the loop does the remaining N-1 moves.
521             a = parent_a;
522             for _ in 0..(parent_a_depth - parent_b_depth - 1) {
523                 a = self.parent_map.get(&a).unwrap().0;
524             }
525         } else if parent_b_depth > parent_a_depth {
526             // `b` is lower than `a`.
527             b = parent_b;
528             for _ in 0..(parent_b_depth - parent_a_depth - 1) {
529                 b = self.parent_map.get(&b).unwrap().0;
530             }
531         } else {
532             // Both scopes are at the same depth, and we know they're not equal
533             // because that case was tested for at the top of this function. So
534             // we can trivially move them both up one level now.
535             assert!(parent_a_depth != 0);
536             a = parent_a;
537             b = parent_b;
538         }
539
540         // Now both scopes are at the same level. We move upwards in lockstep
541         // until they match. In practice, this loop is almost always executed
542         // zero times because `a` is almost always a direct ancestor of `b` or
543         // vice versa.
544         while a != b {
545             a = self.parent_map.get(&a).unwrap().0;
546             b = self.parent_map.get(&b).unwrap().0;
547         }
548
549         a
550     }
551
552     /// Assuming that the provided region was defined within this `ScopeTree`,
553     /// returns the outermost `Scope` that the region outlives.
554     pub fn early_free_scope(&self, tcx: TyCtxt<'tcx>, br: &ty::EarlyBoundRegion) -> Scope {
555         let param_owner = tcx.parent(br.def_id).unwrap();
556
557         let param_owner_id = tcx.hir().as_local_hir_id(param_owner.expect_local());
558         let scope = tcx
559             .hir()
560             .maybe_body_owned_by(param_owner_id)
561             .map(|body_id| tcx.hir().body(body_id).value.hir_id.local_id)
562             .unwrap_or_else(|| {
563                 // The lifetime was defined on node that doesn't own a body,
564                 // which in practice can only mean a trait or an impl, that
565                 // is the parent of a method, and that is enforced below.
566                 if Some(param_owner_id) != self.root_parent {
567                     tcx.sess.delay_span_bug(
568                         DUMMY_SP,
569                         &format!(
570                             "free_scope: {:?} not recognized by the \
571                               region scope tree for {:?} / {:?}",
572                             param_owner,
573                             self.root_parent.map(|id| tcx.hir().local_def_id(id)),
574                             self.root_body.map(|hir_id| hir_id.owner)
575                         ),
576                     );
577                 }
578
579                 // The trait/impl lifetime is in scope for the method's body.
580                 self.root_body.unwrap().local_id
581             });
582
583         Scope { id: scope, data: ScopeData::CallSite }
584     }
585
586     /// Assuming that the provided region was defined within this `ScopeTree`,
587     /// returns the outermost `Scope` that the region outlives.
588     pub fn free_scope(&self, tcx: TyCtxt<'tcx>, fr: &ty::FreeRegion) -> Scope {
589         let param_owner = match fr.bound_region {
590             ty::BoundRegion::BrNamed(def_id, _) => tcx.parent(def_id).unwrap(),
591             _ => fr.scope,
592         };
593
594         // Ensure that the named late-bound lifetimes were defined
595         // on the same function that they ended up being freed in.
596         assert_eq!(param_owner, fr.scope);
597
598         let param_owner_id = tcx.hir().as_local_hir_id(param_owner.expect_local());
599         let body_id = tcx.hir().body_owned_by(param_owner_id);
600         Scope { id: tcx.hir().body(body_id).value.hir_id.local_id, data: ScopeData::CallSite }
601     }
602
603     /// Checks whether the given scope contains a `yield`. If so,
604     /// returns `Some((span, expr_count))` with the span of a yield we found and
605     /// the number of expressions and patterns appearing before the `yield` in the body + 1.
606     /// If there a are multiple yields in a scope, the one with the highest number is returned.
607     pub fn yield_in_scope(&self, scope: Scope) -> Option<YieldData> {
608         self.yield_in_scope.get(&scope).cloned()
609     }
610
611     /// Gives the number of expressions visited in a body.
612     /// Used to sanity check visit_expr call count when
613     /// calculating generator interiors.
614     pub fn body_expr_count(&self, body_id: hir::BodyId) -> Option<usize> {
615         self.body_expr_count.get(&body_id).copied()
616     }
617 }
618
619 impl<'a> HashStable<StableHashingContext<'a>> for ScopeTree {
620     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
621         let ScopeTree {
622             root_body,
623             root_parent,
624             ref body_expr_count,
625             ref parent_map,
626             ref var_map,
627             ref destruction_scopes,
628             ref rvalue_scopes,
629             ref closure_tree,
630             ref yield_in_scope,
631         } = *self;
632
633         hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
634             root_body.hash_stable(hcx, hasher);
635             root_parent.hash_stable(hcx, hasher);
636         });
637
638         body_expr_count.hash_stable(hcx, hasher);
639         parent_map.hash_stable(hcx, hasher);
640         var_map.hash_stable(hcx, hasher);
641         destruction_scopes.hash_stable(hcx, hasher);
642         rvalue_scopes.hash_stable(hcx, hasher);
643         closure_tree.hash_stable(hcx, hasher);
644         yield_in_scope.hash_stable(hcx, hasher);
645     }
646 }