]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | //! This file declares the `ScopeTree` type, which describes |
ea8adc8c | 2 | //! the parent links in the region hierarchy. |
1a4d82fc | 3 | //! |
0531ce1d XL |
4 | //! For more information about how MIR-based region-checking works, |
5 | //! see the [rustc guide]. | |
6 | //! | |
a1dfa0c6 | 7 | //! [rustc guide]: https://rust-lang.github.io/rustc-guide/mir/borrowck.html |
1a4d82fc | 8 | |
dfeec247 | 9 | use crate::ich::{NodeIdHashingMode, StableHashingContext}; |
e1599b0c | 10 | use crate::ty::{self, DefIdTree, TyCtxt}; |
dfeec247 XL |
11 | use rustc_hir as hir; |
12 | use rustc_hir::def_id::DefId; | |
13 | use rustc_hir::Node; | |
1a4d82fc | 14 | |
dfeec247 | 15 | use rustc_data_structures::fx::FxHashMap; |
e74abb32 | 16 | use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; |
532ac7d7 | 17 | use rustc_macros::HashStable; |
dfeec247 | 18 | use rustc_span::{Span, DUMMY_SP}; |
9fa01778 | 19 | |
e1599b0c | 20 | use std::fmt; |
223e47cc | 21 | |
e1599b0c XL |
22 | /// Represents a statically-describable scope that can be used to |
23 | /// bound the lifetime/region for values. | |
1a4d82fc | 24 | /// |
ea8adc8c XL |
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 | |
85aaf69f SL |
27 | /// immediately derivable from the abstract syntax tree structure. |
28 | /// | |
ea8adc8c | 29 | /// `DestructionScope(node_id)` represents the scope of destructors |
85aaf69f SL |
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; | |
ea8adc8c | 33 | /// see discussion with `ScopeTree`. |
85aaf69f | 34 | /// |
b7449926 | 35 | /// `Remainder { block, statement_index }` represents |
ea8adc8c | 36 | /// the scope of user code running immediately after the initializer |
85aaf69f SL |
37 | /// expression for the indexed statement, until the end of the block. |
38 | /// | |
ea8adc8c | 39 | /// So: the following code can be broken down into the scopes beneath: |
ff7c6d11 XL |
40 | /// |
41 | /// ```text | |
85aaf69f | 42 | /// let a = f().g( 'b: { let x = d(); let y = d(); x.h(y) } ) ; |
85aaf69f SL |
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 | /// | |
ea8adc8c XL |
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 | |
85aaf69f | 62 | /// (D6.): DestructionScope for temporaries created during M5. |
ea8adc8c XL |
63 | /// (R7.): Remainder scope for block `'b:`, stmt 0 (let x = ...). |
64 | /// (M8.): Node scope of the `let y = d();` statement. | |
85aaf69f | 65 | /// (D9.): DestructionScope for temporaries created during M8. |
ea8adc8c | 66 | /// (R10.): Remainder scope for block `'b:`, stmt 1 (let y = ...). |
85aaf69f | 67 | /// (D11.): DestructionScope for temporaries and bindings from block `'b:`. |
0731742a | 68 | /// (D12.): DestructionScope for temporaries created during M1 (e.g., f()). |
ff7c6d11 | 69 | /// ``` |
85aaf69f SL |
70 | /// |
71 | /// Note that while the above picture shows the destruction scopes | |
ea8adc8c | 72 | /// as following their corresponding node scopes, in the internal |
85aaf69f SL |
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. | |
9fa01778 XL |
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. | |
dfeec247 XL |
84 | #[derive( |
85 | Clone, | |
86 | PartialEq, | |
87 | PartialOrd, | |
88 | Eq, | |
89 | Ord, | |
90 | Hash, | |
91 | Copy, | |
92 | RustcEncodable, | |
93 | RustcDecodable, | |
94 | HashStable | |
95 | )] | |
ea8adc8c | 96 | pub struct Scope { |
b7449926 XL |
97 | pub id: hir::ItemLocalId, |
98 | pub data: ScopeData, | |
ea8adc8c XL |
99 | } |
100 | ||
b7449926 | 101 | impl fmt::Debug for Scope { |
0bf4aa26 | 102 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
b7449926 XL |
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 | } | |
ea8adc8c | 117 | |
dfeec247 XL |
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 | )] | |
ea8adc8c | 131 | pub enum ScopeData { |
b7449926 | 132 | Node, |
9346a6ac | 133 | |
dc9dc135 XL |
134 | /// Scope of the call-site for a function or closure |
135 | /// (outlives the arguments as well as the body). | |
b7449926 | 136 | CallSite, |
9cc50fc6 | 137 | |
dc9dc135 XL |
138 | /// Scope of arguments passed to a function or closure |
139 | /// (they outlive its body). | |
b7449926 | 140 | Arguments, |
9346a6ac | 141 | |
dc9dc135 | 142 | /// Scope of destructors for temporaries of node-id. |
b7449926 | 143 | Destruction, |
9346a6ac | 144 | |
dc9dc135 | 145 | /// Scope following a `let id = expr;` binding in a block. |
dfeec247 | 146 | Remainder(FirstStatementIndex), |
85aaf69f SL |
147 | } |
148 | ||
e74abb32 | 149 | rustc_index::newtype_index! { |
532ac7d7 XL |
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 `...`. | |
dc9dc135 XL |
167 | pub struct FirstStatementIndex { |
168 | derive [HashStable] | |
169 | } | |
ea8adc8c XL |
170 | } |
171 | ||
b7449926 | 172 | // compilation error if size of `ScopeData` is not the same as a `u32` |
48663c56 | 173 | static_assert_size!(ScopeData, 4); |
ea8adc8c XL |
174 | |
175 | impl Scope { | |
9fa01778 | 176 | /// Returns a item-local ID associated with this scope. |
1a4d82fc | 177 | /// |
0731742a | 178 | /// N.B., likely to be replaced as API is refined; e.g., pnkfelix |
1a4d82fc | 179 | /// anticipates `fn entry_node_id` and `fn each_exit_node_id`. |
ea8adc8c XL |
180 | pub fn item_local_id(&self) -> hir::ItemLocalId { |
181 | self.id | |
182 | } | |
183 | ||
dc9dc135 | 184 | pub fn hir_id(&self, scope_tree: &ScopeTree) -> hir::HirId { |
ea8adc8c | 185 | match scope_tree.root_body { |
dfeec247 XL |
186 | Some(hir_id) => hir::HirId { owner: hir_id.owner, local_id: self.item_local_id() }, |
187 | None => hir::DUMMY_HIR_ID, | |
1a4d82fc JJ |
188 | } |
189 | } | |
85aaf69f | 190 | |
9fa01778 XL |
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 | |
85aaf69f | 193 | /// the AST. |
dc9dc135 XL |
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 { | |
ea8adc8c XL |
197 | return DUMMY_SP; |
198 | } | |
dc9dc135 | 199 | let span = tcx.hir().span(hir_id); |
b7449926 | 200 | if let ScopeData::Remainder(first_statement_index) = self.data { |
dc9dc135 | 201 | if let Node::Block(ref blk) = tcx.hir().get(hir_id) { |
ea8adc8c XL |
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 | ||
b7449926 | 210 | let stmt_span = blk.stmts[first_statement_index.index()].span; |
ea8adc8c XL |
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()); | |
85aaf69f SL |
216 | } |
217 | } | |
dfeec247 XL |
218 | } |
219 | span | |
85aaf69f | 220 | } |
1a4d82fc | 221 | } |
223e47cc | 222 | |
94b46f34 XL |
223 | pub type ScopeDepth = u32; |
224 | ||
ea8adc8c XL |
225 | /// The region scope tree encodes information about region relationships. |
226 | #[derive(Default, Debug)] | |
227 | pub struct ScopeTree { | |
7cac9316 | 228 | /// If not empty, this body is the root of this region hierarchy. |
dfeec247 | 229 | pub root_body: Option<hir::HirId>, |
7cac9316 XL |
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. | |
dfeec247 | 234 | pub root_parent: Option<hir::HirId>, |
7cac9316 | 235 | |
e1599b0c | 236 | /// Maps from a scope ID to the enclosing scope id; |
c34b1796 AL |
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 | |
9fa01778 | 240 | /// enclosing scope ID for the block associated with a closure is |
c34b1796 | 241 | /// the closure itself.) |
dfeec247 | 242 | pub parent_map: FxHashMap<Scope, (Scope, ScopeDepth)>, |
c34b1796 | 243 | |
e1599b0c XL |
244 | /// Maps from a variable or binding ID to the block in which that |
245 | /// variable is declared. | |
ea8adc8c | 246 | var_map: FxHashMap<hir::ItemLocalId, Scope>, |
7cac9316 | 247 | |
e1599b0c | 248 | /// Maps from a `NodeId` to the associated destruction scope (if any). |
ea8adc8c | 249 | destruction_scopes: FxHashMap<hir::ItemLocalId, Scope>, |
c34b1796 | 250 | |
e1599b0c XL |
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 | |
c34b1796 AL |
255 | /// enclosing statement, conditional expression, or repeating |
256 | /// block (see `terminating_scopes`). | |
ea8adc8c XL |
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>>, | |
32a655c1 | 260 | |
c34b1796 AL |
261 | /// Encodes the hierarchy of fn bodies. Every fn body (including |
262 | /// closures) forms its own distinct region hierarchy, rooted in | |
9fa01778 XL |
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 | |
c34b1796 AL |
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" | |
abe05a73 | 269 | /// section of the README in infer::region_constraints for |
c34b1796 | 270 | /// more details. |
ea8adc8c XL |
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: | |
83c7162d XL |
299 | /// ```plain,ignore |
300 | /// `foo` `f` Call#1 `y` Yield `bar` `g` Call#3 Call#2 Call#0 | |
ea8adc8c XL |
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 | /// | |
e1599b0c | 329 | /// This property ought to not on (3) in an essential way -- it |
ea8adc8c XL |
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`. | |
dfeec247 | 342 | pub yield_in_scope: FxHashMap<Scope, YieldData>, |
ea8adc8c XL |
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. | |
dfeec247 | 347 | pub body_expr_count: FxHashMap<hir::BodyId, usize>, |
970d7e83 | 348 | } |
223e47cc | 349 | |
dc9dc135 XL |
350 | #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)] |
351 | pub struct YieldData { | |
e1599b0c | 352 | /// The `Span` of the yield. |
dc9dc135 | 353 | pub span: Span, |
e1599b0c | 354 | /// The number of expressions and patterns appearing before the `yield` in the body plus one. |
dc9dc135 XL |
355 | pub expr_and_pat_count: usize, |
356 | pub source: hir::YieldSource, | |
357 | } | |
358 | ||
ea8adc8c | 359 | impl<'tcx> ScopeTree { |
94b46f34 | 360 | pub fn record_scope_parent(&mut self, child: Scope, parent: Option<(Scope, ScopeDepth)>) { |
7cac9316 XL |
361 | debug!("{:?}.parent = {:?}", child, parent); |
362 | ||
363 | if let Some(p) = parent { | |
ea8adc8c | 364 | let prev = self.parent_map.insert(child, p); |
7cac9316 XL |
365 | assert!(prev.is_none()); |
366 | } | |
367 | ||
e1599b0c | 368 | // Record the destruction scopes for later so we can query them. |
b7449926 XL |
369 | if let ScopeData::Destruction = child.data { |
370 | self.destruction_scopes.insert(child.item_local_id(), child); | |
e9174d1e SL |
371 | } |
372 | } | |
7cac9316 | 373 | |
dfeec247 XL |
374 | pub fn each_encl_scope<E>(&self, mut e: E) |
375 | where | |
376 | E: FnMut(Scope, Scope), | |
377 | { | |
ea8adc8c | 378 | for (&child, &parent) in &self.parent_map { |
94b46f34 | 379 | e(child, parent.0) |
85aaf69f SL |
380 | } |
381 | } | |
7cac9316 | 382 | |
dfeec247 XL |
383 | pub fn each_var_scope<E>(&self, mut e: E) |
384 | where | |
385 | E: FnMut(&hir::ItemLocalId, Scope), | |
386 | { | |
7cac9316 | 387 | for (child, &parent) in self.var_map.iter() { |
85aaf69f SL |
388 | e(child, parent) |
389 | } | |
390 | } | |
32a655c1 | 391 | |
ea8adc8c | 392 | pub fn opt_destruction_scope(&self, n: hir::ItemLocalId) -> Option<Scope> { |
7cac9316 XL |
393 | self.destruction_scopes.get(&n).cloned() |
394 | } | |
395 | ||
e1599b0c | 396 | /// Records that `sub_closure` is defined within `sup_closure`. These IDs |
9fa01778 | 397 | /// should be the ID of the block that is the fn body, which is |
c34b1796 | 398 | /// also the root of the region hierarchy for that fn. |
dfeec247 XL |
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 | ); | |
ea8adc8c XL |
408 | assert!(sub_closure != sup_closure); |
409 | let previous = self.closure_tree.insert(sub_closure, sup_closure); | |
c34b1796 AL |
410 | assert!(previous.is_none()); |
411 | } | |
412 | ||
dfeec247 | 413 | pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) { |
1a4d82fc | 414 | debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime); |
ea8adc8c | 415 | assert!(var != lifetime.item_local_id()); |
7cac9316 | 416 | self.var_map.insert(var, lifetime); |
970d7e83 | 417 | } |
223e47cc | 418 | |
dfeec247 | 419 | pub fn record_rvalue_scope(&mut self, var: hir::ItemLocalId, lifetime: Option<Scope>) { |
1a4d82fc | 420 | debug!("record_rvalue_scope(sub={:?}, sup={:?})", var, lifetime); |
ea8adc8c XL |
421 | if let Some(lifetime) = lifetime { |
422 | assert!(var != lifetime.item_local_id()); | |
423 | } | |
7cac9316 | 424 | self.rvalue_scopes.insert(var, lifetime); |
32a655c1 SL |
425 | } |
426 | ||
e1599b0c | 427 | /// Returns the narrowest scope that encloses `id`, if any. |
ea8adc8c | 428 | pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> { |
94b46f34 | 429 | self.parent_map.get(&id).cloned().map(|(p, _)| p) |
970d7e83 LB |
430 | } |
431 | ||
e1599b0c | 432 | /// Returns the narrowest scope that encloses `id`, if any. |
54a0048b | 433 | #[allow(dead_code)] // used in cfg |
ea8adc8c | 434 | pub fn encl_scope(&self, id: Scope) -> Scope { |
e9174d1e | 435 | self.opt_encl_scope(id).unwrap() |
970d7e83 LB |
436 | } |
437 | ||
1a4d82fc | 438 | /// Returns the lifetime of the local variable `var_id` |
ea8adc8c | 439 | pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Scope { |
dfeec247 XL |
440 | self.var_map |
441 | .get(&var_id) | |
442 | .cloned() | |
443 | .unwrap_or_else(|| bug!("no enclosing scope for id {:?}", var_id)) | |
970d7e83 LB |
444 | } |
445 | ||
e1599b0c | 446 | /// Returns the scope when the temp created by `expr_id` will be cleaned up. |
ea8adc8c | 447 | pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> Option<Scope> { |
e1599b0c | 448 | // Check for a designated rvalue scope. |
7cac9316 | 449 | if let Some(&s) = self.rvalue_scopes.get(&expr_id) { |
c30ab7b3 | 450 | debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s); |
ea8adc8c | 451 | return s; |
970d7e83 | 452 | } |
1a4d82fc | 453 | |
e1599b0c | 454 | // Otherwise, locate the innermost terminating scope |
1a4d82fc JJ |
455 | // if there's one. Static items, for instance, won't |
456 | // have an enclosing scope, hence no scope will be | |
457 | // returned. | |
b7449926 | 458 | let mut id = Scope { id: expr_id, data: ScopeData::Node }; |
1a4d82fc | 459 | |
94b46f34 | 460 | while let Some(&(p, _)) = self.parent_map.get(&id) { |
b7449926 XL |
461 | match p.data { |
462 | ScopeData::Destruction => { | |
dfeec247 | 463 | debug!("temporary_scope({:?}) = {:?} [enclosing]", expr_id, id); |
e9174d1e | 464 | return Some(id); |
1a4d82fc | 465 | } |
dfeec247 | 466 | _ => id = p, |
1a4d82fc JJ |
467 | } |
468 | } | |
e9174d1e SL |
469 | |
470 | debug!("temporary_scope({:?}) = None", expr_id); | |
471 | return None; | |
970d7e83 LB |
472 | } |
473 | ||
e1599b0c | 474 | /// Returns the lifetime of the variable `id`. |
ea8adc8c | 475 | pub fn var_region(&self, id: hir::ItemLocalId) -> ty::RegionKind { |
1a4d82fc JJ |
476 | let scope = ty::ReScope(self.var_scope(id)); |
477 | debug!("var_region({:?}) = {:?}", id, scope); | |
478 | scope | |
970d7e83 LB |
479 | } |
480 | ||
0bf4aa26 | 481 | pub fn scopes_intersect(&self, scope1: Scope, scope2: Scope) -> bool { |
dfeec247 | 482 | self.is_subscope_of(scope1, scope2) || self.is_subscope_of(scope2, scope1) |
970d7e83 LB |
483 | } |
484 | ||
9fa01778 XL |
485 | /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and |
486 | /// `false` otherwise. | |
dfeec247 | 487 | pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool { |
970d7e83 | 488 | let mut s = subscope; |
e9174d1e | 489 | debug!("is_subscope_of({:?}, {:?})", subscope, superscope); |
970d7e83 | 490 | while superscope != s { |
e9174d1e | 491 | match self.opt_encl_scope(s) { |
970d7e83 | 492 | None => { |
dfeec247 | 493 | debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s); |
970d7e83 LB |
494 | return false; |
495 | } | |
dfeec247 | 496 | Some(scope) => s = scope, |
223e47cc LB |
497 | } |
498 | } | |
223e47cc | 499 | |
0bf4aa26 | 500 | debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope); |
970d7e83 LB |
501 | |
502 | return true; | |
503 | } | |
504 | ||
e1599b0c | 505 | /// Returns the ID of the innermost containing body. |
0bf4aa26 | 506 | pub fn containing_body(&self, mut scope: Scope) -> Option<hir::ItemLocalId> { |
2c00a5a8 | 507 | loop { |
b7449926 XL |
508 | if let ScopeData::CallSite = scope.data { |
509 | return Some(scope.item_local_id()); | |
2c00a5a8 XL |
510 | } |
511 | ||
0731742a | 512 | scope = self.opt_encl_scope(scope)?; |
2c00a5a8 XL |
513 | } |
514 | } | |
515 | ||
9fa01778 | 516 | /// Finds the nearest common ancestor of two scopes. That is, finds the |
94b46f34 XL |
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 { | |
dfeec247 XL |
520 | if scope_a == scope_b { |
521 | return scope_a; | |
522 | } | |
970d7e83 | 523 | |
94b46f34 XL |
524 | let mut a = scope_a; |
525 | let mut b = scope_b; | |
83c7162d | 526 | |
94b46f34 XL |
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, | |
83c7162d | 538 | }; |
970d7e83 | 539 | |
94b46f34 XL |
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; | |
970d7e83 | 547 | } |
94b46f34 XL |
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; | |
83c7162d | 553 | } |
94b46f34 XL |
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; | |
970d7e83 | 561 | } |
94b46f34 XL |
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; | |
dfeec247 | 570 | } |
94b46f34 XL |
571 | |
572 | a | |
223e47cc | 573 | } |
7cac9316 | 574 | |
ea8adc8c XL |
575 | /// Assuming that the provided region was defined within this `ScopeTree`, |
576 | /// returns the outermost `Scope` that the region outlives. | |
dc9dc135 | 577 | pub fn early_free_scope(&self, tcx: TyCtxt<'tcx>, br: &ty::EarlyBoundRegion) -> Scope { |
532ac7d7 | 578 | let param_owner = tcx.parent(br.def_id).unwrap(); |
7cac9316 | 579 | |
532ac7d7 | 580 | let param_owner_id = tcx.hir().as_local_hir_id(param_owner).unwrap(); |
dfeec247 XL |
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 \ | |
48663c56 | 594 | region scope tree for {:?} / {:?}", |
dfeec247 XL |
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 | } | |
7cac9316 | 601 | |
dfeec247 XL |
602 | // The trait/impl lifetime is in scope for the method's body. |
603 | self.root_body.unwrap().local_id | |
604 | }); | |
7cac9316 | 605 | |
b7449926 | 606 | Scope { id: scope, data: ScopeData::CallSite } |
7cac9316 XL |
607 | } |
608 | ||
ea8adc8c XL |
609 | /// Assuming that the provided region was defined within this `ScopeTree`, |
610 | /// returns the outermost `Scope` that the region outlives. | |
dc9dc135 | 611 | pub fn free_scope(&self, tcx: TyCtxt<'tcx>, fr: &ty::FreeRegion) -> Scope { |
7cac9316 | 612 | let param_owner = match fr.bound_region { |
dfeec247 XL |
613 | ty::BoundRegion::BrNamed(def_id, _) => tcx.parent(def_id).unwrap(), |
614 | _ => fr.scope, | |
7cac9316 XL |
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 | ||
532ac7d7 | 621 | let param_owner_id = tcx.hir().as_local_hir_id(param_owner).unwrap(); |
0731742a XL |
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 } | |
ea8adc8c XL |
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 | |
2c00a5a8 XL |
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. | |
dc9dc135 | 630 | pub fn yield_in_scope(&self, scope: Scope) -> Option<YieldData> { |
ea8adc8c XL |
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> { | |
74b04a01 | 638 | self.body_expr_count.get(&body_id).copied() |
7cac9316 | 639 | } |
223e47cc LB |
640 | } |
641 | ||
0531ce1d | 642 | impl<'a> HashStable<StableHashingContext<'a>> for ScopeTree { |
e74abb32 | 643 | fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { |
ea8adc8c XL |
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 | } |