]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/middle/region.rs
Rollup merge of #94115 - scottmcm:iter-process-by-ref, r=yaahc
[rust.git] / compiler / rustc_middle / src / 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::ty::TyCtxt;
10 use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
11 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
12 use rustc_hir as hir;
13 use rustc_hir::Node;
14 use rustc_macros::HashStable;
15 use rustc_query_system::ich::{NodeIdHashingMode, StableHashingContext};
16 use rustc_span::{Span, DUMMY_SP};
17
18 use std::fmt;
19
20 /// Represents a statically-describable scope that can be used to
21 /// bound the lifetime/region for values.
22 ///
23 /// `Node(node_id)`: Any AST node that has any scope at all has the
24 /// `Node(node_id)` scope. Other variants represent special cases not
25 /// immediately derivable from the abstract syntax tree structure.
26 ///
27 /// `DestructionScope(node_id)` represents the scope of destructors
28 /// implicitly-attached to `node_id` that run immediately after the
29 /// expression for `node_id` itself. Not every AST node carries a
30 /// `DestructionScope`, but those that are `terminating_scopes` do;
31 /// see discussion with `ScopeTree`.
32 ///
33 /// `Remainder { block, statement_index }` represents
34 /// the scope of user code running immediately after the initializer
35 /// expression for the indexed statement, until the end of the block.
36 ///
37 /// So: the following code can be broken down into the scopes beneath:
38 ///
39 /// ```text
40 /// let a = f().g( 'b: { let x = d(); let y = d(); x.h(y)  }   ) ;
41 ///
42 ///                                                              +-+ (D12.)
43 ///                                                        +-+       (D11.)
44 ///                                              +---------+         (R10.)
45 ///                                              +-+                  (D9.)
46 ///                                   +----------+                    (M8.)
47 ///                                 +----------------------+          (R7.)
48 ///                                 +-+                               (D6.)
49 ///                      +----------+                                 (M5.)
50 ///                    +-----------------------------------+          (M4.)
51 ///         +--------------------------------------------------+      (M3.)
52 ///         +--+                                                      (M2.)
53 /// +-----------------------------------------------------------+     (M1.)
54 ///
55 ///  (M1.): Node scope of the whole `let a = ...;` statement.
56 ///  (M2.): Node scope of the `f()` expression.
57 ///  (M3.): Node scope of the `f().g(..)` expression.
58 ///  (M4.): Node scope of the block labeled `'b:`.
59 ///  (M5.): Node scope of the `let x = d();` statement
60 ///  (D6.): DestructionScope for temporaries created during M5.
61 ///  (R7.): Remainder scope for block `'b:`, stmt 0 (let x = ...).
62 ///  (M8.): Node scope of the `let y = d();` statement.
63 ///  (D9.): DestructionScope for temporaries created during M8.
64 /// (R10.): Remainder scope for block `'b:`, stmt 1 (let y = ...).
65 /// (D11.): DestructionScope for temporaries and bindings from block `'b:`.
66 /// (D12.): DestructionScope for temporaries created during M1 (e.g., f()).
67 /// ```
68 ///
69 /// Note that while the above picture shows the destruction scopes
70 /// as following their corresponding node scopes, in the internal
71 /// data structures of the compiler the destruction scopes are
72 /// represented as enclosing parents. This is sound because we use the
73 /// enclosing parent relationship just to ensure that referenced
74 /// values live long enough; phrased another way, the starting point
75 /// of each range is not really the important thing in the above
76 /// picture, but rather the ending point.
77 //
78 // FIXME(pnkfelix): this currently derives `PartialOrd` and `Ord` to
79 // placate the same deriving in `ty::FreeRegion`, but we may want to
80 // actually attach a more meaningful ordering to scopes than the one
81 // generated via deriving here.
82 #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Copy, TyEncodable, TyDecodable)]
83 #[derive(HashStable)]
84 pub struct Scope {
85     pub id: hir::ItemLocalId,
86     pub data: ScopeData,
87 }
88
89 impl fmt::Debug for Scope {
90     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
91         match self.data {
92             ScopeData::Node => write!(fmt, "Node({:?})", self.id),
93             ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.id),
94             ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.id),
95             ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.id),
96             ScopeData::IfThen => write!(fmt, "IfThen({:?})", 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, TyEncodable, TyDecodable)]
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 of the condition and then block of an if expression
124     /// Used for variables introduced in an if-let expression.
125     IfThen,
126
127     /// Scope following a `let id = expr;` binding in a block.
128     Remainder(FirstStatementIndex),
129 }
130
131 rustc_index::newtype_index! {
132     /// Represents a subscope of `block` for a binding that is introduced
133     /// by `block.stmts[first_statement_index]`. Such subscopes represent
134     /// a suffix of the block. Note that each subscope does not include
135     /// the initializer expression, if any, for the statement indexed by
136     /// `first_statement_index`.
137     ///
138     /// For example, given `{ let (a, b) = EXPR_1; let c = EXPR_2; ... }`:
139     ///
140     /// * The subscope with `first_statement_index == 0` is scope of both
141     ///   `a` and `b`; it does not include EXPR_1, but does include
142     ///   everything after that first `let`. (If you want a scope that
143     ///   includes EXPR_1 as well, then do not use `Scope::Remainder`,
144     ///   but instead another `Scope` that encompasses the whole block,
145     ///   e.g., `Scope::Node`.
146     ///
147     /// * The subscope with `first_statement_index == 1` is scope of `c`,
148     ///   and thus does not include EXPR_2, but covers the `...`.
149     pub struct FirstStatementIndex {
150         derive [HashStable]
151     }
152 }
153
154 // compilation error if size of `ScopeData` is not the same as a `u32`
155 static_assert_size!(ScopeData, 4);
156
157 impl Scope {
158     /// Returns an item-local ID associated with this scope.
159     ///
160     /// N.B., likely to be replaced as API is refined; e.g., pnkfelix
161     /// anticipates `fn entry_node_id` and `fn each_exit_node_id`.
162     pub fn item_local_id(&self) -> hir::ItemLocalId {
163         self.id
164     }
165
166     pub fn hir_id(&self, scope_tree: &ScopeTree) -> Option<hir::HirId> {
167         scope_tree
168             .root_body
169             .map(|hir_id| hir::HirId { owner: hir_id.owner, local_id: self.item_local_id() })
170     }
171
172     /// Returns the span of this `Scope`. Note that in general the
173     /// returned span may not correspond to the span of any `NodeId` in
174     /// the AST.
175     pub fn span(&self, tcx: TyCtxt<'_>, scope_tree: &ScopeTree) -> Span {
176         let Some(hir_id) = self.hir_id(scope_tree) else {
177             return DUMMY_SP;
178         };
179         let span = tcx.hir().span(hir_id);
180         if let ScopeData::Remainder(first_statement_index) = self.data {
181             if let Node::Block(ref blk) = tcx.hir().get(hir_id) {
182                 // Want span for scope starting after the
183                 // indexed statement and ending at end of
184                 // `blk`; reuse span of `blk` and shift `lo`
185                 // forward to end of indexed statement.
186                 //
187                 // (This is the special case alluded to in the
188                 // doc-comment for this method)
189
190                 let stmt_span = blk.stmts[first_statement_index.index()].span;
191
192                 // To avoid issues with macro-generated spans, the span
193                 // of the statement must be nested in that of the block.
194                 if span.lo() <= stmt_span.lo() && stmt_span.lo() <= span.hi() {
195                     return span.with_lo(stmt_span.lo());
196                 }
197             }
198         }
199         span
200     }
201 }
202
203 pub type ScopeDepth = u32;
204
205 /// The region scope tree encodes information about region relationships.
206 #[derive(Default, Debug)]
207 pub struct ScopeTree {
208     /// If not empty, this body is the root of this region hierarchy.
209     pub root_body: Option<hir::HirId>,
210
211     /// Maps from a scope ID to the enclosing scope id;
212     /// this is usually corresponding to the lexical nesting, though
213     /// in the case of closures the parent scope is the innermost
214     /// conditional expression or repeating block. (Note that the
215     /// enclosing scope ID for the block associated with a closure is
216     /// the closure itself.)
217     pub parent_map: FxIndexMap<Scope, (Scope, ScopeDepth)>,
218
219     /// Maps from a variable or binding ID to the block in which that
220     /// variable is declared.
221     var_map: FxIndexMap<hir::ItemLocalId, Scope>,
222
223     /// Maps from a `NodeId` to the associated destruction scope (if any).
224     destruction_scopes: FxIndexMap<hir::ItemLocalId, Scope>,
225
226     /// `rvalue_scopes` includes entries for those expressions whose
227     /// cleanup scope is larger than the default. The map goes from the
228     /// expression ID to the cleanup scope id. For rvalues not present in
229     /// this table, the appropriate cleanup scope is the innermost
230     /// enclosing statement, conditional expression, or repeating
231     /// block (see `terminating_scopes`).
232     /// In constants, None is used to indicate that certain expressions
233     /// escape into 'static and should have no local cleanup scope.
234     rvalue_scopes: FxHashMap<hir::ItemLocalId, Option<Scope>>,
235
236     /// If there are any `yield` nested within a scope, this map
237     /// stores the `Span` of the last one and its index in the
238     /// postorder of the Visitor traversal on the HIR.
239     ///
240     /// HIR Visitor postorder indexes might seem like a peculiar
241     /// thing to care about. but it turns out that HIR bindings
242     /// and the temporary results of HIR expressions are never
243     /// storage-live at the end of HIR nodes with postorder indexes
244     /// lower than theirs, and therefore don't need to be suspended
245     /// at yield-points at these indexes.
246     ///
247     /// For an example, suppose we have some code such as:
248     /// ```rust,ignore (example)
249     ///     foo(f(), yield y, bar(g()))
250     /// ```
251     ///
252     /// With the HIR tree (calls numbered for expository purposes)
253     ///
254     /// ```text
255     ///     Call#0(foo, [Call#1(f), Yield(y), Call#2(bar, Call#3(g))])
256     /// ```
257     ///
258     /// Obviously, the result of `f()` was created before the yield
259     /// (and therefore needs to be kept valid over the yield) while
260     /// the result of `g()` occurs after the yield (and therefore
261     /// doesn't). If we want to infer that, we can look at the
262     /// postorder traversal:
263     /// ```plain,ignore
264     ///     `foo` `f` Call#1 `y` Yield `bar` `g` Call#3 Call#2 Call#0
265     /// ```
266     ///
267     /// In which we can easily see that `Call#1` occurs before the yield,
268     /// and `Call#3` after it.
269     ///
270     /// To see that this method works, consider:
271     ///
272     /// Let `D` be our binding/temporary and `U` be our other HIR node, with
273     /// `HIR-postorder(U) < HIR-postorder(D)`. Suppose, as in our example,
274     /// U is the yield and D is one of the calls.
275     /// Let's show that `D` is storage-dead at `U`.
276     ///
277     /// Remember that storage-live/storage-dead refers to the state of
278     /// the *storage*, and does not consider moves/drop flags.
279     ///
280     /// Then:
281     ///
282     ///   1. From the ordering guarantee of HIR visitors (see
283     ///   `rustc_hir::intravisit`), `D` does not dominate `U`.
284     ///
285     ///   2. Therefore, `D` is *potentially* storage-dead at `U` (because
286     ///   we might visit `U` without ever getting to `D`).
287     ///
288     ///   3. However, we guarantee that at each HIR point, each
289     ///   binding/temporary is always either always storage-live
290     ///   or always storage-dead. This is what is being guaranteed
291     ///   by `terminating_scopes` including all blocks where the
292     ///   count of executions is not guaranteed.
293     ///
294     ///   4. By `2.` and `3.`, `D` is *statically* storage-dead at `U`,
295     ///   QED.
296     ///
297     /// This property ought to not on (3) in an essential way -- it
298     /// is probably still correct even if we have "unrestricted" terminating
299     /// scopes. However, why use the complicated proof when a simple one
300     /// works?
301     ///
302     /// A subtle thing: `box` expressions, such as `box (&x, yield 2, &y)`. It
303     /// might seem that a `box` expression creates a `Box<T>` temporary
304     /// when it *starts* executing, at `HIR-preorder(BOX-EXPR)`. That might
305     /// be true in the MIR desugaring, but it is not important in the semantics.
306     ///
307     /// The reason is that semantically, until the `box` expression returns,
308     /// the values are still owned by their containing expressions. So
309     /// we'll see that `&x`.
310     pub yield_in_scope: FxHashMap<Scope, Vec<YieldData>>,
311
312     /// The number of visit_expr and visit_pat calls done in the body.
313     /// Used to sanity check visit_expr/visit_pat call count when
314     /// calculating generator interiors.
315     pub body_expr_count: FxHashMap<hir::BodyId, usize>,
316 }
317
318 #[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
319 pub struct YieldData {
320     /// The `Span` of the yield.
321     pub span: Span,
322     /// The number of expressions and patterns appearing before the `yield` in the body, plus one.
323     pub expr_and_pat_count: usize,
324     pub source: hir::YieldSource,
325 }
326
327 impl ScopeTree {
328     pub fn record_scope_parent(&mut self, child: Scope, parent: Option<(Scope, ScopeDepth)>) {
329         debug!("{:?}.parent = {:?}", child, parent);
330
331         if let Some(p) = parent {
332             let prev = self.parent_map.insert(child, p);
333             assert!(prev.is_none());
334         }
335
336         // Record the destruction scopes for later so we can query them.
337         if let ScopeData::Destruction = child.data {
338             self.destruction_scopes.insert(child.item_local_id(), child);
339         }
340     }
341
342     pub fn opt_destruction_scope(&self, n: hir::ItemLocalId) -> Option<Scope> {
343         self.destruction_scopes.get(&n).cloned()
344     }
345
346     pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
347         debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
348         assert!(var != lifetime.item_local_id());
349         self.var_map.insert(var, lifetime);
350     }
351
352     pub fn record_rvalue_scope(&mut self, var: hir::ItemLocalId, lifetime: Option<Scope>) {
353         debug!("record_rvalue_scope(sub={:?}, sup={:?})", var, lifetime);
354         if let Some(lifetime) = lifetime {
355             assert!(var != lifetime.item_local_id());
356         }
357         self.rvalue_scopes.insert(var, lifetime);
358     }
359
360     /// Returns the narrowest scope that encloses `id`, if any.
361     pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
362         self.parent_map.get(&id).cloned().map(|(p, _)| p)
363     }
364
365     /// Returns the lifetime of the local variable `var_id`
366     pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Scope {
367         self.var_map
368             .get(&var_id)
369             .cloned()
370             .unwrap_or_else(|| bug!("no enclosing scope for id {:?}", var_id))
371     }
372
373     /// Returns the scope when the temp created by `expr_id` will be cleaned up.
374     pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> Option<Scope> {
375         // Check for a designated rvalue scope.
376         if let Some(&s) = self.rvalue_scopes.get(&expr_id) {
377             debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s);
378             return s;
379         }
380
381         // Otherwise, locate the innermost terminating scope
382         // if there's one. Static items, for instance, won't
383         // have an enclosing scope, hence no scope will be
384         // returned.
385         let mut id = Scope { id: expr_id, data: ScopeData::Node };
386
387         while let Some(&(p, _)) = self.parent_map.get(&id) {
388             match p.data {
389                 ScopeData::Destruction => {
390                     debug!("temporary_scope({:?}) = {:?} [enclosing]", expr_id, id);
391                     return Some(id);
392                 }
393                 _ => id = p,
394             }
395         }
396
397         debug!("temporary_scope({:?}) = None", expr_id);
398         None
399     }
400
401     /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and
402     /// `false` otherwise.
403     ///
404     /// Used by clippy.
405     pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool {
406         let mut s = subscope;
407         debug!("is_subscope_of({:?}, {:?})", subscope, superscope);
408         while superscope != s {
409             match self.opt_encl_scope(s) {
410                 None => {
411                     debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
412                     return false;
413                 }
414                 Some(scope) => s = scope,
415             }
416         }
417
418         debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
419
420         true
421     }
422
423     /// Checks whether the given scope contains a `yield`. If so,
424     /// returns `Some(YieldData)`. If not, returns `None`.
425     pub fn yield_in_scope(&self, scope: Scope) -> Option<&Vec<YieldData>> {
426         self.yield_in_scope.get(&scope)
427     }
428
429     /// Gives the number of expressions visited in a body.
430     /// Used to sanity check visit_expr call count when
431     /// calculating generator interiors.
432     pub fn body_expr_count(&self, body_id: hir::BodyId) -> Option<usize> {
433         self.body_expr_count.get(&body_id).copied()
434     }
435 }
436
437 impl<'a> HashStable<StableHashingContext<'a>> for ScopeTree {
438     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
439         let ScopeTree {
440             root_body,
441             ref body_expr_count,
442             ref parent_map,
443             ref var_map,
444             ref destruction_scopes,
445             ref rvalue_scopes,
446             ref yield_in_scope,
447         } = *self;
448
449         hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
450             root_body.hash_stable(hcx, hasher)
451         });
452
453         body_expr_count.hash_stable(hcx, hasher);
454         parent_map.hash_stable(hcx, hasher);
455         var_map.hash_stable(hcx, hasher);
456         destruction_scopes.hash_stable(hcx, hasher);
457         rvalue_scopes.hash_stable(hcx, hasher);
458         yield_in_scope.hash_stable(hcx, hasher);
459     }
460 }