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, ItemLocalMap, Node};
15use rustc_macros::{HashStable, TyDecodable, TyEncodable};
16use rustc_span::{DUMMY_SP, Span};
17use tracing::debug;
18
19use crate::ty::{self, 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::MatchGuard => write!(fmt, "MatchGuard({:?})", 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 of the condition and body of a match arm with a guard
136    /// Used for variables introduced in an if-let guard,
137    /// whose lifetimes do not cross beyond this scope.
138    MatchGuard,
139
140    /// Scope following a `let id = expr;` binding in a block.
141    Remainder(FirstStatementIndex),
142}
143
144rustc_index::newtype_index! {
145    /// Represents a subscope of `block` for a binding that is introduced
146    /// by `block.stmts[first_statement_index]`. Such subscopes represent
147    /// a suffix of the block. Note that each subscope does not include
148    /// the initializer expression, if any, for the statement indexed by
149    /// `first_statement_index`.
150    ///
151    /// For example, given `{ let (a, b) = EXPR_1; let c = EXPR_2; ... }`:
152    ///
153    /// * The subscope with `first_statement_index == 0` is scope of both
154    ///   `a` and `b`; it does not include EXPR_1, but does include
155    ///   everything after that first `let`. (If you want a scope that
156    ///   includes EXPR_1 as well, then do not use `Scope::Remainder`,
157    ///   but instead another `Scope` that encompasses the whole block,
158    ///   e.g., `Scope::Node`.
159    ///
160    /// * The subscope with `first_statement_index == 1` is scope of `c`,
161    ///   and thus does not include EXPR_2, but covers the `...`.
162    #[derive(HashStable)]
163    #[encodable]
164    #[orderable]
165    pub struct FirstStatementIndex {}
166}
167
168// compilation error if size of `ScopeData` is not the same as a `u32`
169rustc_data_structures::static_assert_size!(ScopeData, 4);
170
171impl Scope {
172    pub fn hir_id(&self, scope_tree: &ScopeTree) -> Option<HirId> {
173        scope_tree.root_body.map(|hir_id| HirId { owner: hir_id.owner, local_id: self.local_id })
174    }
175
176    /// Returns the span of this `Scope`. Note that in general the
177    /// returned span may not correspond to the span of any `NodeId` in
178    /// the AST.
179    pub fn span(&self, tcx: TyCtxt<'_>, scope_tree: &ScopeTree) -> Span {
180        let Some(hir_id) = self.hir_id(scope_tree) else {
181            return DUMMY_SP;
182        };
183        let span = tcx.hir_span(hir_id);
184        if let ScopeData::Remainder(first_statement_index) = self.data
185            // Want span for scope starting after the
186            // indexed statement and ending at end of
187            // `blk`; reuse span of `blk` and shift `lo`
188            // forward to end of indexed statement.
189            //
190            // (This is the special case alluded to in the
191            // doc-comment for this method)
192            && let Node::Block(blk) = tcx.hir_node(hir_id)
193        {
194            let stmt_span = blk.stmts[first_statement_index.index()].span;
195
196            // To avoid issues with macro-generated spans, the span
197            // of the statement must be nested in that of the block.
198            if span.lo() <= stmt_span.lo() && stmt_span.lo() <= span.hi() {
199                return span.with_lo(stmt_span.lo());
200            }
201        }
202        span
203    }
204}
205
206/// The region scope tree encodes information about region relationships.
207#[derive(Default, Debug, HashStable)]
208pub struct ScopeTree {
209    /// If not empty, this body is the root of this region hierarchy.
210    pub root_body: Option<HirId>,
211
212    /// Maps from a scope ID to the enclosing scope id;
213    /// this is usually corresponding to the lexical nesting, though
214    /// in the case of closures the parent scope is the innermost
215    /// conditional expression or repeating block. (Note that the
216    /// enclosing scope ID for the block associated with a closure is
217    /// the closure itself.)
218    pub parent_map: FxIndexMap<Scope, Scope>,
219
220    /// Maps from a variable or binding ID to the block in which that
221    /// variable is declared.
222    var_map: FxIndexMap<hir::ItemLocalId, Scope>,
223
224    /// Tracks expressions with extended temporary scopes, based on the syntactic rules for
225    /// temporary lifetime extension. Further details may be found in
226    /// `rustc_hir_analysis::check::region` and in the [Reference].
227    ///
228    /// [Reference]: https://doc.rust-lang.org/nightly/reference/destructors.html#temporary-lifetime-extension
229    extended_temp_scopes: ItemLocalMap<Option<Scope>>,
230
231    /// Backwards incompatible scoping that will be introduced in future editions.
232    /// This information is used later for linting to identify locals and
233    /// temporary values that will receive backwards-incompatible drop orders.
234    pub backwards_incompatible_scope: UnordMap<hir::ItemLocalId, Scope>,
235}
236
237/// Temporary lifetime information for expressions, used when lowering to MIR.
238#[derive(Clone, Copy, Debug, HashStable)]
239pub struct TempLifetime {
240    /// The scope in which a temporary should be dropped. If `None`, no drop is scheduled; this is
241    /// the case for lifetime-extended temporaries extended by a const/static item or const block.
242    pub temp_lifetime: Option<Scope>,
243    /// If `Some(lt)`, indicates that the lifetime of this temporary will change to `lt` in a future edition.
244    /// If `None`, then no changes are expected, or lints are disabled.
245    pub backwards_incompatible: Option<Scope>,
246}
247
248impl ScopeTree {
249    pub fn record_scope_parent(&mut self, child: Scope, parent: Option<Scope>) {
250        debug!("{:?}.parent = {:?}", child, parent);
251
252        if let Some(p) = parent {
253            let prev = self.parent_map.insert(child, p);
254            assert!(prev.is_none());
255        }
256    }
257
258    pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
259        debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
260        assert!(var != lifetime.local_id);
261        self.var_map.insert(var, lifetime);
262    }
263
264    /// Make an association between a sub-expression and an extended lifetime
265    pub fn record_extended_temp_scope(&mut self, var: hir::ItemLocalId, lifetime: Option<Scope>) {
266        debug!(?var, ?lifetime);
267        if let Some(lifetime) = lifetime {
268            assert!(var != lifetime.local_id);
269        }
270        self.extended_temp_scopes.insert(var, lifetime);
271    }
272
273    /// Returns the narrowest scope that encloses `id`, if any.
274    pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
275        self.parent_map.get(&id).cloned()
276    }
277
278    /// Returns the lifetime of the local variable `var_id`, if any.
279    pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Option<Scope> {
280        self.var_map.get(&var_id).cloned()
281    }
282
283    /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and
284    /// `false` otherwise.
285    ///
286    /// Used by clippy.
287    pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool {
288        let mut s = subscope;
289        debug!("is_subscope_of({:?}, {:?})", subscope, superscope);
290        while superscope != s {
291            match self.opt_encl_scope(s) {
292                None => {
293                    debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
294                    return false;
295                }
296                Some(scope) => s = scope,
297            }
298        }
299
300        debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
301
302        true
303    }
304
305    /// Returns the scope of non-lifetime-extended temporaries within a given scope, as well as
306    /// whether we've recorded a potential backwards-incompatible change to lint on.
307    /// Panics if no enclosing temporary scope is found.
308    pub fn default_temporary_scope(&self, inner: Scope) -> (Scope, Option<Scope>) {
309        let mut id = inner;
310        let mut backwards_incompatible = None;
311
312        while let Some(&p) = self.parent_map.get(&id) {
313            match p.data {
314                ScopeData::Destruction => {
315                    debug!("temporary_scope({inner:?}) = {id:?} [enclosing]");
316                    return (id, backwards_incompatible);
317                }
318                ScopeData::IfThenRescope | ScopeData::MatchGuard => {
319                    debug!("temporary_scope({inner:?}) = {p:?} [enclosing]");
320                    return (p, backwards_incompatible);
321                }
322                ScopeData::Node
323                | ScopeData::CallSite
324                | ScopeData::Arguments
325                | ScopeData::IfThen
326                | ScopeData::Remainder(_) => {
327                    // If we haven't already passed through a backwards-incompatible node,
328                    // then check if we are passing through one now and record it if so.
329                    // This is for now only working for cases where a temporary lifetime is
330                    // *shortened*.
331                    if backwards_incompatible.is_none() {
332                        backwards_incompatible =
333                            self.backwards_incompatible_scope.get(&p.local_id).copied();
334                    }
335                    id = p
336                }
337            }
338        }
339
340        span_bug!(ty::tls::with(|tcx| inner.span(tcx, self)), "no enclosing temporary scope")
341    }
342
343    /// Returns the scope when the temp created by `expr_id` will be cleaned up.
344    /// It also emits a lint on potential backwards incompatible change to the temporary scope
345    /// which is *for now* always shortening.
346    pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> TempLifetime {
347        // Check for a designated extended temporary scope.
348        if let Some(&s) = self.extended_temp_scopes.get(&expr_id) {
349            debug!("temporary_scope({expr_id:?}) = {s:?} [custom]");
350            return TempLifetime { temp_lifetime: s, backwards_incompatible: None };
351        }
352
353        // Otherwise, locate the innermost terminating scope.
354        let (scope, backwards_incompatible) =
355            self.default_temporary_scope(Scope { local_id: expr_id, data: ScopeData::Node });
356        TempLifetime { temp_lifetime: Some(scope), backwards_incompatible }
357    }
358}