rustc_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
9use std::fmt;
10use std::ops::Deref;
11
12use rustc_data_structures::fx::FxIndexMap;
13use rustc_data_structures::unord::UnordMap;
14use rustc_hir as hir;
15use rustc_hir::{HirId, HirIdMap, Node};
16use rustc_macros::{HashStable, TyDecodable, TyEncodable};
17use rustc_span::{DUMMY_SP, Span};
18use tracing::debug;
19
20use crate::ty::TyCtxt;
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::LateParamRegion`, but we may want to
82// actually attach a more meaningful ordering to scopes than the one
83// generated via deriving here.
84#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Copy, TyEncodable, TyDecodable)]
85#[derive(HashStable)]
86pub struct Scope {
87 pub local_id: hir::ItemLocalId,
88 pub data: ScopeData,
89}
90
91impl fmt::Debug for Scope {
92 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
93 match self.data {
94 ScopeData::Node => write!(fmt, "Node({:?})", self.local_id),
95 ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.local_id),
96 ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.local_id),
97 ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.local_id),
98 ScopeData::IfThen => write!(fmt, "IfThen({:?})", self.local_id),
99 ScopeData::IfThenRescope => write!(fmt, "IfThen[edition2024]({:?})", self.local_id),
100 ScopeData::Remainder(fsi) => write!(
101 fmt,
102 "Remainder {{ block: {:?}, first_statement_index: {}}}",
103 self.local_id,
104 fsi.as_u32(),
105 ),
106 }
107 }
108}
109
110#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, TyEncodable, TyDecodable)]
111#[derive(HashStable)]
112pub enum ScopeData {
113 Node,
114
115 /// Scope of the call-site for a function or closure
116 /// (outlives the arguments as well as the body).
117 CallSite,
118
119 /// Scope of arguments passed to a function or closure
120 /// (they outlive its body).
121 Arguments,
122
123 /// Scope of destructors for temporaries of node-id.
124 Destruction,
125
126 /// Scope of the condition and then block of an if expression
127 /// Used for variables introduced in an if-let expression.
128 IfThen,
129
130 /// Scope of the condition and then block of an if expression
131 /// Used for variables introduced in an if-let expression,
132 /// whose lifetimes do not cross beyond this scope.
133 IfThenRescope,
134
135 /// Scope following a `let id = expr;` binding in a block.
136 Remainder(FirstStatementIndex),
137}
138
139rustc_index::newtype_index! {
140 /// Represents a subscope of `block` for a binding that is introduced
141 /// by `block.stmts[first_statement_index]`. Such subscopes represent
142 /// a suffix of the block. Note that each subscope does not include
143 /// the initializer expression, if any, for the statement indexed by
144 /// `first_statement_index`.
145 ///
146 /// For example, given `{ let (a, b) = EXPR_1; let c = EXPR_2; ... }`:
147 ///
148 /// * The subscope with `first_statement_index == 0` is scope of both
149 /// `a` and `b`; it does not include EXPR_1, but does include
150 /// everything after that first `let`. (If you want a scope that
151 /// includes EXPR_1 as well, then do not use `Scope::Remainder`,
152 /// but instead another `Scope` that encompasses the whole block,
153 /// e.g., `Scope::Node`.
154 ///
155 /// * The subscope with `first_statement_index == 1` is scope of `c`,
156 /// and thus does not include EXPR_2, but covers the `...`.
157 #[derive(HashStable)]
158 #[encodable]
159 #[orderable]
160 pub struct FirstStatementIndex {}
161}
162
163// compilation error if size of `ScopeData` is not the same as a `u32`
164rustc_data_structures::static_assert_size!(ScopeData, 4);
165
166impl Scope {
167 pub fn hir_id(&self, scope_tree: &ScopeTree) -> Option<HirId> {
168 scope_tree.root_body.map(|hir_id| HirId { owner: hir_id.owner, local_id: self.local_id })
169 }
170
171 /// Returns the span of this `Scope`. Note that in general the
172 /// returned span may not correspond to the span of any `NodeId` in
173 /// the AST.
174 pub fn span(&self, tcx: TyCtxt<'_>, scope_tree: &ScopeTree) -> Span {
175 let Some(hir_id) = self.hir_id(scope_tree) else {
176 return DUMMY_SP;
177 };
178 let span = tcx.hir().span(hir_id);
179 if let ScopeData::Remainder(first_statement_index) = self.data {
180 if let Node::Block(blk) = tcx.hir_node(hir_id) {
181 // Want span for scope starting after the
182 // indexed statement and ending at end of
183 // `blk`; reuse span of `blk` and shift `lo`
184 // forward to end of indexed statement.
185 //
186 // (This is the special case alluded to in the
187 // doc-comment for this method)
188
189 let stmt_span = blk.stmts[first_statement_index.index()].span;
190
191 // To avoid issues with macro-generated spans, the span
192 // of the statement must be nested in that of the block.
193 if span.lo() <= stmt_span.lo() && stmt_span.lo() <= span.hi() {
194 return span.with_lo(stmt_span.lo());
195 }
196 }
197 }
198 span
199 }
200}
201
202/// The region scope tree encodes information about region relationships.
203#[derive(Default, Debug, HashStable)]
204pub struct ScopeTree {
205 /// If not empty, this body is the root of this region hierarchy.
206 pub root_body: Option<HirId>,
207
208 /// Maps from a scope ID to the enclosing scope id;
209 /// this is usually corresponding to the lexical nesting, though
210 /// in the case of closures the parent scope is the innermost
211 /// conditional expression or repeating block. (Note that the
212 /// enclosing scope ID for the block associated with a closure is
213 /// the closure itself.)
214 pub parent_map: FxIndexMap<Scope, Scope>,
215
216 /// Maps from a variable or binding ID to the block in which that
217 /// variable is declared.
218 var_map: FxIndexMap<hir::ItemLocalId, Scope>,
219
220 /// Identifies expressions which, if captured into a temporary, ought to
221 /// have a temporary whose lifetime extends to the end of the enclosing *block*,
222 /// and not the enclosing *statement*. Expressions that are not present in this
223 /// table are not rvalue candidates. The set of rvalue candidates is computed
224 /// during type check based on a traversal of the AST.
225 pub rvalue_candidates: HirIdMap<RvalueCandidate>,
226
227 /// Backwards incompatible scoping that will be introduced in future editions.
228 /// This information is used later for linting to identify locals and
229 /// temporary values that will receive backwards-incompatible drop orders.
230 pub backwards_incompatible_scope: UnordMap<hir::ItemLocalId, Scope>,
231
232 /// If there are any `yield` nested within a scope, this map
233 /// stores the `Span` of the last one and its index in the
234 /// postorder of the Visitor traversal on the HIR.
235 ///
236 /// HIR Visitor postorder indexes might seem like a peculiar
237 /// thing to care about. but it turns out that HIR bindings
238 /// and the temporary results of HIR expressions are never
239 /// storage-live at the end of HIR nodes with postorder indexes
240 /// lower than theirs, and therefore don't need to be suspended
241 /// at yield-points at these indexes.
242 ///
243 /// For an example, suppose we have some code such as:
244 /// ```rust,ignore (example)
245 /// foo(f(), yield y, bar(g()))
246 /// ```
247 ///
248 /// With the HIR tree (calls numbered for expository purposes)
249 ///
250 /// ```text
251 /// Call#0(foo, [Call#1(f), Yield(y), Call#2(bar, Call#3(g))])
252 /// ```
253 ///
254 /// Obviously, the result of `f()` was created before the yield
255 /// (and therefore needs to be kept valid over the yield) while
256 /// the result of `g()` occurs after the yield (and therefore
257 /// doesn't). If we want to infer that, we can look at the
258 /// postorder traversal:
259 /// ```plain,ignore
260 /// `foo` `f` Call#1 `y` Yield `bar` `g` Call#3 Call#2 Call#0
261 /// ```
262 ///
263 /// In which we can easily see that `Call#1` occurs before the yield,
264 /// and `Call#3` after it.
265 ///
266 /// To see that this method works, consider:
267 ///
268 /// Let `D` be our binding/temporary and `U` be our other HIR node, with
269 /// `HIR-postorder(U) < HIR-postorder(D)`. Suppose, as in our example,
270 /// U is the yield and D is one of the calls.
271 /// Let's show that `D` is storage-dead at `U`.
272 ///
273 /// Remember that storage-live/storage-dead refers to the state of
274 /// the *storage*, and does not consider moves/drop flags.
275 ///
276 /// Then:
277 ///
278 /// 1. From the ordering guarantee of HIR visitors (see
279 /// `rustc_hir::intravisit`), `D` does not dominate `U`.
280 ///
281 /// 2. Therefore, `D` is *potentially* storage-dead at `U` (because
282 /// we might visit `U` without ever getting to `D`).
283 ///
284 /// 3. However, we guarantee that at each HIR point, each
285 /// binding/temporary is always either always storage-live
286 /// or always storage-dead. This is what is being guaranteed
287 /// by `terminating_scopes` including all blocks where the
288 /// count of executions is not guaranteed.
289 ///
290 /// 4. By `2.` and `3.`, `D` is *statically* storage-dead at `U`,
291 /// QED.
292 ///
293 /// This property ought to not on (3) in an essential way -- it
294 /// is probably still correct even if we have "unrestricted" terminating
295 /// scopes. However, why use the complicated proof when a simple one
296 /// works?
297 ///
298 /// A subtle thing: `box` expressions, such as `box (&x, yield 2, &y)`. It
299 /// might seem that a `box` expression creates a `Box<T>` temporary
300 /// when it *starts* executing, at `HIR-preorder(BOX-EXPR)`. That might
301 /// be true in the MIR desugaring, but it is not important in the semantics.
302 ///
303 /// The reason is that semantically, until the `box` expression returns,
304 /// the values are still owned by their containing expressions. So
305 /// we'll see that `&x`.
306 pub yield_in_scope: UnordMap<Scope, Vec<YieldData>>,
307}
308
309/// See the `rvalue_candidates` field for more information on rvalue
310/// candidates in general.
311/// The `lifetime` field is None to indicate that certain expressions escape
312/// into 'static and should have no local cleanup scope.
313#[derive(Debug, Copy, Clone, HashStable)]
314pub struct RvalueCandidate {
315 pub target: hir::ItemLocalId,
316 pub lifetime: Option<Scope>,
317}
318
319#[derive(Debug, Copy, Clone, HashStable)]
320pub struct YieldData {
321 /// The `Span` of the yield.
322 pub span: Span,
323 /// The number of expressions and patterns appearing before the `yield` in the body, plus one.
324 pub expr_and_pat_count: usize,
325 pub source: hir::YieldSource,
326}
327
328impl ScopeTree {
329 pub fn record_scope_parent(&mut self, child: Scope, parent: Option<Scope>) {
330 debug!("{:?}.parent = {:?}", child, parent);
331
332 if let Some(p) = parent {
333 let prev = self.parent_map.insert(child, p);
334 assert!(prev.is_none());
335 }
336 }
337
338 pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
339 debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
340 assert!(var != lifetime.local_id);
341 self.var_map.insert(var, lifetime);
342 }
343
344 pub fn record_rvalue_candidate(&mut self, var: HirId, candidate: RvalueCandidate) {
345 debug!("record_rvalue_candidate(var={var:?}, candidate={candidate:?})");
346 if let Some(lifetime) = &candidate.lifetime {
347 assert!(var.local_id != lifetime.local_id)
348 }
349 self.rvalue_candidates.insert(var, candidate);
350 }
351
352 /// Returns the narrowest scope that encloses `id`, if any.
353 pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
354 self.parent_map.get(&id).cloned()
355 }
356
357 /// Returns the lifetime of the local variable `var_id`, if any.
358 pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Option<Scope> {
359 self.var_map.get(&var_id).cloned()
360 }
361
362 /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and
363 /// `false` otherwise.
364 ///
365 /// Used by clippy.
366 pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool {
367 let mut s = subscope;
368 debug!("is_subscope_of({:?}, {:?})", subscope, superscope);
369 while superscope != s {
370 match self.opt_encl_scope(s) {
371 None => {
372 debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
373 return false;
374 }
375 Some(scope) => s = scope,
376 }
377 }
378
379 debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
380
381 true
382 }
383
384 /// Checks whether the given scope contains a `yield`. If so,
385 /// returns `Some(YieldData)`. If not, returns `None`.
386 pub fn yield_in_scope(&self, scope: Scope) -> Option<&[YieldData]> {
387 self.yield_in_scope.get(&scope).map(Deref::deref)
388 }
389}