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::mir::BackwardIncompatibleDropReason;
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::MatchGuard => write!(fmt, "MatchGuard({:?})", self.local_id),
101            ScopeData::Remainder(fsi) => write!(
102                fmt,
103                "Remainder {{ block: {:?}, first_statement_index: {}}}",
104                self.local_id,
105                fsi.as_u32(),
106            ),
107        }
108    }
109}
110
111#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, TyEncodable, TyDecodable)]
112#[derive(HashStable)]
113pub enum ScopeData {
114    Node,
115
116    /// Scope of the call-site for a function or closure
117    /// (outlives the arguments as well as the body).
118    CallSite,
119
120    /// Scope of arguments passed to a function or closure
121    /// (they outlive its body).
122    Arguments,
123
124    /// Scope of destructors for temporaries of node-id.
125    Destruction,
126
127    /// Scope of the condition and then block of an if expression
128    /// Used for variables introduced in an if-let expression.
129    IfThen,
130
131    /// Scope of the condition and then block of an if expression
132    /// Used for variables introduced in an if-let expression,
133    /// whose lifetimes do not cross beyond this scope.
134    IfThenRescope,
135
136    /// Scope of the condition and body of a match arm with a guard
137    /// Used for variables introduced in an if-let guard,
138    /// whose lifetimes do not cross beyond this scope.
139    MatchGuard,
140
141    /// Scope following a `let id = expr;` binding in a block.
142    Remainder(FirstStatementIndex),
143}
144
145rustc_index::newtype_index! {
146    /// Represents a subscope of `block` for a binding that is introduced
147    /// by `block.stmts[first_statement_index]`. Such subscopes represent
148    /// a suffix of the block. Note that each subscope does not include
149    /// the initializer expression, if any, for the statement indexed by
150    /// `first_statement_index`.
151    ///
152    /// For example, given `{ let (a, b) = EXPR_1; let c = EXPR_2; ... }`:
153    ///
154    /// * The subscope with `first_statement_index == 0` is scope of both
155    ///   `a` and `b`; it does not include EXPR_1, but does include
156    ///   everything after that first `let`. (If you want a scope that
157    ///   includes EXPR_1 as well, then do not use `Scope::Remainder`,
158    ///   but instead another `Scope` that encompasses the whole block,
159    ///   e.g., `Scope::Node`.
160    ///
161    /// * The subscope with `first_statement_index == 1` is scope of `c`,
162    ///   and thus does not include EXPR_2, but covers the `...`.
163    #[derive(HashStable)]
164    #[encodable]
165    #[orderable]
166    pub struct FirstStatementIndex {}
167}
168
169// compilation error if size of `ScopeData` is not the same as a `u32`
170rustc_data_structures::static_assert_size!(ScopeData, 4);
171
172impl Scope {
173    pub fn hir_id(&self, scope_tree: &ScopeTree) -> Option<HirId> {
174        scope_tree.root_body.map(|hir_id| HirId { owner: hir_id.owner, local_id: self.local_id })
175    }
176
177    /// Returns the span of this `Scope`. Note that in general the
178    /// returned span may not correspond to the span of any `NodeId` in
179    /// the AST.
180    pub fn span(&self, tcx: TyCtxt<'_>, scope_tree: &ScopeTree) -> Span {
181        let Some(hir_id) = self.hir_id(scope_tree) else {
182            return DUMMY_SP;
183        };
184        let span = tcx.hir_span(hir_id);
185        if let ScopeData::Remainder(first_statement_index) = self.data
186            // Want span for scope starting after the
187            // indexed statement and ending at end of
188            // `blk`; reuse span of `blk` and shift `lo`
189            // forward to end of indexed statement.
190            //
191            // (This is the special case alluded to in the
192            // doc-comment for this method)
193            && let Node::Block(blk) = tcx.hir_node(hir_id)
194        {
195            let stmt_span = blk.stmts[first_statement_index.index()].span;
196
197            // To avoid issues with macro-generated spans, the span
198            // of the statement must be nested in that of the block.
199            if span.lo() <= stmt_span.lo() && stmt_span.lo() <= span.hi() {
200                return span.with_lo(stmt_span.lo());
201            }
202        }
203        span
204    }
205}
206
207/// The region scope tree encodes information about region relationships.
208#[derive(Default, Debug, HashStable)]
209pub struct ScopeTree {
210    /// If not empty, this body is the root of this region hierarchy.
211    pub root_body: Option<HirId>,
212
213    /// Maps from a scope ID to the enclosing scope id;
214    /// this is usually corresponding to the lexical nesting, though
215    /// in the case of closures the parent scope is the innermost
216    /// conditional expression or repeating block. (Note that the
217    /// enclosing scope ID for the block associated with a closure is
218    /// the closure itself.)
219    pub parent_map: FxIndexMap<Scope, Scope>,
220
221    /// Maps from a variable or binding ID to the block in which that
222    /// variable is declared.
223    var_map: FxIndexMap<hir::ItemLocalId, Scope>,
224
225    /// Maps from bindings to their future scopes after #145838 for the
226    /// `macro_extended_temporary_scopes` lint.
227    var_compatibility_map: FxIndexMap<hir::ItemLocalId, Scope>,
228
229    /// Identifies expressions which, if captured into a temporary, ought to
230    /// have a temporary whose lifetime extends to the end of the enclosing *block*,
231    /// and not the enclosing *statement*. Expressions that are not present in this
232    /// table are not rvalue candidates. The set of rvalue candidates is computed
233    /// during type check based on a traversal of the AST.
234    pub rvalue_candidates: HirIdMap<RvalueCandidate>,
235
236    /// Backwards incompatible scoping that will be introduced in future editions.
237    /// This information is used later for linting to identify locals and
238    /// temporary values that will receive backwards-incompatible drop orders.
239    pub backwards_incompatible_scope: UnordMap<hir::ItemLocalId, Scope>,
240}
241
242/// See the `rvalue_candidates` field for more information on rvalue
243/// candidates in general.
244/// The `lifetime` field is None to indicate that certain expressions escape
245/// into 'static and should have no local cleanup scope.
246#[derive(Debug, Copy, Clone, HashStable)]
247pub struct RvalueCandidate {
248    pub target: hir::ItemLocalId,
249    pub lifetime: Option<Scope>,
250    pub compat: ScopeCompatibility,
251}
252
253/// Marks extended temporary scopes that will be shortened by #145838 and thus need to be linted on
254/// by the `macro_extended_temporary_scopes` future-incompatibility warning.
255#[derive(TyEncodable, TyDecodable, Clone, Copy, Debug, Eq, PartialEq, HashStable)]
256pub enum ScopeCompatibility {
257    /// Marks a scope that was extended past a temporary destruction scope by a non-extending
258    /// `super let` initializer.
259    FutureIncompatible { shortens_to: Scope },
260    /// Marks an extended temporary scope that was not extended by a non-extending `super let`
261    /// initializer.
262    FutureCompatible,
263}
264
265impl ScopeTree {
266    pub fn record_scope_parent(&mut self, child: Scope, parent: Option<Scope>) {
267        debug!("{:?}.parent = {:?}", child, parent);
268
269        if let Some(p) = parent {
270            let prev = self.parent_map.insert(child, p);
271            assert!(prev.is_none());
272        }
273    }
274
275    pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
276        debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
277        assert!(var != lifetime.local_id);
278        self.var_map.insert(var, lifetime);
279    }
280
281    pub fn record_future_incompatible_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
282        assert!(var != lifetime.local_id);
283        self.var_compatibility_map.insert(var, lifetime);
284    }
285
286    pub fn record_rvalue_candidate(&mut self, var: HirId, candidate: RvalueCandidate) {
287        debug!("record_rvalue_candidate(var={var:?}, candidate={candidate:?})");
288        if let Some(lifetime) = &candidate.lifetime {
289            assert!(var.local_id != lifetime.local_id)
290        }
291        self.rvalue_candidates.insert(var, candidate);
292    }
293
294    /// Returns the narrowest scope that encloses `id`, if any.
295    pub fn opt_encl_scope(&self, id: Scope) -> Option<Scope> {
296        self.parent_map.get(&id).cloned()
297    }
298
299    /// Returns the lifetime of the local variable `var_id`, if any, as well as whether it is
300    /// shortening after #145838.
301    pub fn var_scope(&self, var_id: hir::ItemLocalId) -> (Option<Scope>, ScopeCompatibility) {
302        let compat = match self.var_compatibility_map.get(&var_id) {
303            Some(&shortens_to) => ScopeCompatibility::FutureIncompatible { shortens_to },
304            None => ScopeCompatibility::FutureCompatible,
305        };
306        (self.var_map.get(&var_id).cloned(), compat)
307    }
308
309    /// Returns `true` if `subscope` is equal to or is lexically nested inside `superscope`, and
310    /// `false` otherwise.
311    ///
312    /// Used by clippy.
313    pub fn is_subscope_of(&self, subscope: Scope, superscope: Scope) -> bool {
314        let mut s = subscope;
315        debug!("is_subscope_of({:?}, {:?})", subscope, superscope);
316        while superscope != s {
317            match self.opt_encl_scope(s) {
318                None => {
319                    debug!("is_subscope_of({:?}, {:?}, s={:?})=false", subscope, superscope, s);
320                    return false;
321                }
322                Some(scope) => s = scope,
323            }
324        }
325
326        debug!("is_subscope_of({:?}, {:?})=true", subscope, superscope);
327
328        true
329    }
330
331    /// Returns the scope of non-lifetime-extended temporaries within a given scope, as well as
332    /// whether we've recorded a potential backwards-incompatible change to lint on.
333    /// Returns `None` when no enclosing temporary scope is found, such as for static items.
334    pub fn default_temporary_scope(
335        &self,
336        inner: Scope,
337    ) -> (Option<Scope>, Option<(Scope, BackwardIncompatibleDropReason)>) {
338        let mut id = inner;
339        let mut backwards_incompatible = None;
340
341        while let Some(&p) = self.parent_map.get(&id) {
342            match p.data {
343                ScopeData::Destruction => {
344                    debug!("temporary_scope({inner:?}) = {id:?} [enclosing]");
345                    return (Some(id), backwards_incompatible);
346                }
347                ScopeData::IfThenRescope | ScopeData::MatchGuard => {
348                    debug!("temporary_scope({inner:?}) = {p:?} [enclosing]");
349                    return (Some(p), backwards_incompatible);
350                }
351                ScopeData::Node
352                | ScopeData::CallSite
353                | ScopeData::Arguments
354                | ScopeData::IfThen
355                | ScopeData::Remainder(_) => {
356                    // If we haven't already passed through a backwards-incompatible node,
357                    // then check if we are passing through one now and record it if so.
358                    // This is for now only working for cases where a temporary lifetime is
359                    // *shortened*.
360                    if backwards_incompatible.is_none() {
361                        backwards_incompatible = self
362                            .backwards_incompatible_scope
363                            .get(&p.local_id)
364                            .map(|&s| (s, BackwardIncompatibleDropReason::Edition2024));
365                    }
366                    id = p
367                }
368            }
369        }
370
371        debug!("temporary_scope({inner:?}) = None");
372        (None, backwards_incompatible)
373    }
374}