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;
10
11use rustc_data_structures::fx::FxIndexMap;
12use rustc_data_structures::unord::UnordMap;
13use rustc_hir as hir;
14use rustc_hir::{HirId, HirIdMap, Node};
15use rustc_macros::{HashStable, TyDecodable, TyEncodable};
16use rustc_span::{DUMMY_SP, Span};
17use tracing::debug;
18
19use crate::ty::TyCtxt;
20
21/// Represents a statically-describable scope that can be used to
22/// bound the lifetime/region for values.
23///
24/// `Node(node_id)`: Any AST node that has any scope at all has the
25/// `Node(node_id)` scope. Other variants represent special cases not
26/// immediately derivable from the abstract syntax tree structure.
27///
28/// `DestructionScope(node_id)` represents the scope of destructors
29/// implicitly-attached to `node_id` that run immediately after the
30/// expression for `node_id` itself. Not every AST node carries a
31/// `DestructionScope`, but those that are `terminating_scopes` do;
32/// see discussion with `ScopeTree`.
33///
34/// `Remainder { block, statement_index }` represents
35/// the scope of user code running immediately after the initializer
36/// expression for the indexed statement, until the end of the block.
37///
38/// So: the following code can be broken down into the scopes beneath:
39///
40/// ```text
41/// let a = f().g( 'b: { let x = d(); let y = d(); x.h(y)  }   ) ;
42///
43///                                                              +-+ (D12.)
44///                                                        +-+       (D11.)
45///                                              +---------+         (R10.)
46///                                              +-+                  (D9.)
47///                                   +----------+                    (M8.)
48///                                 +----------------------+          (R7.)
49///                                 +-+                               (D6.)
50///                      +----------+                                 (M5.)
51///                    +-----------------------------------+          (M4.)
52///         +--------------------------------------------------+      (M3.)
53///         +--+                                                      (M2.)
54/// +-----------------------------------------------------------+     (M1.)
55///
56///  (M1.): Node scope of the whole `let a = ...;` statement.
57///  (M2.): Node scope of the `f()` expression.
58///  (M3.): Node scope of the `f().g(..)` expression.
59///  (M4.): Node scope of the block labeled `'b:`.
60///  (M5.): Node scope of the `let x = d();` statement
61///  (D6.): DestructionScope for temporaries created during M5.
62///  (R7.): Remainder scope for block `'b:`, stmt 0 (let x = ...).
63///  (M8.): Node scope of the `let y = d();` statement.
64///  (D9.): DestructionScope for temporaries created during M8.
65/// (R10.): Remainder scope for block `'b:`, stmt 1 (let y = ...).
66/// (D11.): DestructionScope for temporaries and bindings from block `'b:`.
67/// (D12.): DestructionScope for temporaries created during M1 (e.g., f()).
68/// ```
69///
70/// Note that while the above picture shows the destruction scopes
71/// as following their corresponding node scopes, in the internal
72/// data structures of the compiler the destruction scopes are
73/// represented as enclosing parents. This is sound because we use the
74/// enclosing parent relationship just to ensure that referenced
75/// values live long enough; phrased another way, the starting point
76/// of each range is not really the important thing in the above
77/// picture, but rather the ending point.
78//
79// FIXME(pnkfelix): this currently derives `PartialOrd` and `Ord` to
80// placate the same deriving in `ty::LateParamRegion`, but we may want to
81// actually attach a more meaningful ordering to scopes than the one
82// generated via deriving here.
83#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Copy, TyEncodable, TyDecodable)]
84#[derive(HashStable)]
85pub struct Scope {
86    pub local_id: hir::ItemLocalId,
87    pub data: ScopeData,
88}
89
90impl fmt::Debug for Scope {
91    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
92        match self.data {
93            ScopeData::Node => write!(fmt, "Node({:?})", self.local_id),
94            ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.local_id),
95            ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.local_id),
96            ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.local_id),
97            ScopeData::IfThen => write!(fmt, "IfThen({:?})", self.local_id),
98            ScopeData::IfThenRescope => write!(fmt, "IfThen[edition2024]({:?})", self.local_id),
99            ScopeData::Remainder(fsi) => write!(
100                fmt,
101                "Remainder {{ block: {:?}, first_statement_index: {}}}",
102                self.local_id,
103                fsi.as_u32(),
104            ),
105        }
106    }
107}
108
109#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, TyEncodable, TyDecodable)]
110#[derive(HashStable)]
111pub enum ScopeData {
112    Node,
113
114    /// Scope of the call-site for a function or closure
115    /// (outlives the arguments as well as the body).
116    CallSite,
117
118    /// Scope of arguments passed to a function or closure
119    /// (they outlive its body).
120    Arguments,
121
122    /// Scope of destructors for temporaries of node-id.
123    Destruction,
124
125    /// Scope of the condition and then block of an if expression
126    /// Used for variables introduced in an if-let expression.
127    IfThen,
128
129    /// Scope of the condition and then block of an if expression
130    /// Used for variables introduced in an if-let expression,
131    /// whose lifetimes do not cross beyond this scope.
132    IfThenRescope,
133
134    /// Scope following a `let id = expr;` binding in a block.
135    Remainder(FirstStatementIndex),
136}
137
138rustc_index::newtype_index! {
139    /// Represents a subscope of `block` for a binding that is introduced
140    /// by `block.stmts[first_statement_index]`. Such subscopes represent
141    /// a suffix of the block. Note that each subscope does not include
142    /// the initializer expression, if any, for the statement indexed by
143    /// `first_statement_index`.
144    ///
145    /// For example, given `{ let (a, b) = EXPR_1; let c = EXPR_2; ... }`:
146    ///
147    /// * The subscope with `first_statement_index == 0` is scope of both
148    ///   `a` and `b`; it does not include EXPR_1, but does include
149    ///   everything after that first `let`. (If you want a scope that
150    ///   includes EXPR_1 as well, then do not use `Scope::Remainder`,
151    ///   but instead another `Scope` that encompasses the whole block,
152    ///   e.g., `Scope::Node`.
153    ///
154    /// * The subscope with `first_statement_index == 1` is scope of `c`,
155    ///   and thus does not include EXPR_2, but covers the `...`.
156    #[derive(HashStable)]
157    #[encodable]
158    #[orderable]
159    pub struct FirstStatementIndex {}
160}
161
162// compilation error if size of `ScopeData` is not the same as a `u32`
163rustc_data_structures::static_assert_size!(ScopeData, 4);
164
165impl Scope {
166    pub fn hir_id(&self, scope_tree: &ScopeTree) -> Option<HirId> {
167        scope_tree.root_body.map(|hir_id| HirId { owner: hir_id.owner, local_id: self.local_id })
168    }
169
170    /// Returns the span of this `Scope`. Note that in general the
171    /// returned span may not correspond to the span of any `NodeId` in
172    /// the AST.
173    pub fn span(&self, tcx: TyCtxt<'_>, scope_tree: &ScopeTree) -> Span {
174        let Some(hir_id) = self.hir_id(scope_tree) else {
175            return DUMMY_SP;
176        };
177        let span = tcx.hir_span(hir_id);
178        if let ScopeData::Remainder(first_statement_index) = self.data {
179            if let Node::Block(blk) = tcx.hir_node(hir_id) {
180                // Want span for scope starting after the
181                // indexed statement and ending at end of
182                // `blk`; reuse span of `blk` and shift `lo`
183                // forward to end of indexed statement.
184                //
185                // (This is the special case alluded to in the
186                // doc-comment for this method)
187
188                let stmt_span = blk.stmts[first_statement_index.index()].span;
189
190                // To avoid issues with macro-generated spans, the span
191                // of the statement must be nested in that of the block.
192                if span.lo() <= stmt_span.lo() && stmt_span.lo() <= span.hi() {
193                    return span.with_lo(stmt_span.lo());
194                }
195            }
196        }
197        span
198    }
199}
200
201/// The region scope tree encodes information about region relationships.
202#[derive(Default, Debug, HashStable)]
203pub struct ScopeTree {
204    /// If not empty, this body is the root of this region hierarchy.
205    pub root_body: Option<HirId>,
206
207    /// Maps from a scope ID to the enclosing scope id;
208    /// this is usually corresponding to the lexical nesting, though
209    /// in the case of closures the parent scope is the innermost
210    /// conditional expression or repeating block. (Note that the
211    /// enclosing scope ID for the block associated with a closure is
212    /// the closure itself.)
213    pub parent_map: FxIndexMap<Scope, Scope>,
214
215    /// Maps from a variable or binding ID to the block in which that
216    /// variable is declared.
217    var_map: FxIndexMap<hir::ItemLocalId, Scope>,
218
219    /// Identifies expressions which, if captured into a temporary, ought to
220    /// have a temporary whose lifetime extends to the end of the enclosing *block*,
221    /// and not the enclosing *statement*. Expressions that are not present in this
222    /// table are not rvalue candidates. The set of rvalue candidates is computed
223    /// during type check based on a traversal of the AST.
224    pub rvalue_candidates: HirIdMap<RvalueCandidate>,
225
226    /// Backwards incompatible scoping that will be introduced in future editions.
227    /// This information is used later for linting to identify locals and
228    /// temporary values that will receive backwards-incompatible drop orders.
229    pub backwards_incompatible_scope: UnordMap<hir::ItemLocalId, Scope>,
230}
231
232/// See the `rvalue_candidates` field for more information on rvalue
233/// candidates in general.
234/// The `lifetime` field is None to indicate that certain expressions escape
235/// into 'static and should have no local cleanup scope.
236#[derive(Debug, Copy, Clone, HashStable)]
237pub struct RvalueCandidate {
238    pub target: hir::ItemLocalId,
239    pub lifetime: Option<Scope>,
240}
241
242impl ScopeTree {
243    pub fn record_scope_parent(&mut self, child: Scope, parent: Option<Scope>) {
244        debug!("{:?}.parent = {:?}", child, parent);
245
246        if let Some(p) = parent {
247            let prev = self.parent_map.insert(child, p);
248            assert!(prev.is_none());
249        }
250    }
251
252    pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
253        debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
254        assert!(var != lifetime.local_id);
255        self.var_map.insert(var, lifetime);
256    }
257
258    pub fn record_rvalue_candidate(&mut self, var: HirId, candidate: RvalueCandidate) {
259        debug!("record_rvalue_candidate(var={var:?}, candidate={candidate:?})");
260        if let Some(lifetime) = &candidate.lifetime {
261            assert!(var.local_id != lifetime.local_id)
262        }
263        self.rvalue_candidates.insert(var, candidate);
264    }
265
266    /// Returns the narrowest scope that encloses `id`, if any.
267    pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
268        self.parent_map.get(&id).cloned()
269    }
270
271    /// Returns the lifetime of the local variable `var_id`, if any.
272    pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Option<Scope> {
273        self.var_map.get(&var_id).cloned()
274    }
275
276    /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and
277    /// `false` otherwise.
278    ///
279    /// Used by clippy.
280    pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool {
281        let mut s = subscope;
282        debug!("is_subscope_of({:?}, {:?})", subscope, superscope);
283        while superscope != s {
284            match self.opt_encl_scope(s) {
285                None => {
286                    debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
287                    return false;
288                }
289                Some(scope) => s = scope,
290            }
291        }
292
293        debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
294
295        true
296    }
297}