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
202pub type ScopeDepth = u32;
203
204/// The region scope tree encodes information about region relationships.
205#[derive(Default, Debug, HashStable)]
206pub struct ScopeTree {
207 /// If not empty, this body is the root of this region hierarchy.
208 pub root_body: Option<HirId>,
209
210 /// Maps from a scope ID to the enclosing scope id;
211 /// this is usually corresponding to the lexical nesting, though
212 /// in the case of closures the parent scope is the innermost
213 /// conditional expression or repeating block. (Note that the
214 /// enclosing scope ID for the block associated with a closure is
215 /// the closure itself.)
216 pub parent_map: FxIndexMap<Scope, (Scope, ScopeDepth)>,
217
218 /// Maps from a variable or binding ID to the block in which that
219 /// variable is declared.
220 var_map: FxIndexMap<hir::ItemLocalId, Scope>,
221
222 /// Identifies expressions which, if captured into a temporary, ought to
223 /// have a temporary whose lifetime extends to the end of the enclosing *block*,
224 /// and not the enclosing *statement*. Expressions that are not present in this
225 /// table are not rvalue candidates. The set of rvalue candidates is computed
226 /// during type check based on a traversal of the AST.
227 pub rvalue_candidates: HirIdMap<RvalueCandidateType>,
228
229 /// Backwards incompatible scoping that will be introduced in future editions.
230 /// This information is used later for linting to identify locals and
231 /// temporary values that will receive backwards-incompatible drop orders.
232 pub backwards_incompatible_scope: UnordMap<hir::ItemLocalId, Scope>,
233
234 /// If there are any `yield` nested within a scope, this map
235 /// stores the `Span` of the last one and its index in the
236 /// postorder of the Visitor traversal on the HIR.
237 ///
238 /// HIR Visitor postorder indexes might seem like a peculiar
239 /// thing to care about. but it turns out that HIR bindings
240 /// and the temporary results of HIR expressions are never
241 /// storage-live at the end of HIR nodes with postorder indexes
242 /// lower than theirs, and therefore don't need to be suspended
243 /// at yield-points at these indexes.
244 ///
245 /// For an example, suppose we have some code such as:
246 /// ```rust,ignore (example)
247 /// foo(f(), yield y, bar(g()))
248 /// ```
249 ///
250 /// With the HIR tree (calls numbered for expository purposes)
251 ///
252 /// ```text
253 /// Call#0(foo, [Call#1(f), Yield(y), Call#2(bar, Call#3(g))])
254 /// ```
255 ///
256 /// Obviously, the result of `f()` was created before the yield
257 /// (and therefore needs to be kept valid over the yield) while
258 /// the result of `g()` occurs after the yield (and therefore
259 /// doesn't). If we want to infer that, we can look at the
260 /// postorder traversal:
261 /// ```plain,ignore
262 /// `foo` `f` Call#1 `y` Yield `bar` `g` Call#3 Call#2 Call#0
263 /// ```
264 ///
265 /// In which we can easily see that `Call#1` occurs before the yield,
266 /// and `Call#3` after it.
267 ///
268 /// To see that this method works, consider:
269 ///
270 /// Let `D` be our binding/temporary and `U` be our other HIR node, with
271 /// `HIR-postorder(U) < HIR-postorder(D)`. Suppose, as in our example,
272 /// U is the yield and D is one of the calls.
273 /// Let's show that `D` is storage-dead at `U`.
274 ///
275 /// Remember that storage-live/storage-dead refers to the state of
276 /// the *storage*, and does not consider moves/drop flags.
277 ///
278 /// Then:
279 ///
280 /// 1. From the ordering guarantee of HIR visitors (see
281 /// `rustc_hir::intravisit`), `D` does not dominate `U`.
282 ///
283 /// 2. Therefore, `D` is *potentially* storage-dead at `U` (because
284 /// we might visit `U` without ever getting to `D`).
285 ///
286 /// 3. However, we guarantee that at each HIR point, each
287 /// binding/temporary is always either always storage-live
288 /// or always storage-dead. This is what is being guaranteed
289 /// by `terminating_scopes` including all blocks where the
290 /// count of executions is not guaranteed.
291 ///
292 /// 4. By `2.` and `3.`, `D` is *statically* storage-dead at `U`,
293 /// QED.
294 ///
295 /// This property ought to not on (3) in an essential way -- it
296 /// is probably still correct even if we have "unrestricted" terminating
297 /// scopes. However, why use the complicated proof when a simple one
298 /// works?
299 ///
300 /// A subtle thing: `box` expressions, such as `box (&x, yield 2, &y)`. It
301 /// might seem that a `box` expression creates a `Box<T>` temporary
302 /// when it *starts* executing, at `HIR-preorder(BOX-EXPR)`. That might
303 /// be true in the MIR desugaring, but it is not important in the semantics.
304 ///
305 /// The reason is that semantically, until the `box` expression returns,
306 /// the values are still owned by their containing expressions. So
307 /// we'll see that `&x`.
308 pub yield_in_scope: UnordMap<Scope, Vec<YieldData>>,
309}
310
311/// Identifies the reason that a given expression is an rvalue candidate
312/// (see the `rvalue_candidates` field for more information what rvalue
313/// candidates in general). In constants, the `lifetime` field is None
314/// to indicate that certain expressions escape into 'static and
315/// should have no local cleanup scope.
316#[derive(Debug, Copy, Clone, HashStable)]
317pub enum RvalueCandidateType {
318 Borrow { target: hir::ItemLocalId, lifetime: Option<Scope> },
319 Pattern { target: hir::ItemLocalId, lifetime: Option<Scope> },
320}
321
322#[derive(Debug, Copy, Clone, HashStable)]
323pub struct YieldData {
324 /// The `Span` of the yield.
325 pub span: Span,
326 /// The number of expressions and patterns appearing before the `yield` in the body, plus one.
327 pub expr_and_pat_count: usize,
328 pub source: hir::YieldSource,
329}
330
331impl ScopeTree {
332 pub fn record_scope_parent(&mut self, child: Scope, parent: Option<(Scope, ScopeDepth)>) {
333 debug!("{:?}.parent = {:?}", child, parent);
334
335 if let Some(p) = parent {
336 let prev = self.parent_map.insert(child, p);
337 assert!(prev.is_none());
338 }
339 }
340
341 pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
342 debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
343 assert!(var != lifetime.local_id);
344 self.var_map.insert(var, lifetime);
345 }
346
347 pub fn record_rvalue_candidate(&mut self, var: HirId, candidate_type: RvalueCandidateType) {
348 debug!("record_rvalue_candidate(var={var:?}, type={candidate_type:?})");
349 match &candidate_type {
350 RvalueCandidateType::Borrow { lifetime: Some(lifetime), .. }
351 | RvalueCandidateType::Pattern { lifetime: Some(lifetime), .. } => {
352 assert!(var.local_id != lifetime.local_id)
353 }
354 _ => {}
355 }
356 self.rvalue_candidates.insert(var, candidate_type);
357 }
358
359 /// Returns the narrowest scope that encloses `id`, if any.
360 pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
361 self.parent_map.get(&id).cloned().map(|(p, _)| p)
362 }
363
364 /// Returns the lifetime of the local variable `var_id`, if any.
365 pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Option<Scope> {
366 self.var_map.get(&var_id).cloned()
367 }
368
369 /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and
370 /// `false` otherwise.
371 ///
372 /// Used by clippy.
373 pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool {
374 let mut s = subscope;
375 debug!("is_subscope_of({:?}, {:?})", subscope, superscope);
376 while superscope != s {
377 match self.opt_encl_scope(s) {
378 None => {
379 debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
380 return false;
381 }
382 Some(scope) => s = scope,
383 }
384 }
385
386 debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
387
388 true
389 }
390
391 /// Checks whether the given scope contains a `yield`. If so,
392 /// returns `Some(YieldData)`. If not, returns `None`.
393 pub fn yield_in_scope(&self, scope: Scope) -> Option<&[YieldData]> {
394 self.yield_in_scope.get(&scope).map(Deref::deref)
395 }
396}