]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/region.rs
use .copied() instead of .map(|x| *x) on iterators
[rust.git] / src / librustc / 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 guide].
6 //!
7 //! [rustc guide]: https://rust-lang.github.io/rustc-guide/mir/borrowck.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::def_id::DefId;
13 use rustc_hir::Node;
14
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};
19
20 use std::fmt;
21
22 /// Represents a statically-describable scope that can be used to
23 /// bound the lifetime/region for values.
24 ///
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.
28 ///
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`.
34 ///
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.
38 ///
39 /// So: the following code can be broken down into the scopes beneath:
40 ///
41 /// ```text
42 /// let a = f().g( 'b: { let x = d(); let y = d(); x.h(y)  }   ) ;
43 ///
44 ///                                                              +-+ (D12.)
45 ///                                                        +-+       (D11.)
46 ///                                              +---------+         (R10.)
47 ///                                              +-+                  (D9.)
48 ///                                   +----------+                    (M8.)
49 ///                                 +----------------------+          (R7.)
50 ///                                 +-+                               (D6.)
51 ///                      +----------+                                 (M5.)
52 ///                    +-----------------------------------+          (M4.)
53 ///         +--------------------------------------------------+      (M3.)
54 ///         +--+                                                      (M2.)
55 /// +-----------------------------------------------------------+     (M1.)
56 ///
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()).
69 /// ```
70 ///
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.
79 //
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.
84 #[derive(
85     Clone,
86     PartialEq,
87     PartialOrd,
88     Eq,
89     Ord,
90     Hash,
91     Copy,
92     RustcEncodable,
93     RustcDecodable,
94     HashStable
95 )]
96 pub struct Scope {
97     pub id: hir::ItemLocalId,
98     pub data: ScopeData,
99 }
100
101 impl fmt::Debug for Scope {
102     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
103         match self.data {
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!(
109                 fmt,
110                 "Remainder {{ block: {:?}, first_statement_index: {}}}",
111                 self.id,
112                 fsi.as_u32(),
113             ),
114         }
115     }
116 }
117
118 #[derive(
119     Clone,
120     PartialEq,
121     PartialOrd,
122     Eq,
123     Ord,
124     Hash,
125     Debug,
126     Copy,
127     RustcEncodable,
128     RustcDecodable,
129     HashStable
130 )]
131 pub enum ScopeData {
132     Node,
133
134     /// Scope of the call-site for a function or closure
135     /// (outlives the arguments as well as the body).
136     CallSite,
137
138     /// Scope of arguments passed to a function or closure
139     /// (they outlive its body).
140     Arguments,
141
142     /// Scope of destructors for temporaries of node-id.
143     Destruction,
144
145     /// Scope following a `let id = expr;` binding in a block.
146     Remainder(FirstStatementIndex),
147 }
148
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`.
155     ///
156     /// For example, given `{ let (a, b) = EXPR_1; let c = EXPR_2; ... }`:
157     ///
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`.
164     ///
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 {
168         derive [HashStable]
169     }
170 }
171
172 // compilation error if size of `ScopeData` is not the same as a `u32`
173 static_assert_size!(ScopeData, 4);
174
175 impl Scope {
176     /// Returns a item-local ID associated with this scope.
177     ///
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 {
181         self.id
182     }
183
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,
188         }
189     }
190
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
193     /// the AST.
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 {
197             return DUMMY_SP;
198         }
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.
206                 //
207                 // (This is the special case aluded to in the
208                 // doc-comment for this method)
209
210                 let stmt_span = blk.stmts[first_statement_index.index()].span;
211
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());
216                 }
217             }
218         }
219         span
220     }
221 }
222
223 pub type ScopeDepth = u32;
224
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>,
230
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>,
235
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)>,
243
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>,
247
248     /// Maps from a `NodeId` to the associated destruction scope (if any).
249     destruction_scopes: FxHashMap<hir::ItemLocalId, Scope>,
250
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>>,
260
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
270     /// more details.
271     closure_tree: FxHashMap<hir::ItemLocalId, hir::ItemLocalId>,
272
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.
276     ///
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.
283     ///
284     /// For an example, suppose we have some code such as:
285     /// ```rust,ignore (example)
286     ///     foo(f(), yield y, bar(g()))
287     /// ```
288     ///
289     /// With the HIR tree (calls numbered for expository purposes)
290     /// ```
291     ///     Call#0(foo, [Call#1(f), Yield(y), Call#2(bar, Call#3(g))])
292     /// ```
293     ///
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:
299     /// ```plain,ignore
300     ///     `foo` `f` Call#1 `y` Yield `bar` `g` Call#3 Call#2 Call#0
301     /// ```
302     ///
303     /// In which we can easily see that `Call#1` occurs before the yield,
304     /// and `Call#3` after it.
305     ///
306     /// To see that this method works, consider:
307     ///
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`.
312     ///
313     /// Remember that storage-live/storage-dead refers to the state of
314     /// the *storage*, and does not consider moves/drop flags.
315     ///
316     /// Then:
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`,
327     ///     QED.
328     ///
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
332     /// works?
333     ///
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.
338     ///
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>,
343
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>,
348 }
349
350 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
351 pub struct YieldData {
352     /// The `Span` of the yield.
353     pub span: Span,
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,
357 }
358
359 impl<'tcx> ScopeTree {
360     pub fn record_scope_parent(&mut self, child: Scope, parent: Option<(Scope, ScopeDepth)>) {
361         debug!("{:?}.parent = {:?}", child, parent);
362
363         if let Some(p) = parent {
364             let prev = self.parent_map.insert(child, p);
365             assert!(prev.is_none());
366         }
367
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);
371         }
372     }
373
374     pub fn each_encl_scope<E>(&self, mut e: E)
375     where
376         E: FnMut(Scope, Scope),
377     {
378         for (&child, &parent) in &self.parent_map {
379             e(child, parent.0)
380         }
381     }
382
383     pub fn each_var_scope<E>(&self, mut e: E)
384     where
385         E: FnMut(&hir::ItemLocalId, Scope),
386     {
387         for (child, &parent) in self.var_map.iter() {
388             e(child, parent)
389         }
390     }
391
392     pub fn opt_destruction_scope(&self, n: hir::ItemLocalId) -> Option<Scope> {
393         self.destruction_scopes.get(&n).cloned()
394     }
395
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(
400         &mut self,
401         sub_closure: hir::ItemLocalId,
402         sup_closure: hir::ItemLocalId,
403     ) {
404         debug!(
405             "record_closure_parent(sub_closure={:?}, sup_closure={:?})",
406             sub_closure, sup_closure
407         );
408         assert!(sub_closure != sup_closure);
409         let previous = self.closure_tree.insert(sub_closure, sup_closure);
410         assert!(previous.is_none());
411     }
412
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);
417     }
418
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());
423         }
424         self.rvalue_scopes.insert(var, lifetime);
425     }
426
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)
430     }
431
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()
436     }
437
438     /// Returns the lifetime of the local variable `var_id`
439     pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Scope {
440         self.var_map
441             .get(&var_id)
442             .cloned()
443             .unwrap_or_else(|| bug!("no enclosing scope for id {:?}", var_id))
444     }
445
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);
451             return s;
452         }
453
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
457         // returned.
458         let mut id = Scope { id: expr_id, data: ScopeData::Node };
459
460         while let Some(&(p, _)) = self.parent_map.get(&id) {
461             match p.data {
462                 ScopeData::Destruction => {
463                     debug!("temporary_scope({:?}) = {:?} [enclosing]", expr_id, id);
464                     return Some(id);
465                 }
466                 _ => id = p,
467             }
468         }
469
470         debug!("temporary_scope({:?}) = None", expr_id);
471         return None;
472     }
473
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);
478         scope
479     }
480
481     pub fn scopes_intersect(&self, scope1: Scope, scope2: Scope) -> bool {
482         self.is_subscope_of(scope1, scope2) || self.is_subscope_of(scope2, scope1)
483     }
484
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) {
492                 None => {
493                     debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
494                     return false;
495                 }
496                 Some(scope) => s = scope,
497             }
498         }
499
500         debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
501
502         return true;
503     }
504
505     /// Returns the ID of the innermost containing body.
506     pub fn containing_body(&self, mut scope: Scope) -> Option<hir::ItemLocalId> {
507         loop {
508             if let ScopeData::CallSite = scope.data {
509                 return Some(scope.item_local_id());
510             }
511
512             scope = self.opt_encl_scope(scope)?;
513         }
514     }
515
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
518     /// `scope_b`.
519     pub fn nearest_common_ancestor(&self, scope_a: Scope, scope_b: Scope) -> Scope {
520         if scope_a == scope_b {
521             return scope_a;
522         }
523
524         let mut a = scope_a;
525         let mut b = scope_b;
526
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) {
532             Some(pd) => *pd,
533             None => return a,
534         };
535         let (parent_b, parent_b_depth) = match self.parent_map.get(&b) {
536             Some(pd) => *pd,
537             None => return b,
538         };
539
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.
544             a = parent_a;
545             for _ in 0..(parent_a_depth - parent_b_depth - 1) {
546                 a = self.parent_map.get(&a).unwrap().0;
547             }
548         } else if parent_b_depth > parent_a_depth {
549             // `b` is lower than `a`.
550             b = parent_b;
551             for _ in 0..(parent_b_depth - parent_a_depth - 1) {
552                 b = self.parent_map.get(&b).unwrap().0;
553             }
554         } else {
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);
559             a = parent_a;
560             b = parent_b;
561         }
562
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
566         // vice versa.
567         while a != b {
568             a = self.parent_map.get(&a).unwrap().0;
569             b = self.parent_map.get(&b).unwrap().0;
570         }
571
572         a
573     }
574
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();
579
580         let param_owner_id = tcx.hir().as_local_hir_id(param_owner).unwrap();
581         let scope = tcx
582             .hir()
583             .maybe_body_owned_by(param_owner_id)
584             .map(|body_id| tcx.hir().body(body_id).value.hir_id.local_id)
585             .unwrap_or_else(|| {
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(
591                         DUMMY_SP,
592                         &format!(
593                             "free_scope: {:?} not recognized by the \
594                               region scope tree for {:?} / {:?}",
595                             param_owner,
596                             self.root_parent.map(|id| tcx.hir().local_def_id(id)),
597                             self.root_body.map(|hir_id| DefId::local(hir_id.owner))
598                         ),
599                     );
600                 }
601
602                 // The trait/impl lifetime is in scope for the method's body.
603                 self.root_body.unwrap().local_id
604             });
605
606         Scope { id: scope, data: ScopeData::CallSite }
607     }
608
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(),
614             _ => fr.scope,
615         };
616
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);
620
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 }
624     }
625
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()
632     }
633
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).copied()
639     }
640 }
641
642 impl<'a> HashStable<StableHashingContext<'a>> for ScopeTree {
643     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
644         let ScopeTree {
645             root_body,
646             root_parent,
647             ref body_expr_count,
648             ref parent_map,
649             ref var_map,
650             ref destruction_scopes,
651             ref rvalue_scopes,
652             ref closure_tree,
653             ref yield_in_scope,
654         } = *self;
655
656         hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
657             root_body.hash_stable(hcx, hasher);
658             root_parent.hash_stable(hcx, hasher);
659         });
660
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);
668     }
669 }