Skip to main content

rustc_mir_build/builder/
scope.rs

1/*!
2Managing the scope stack. The scopes are tied to lexical scopes, so as
3we descend the THIR, we push a scope on the stack, build its
4contents, and then pop it off. Every scope is named by a
5`region::Scope`.
6
7### SEME Regions
8
9When pushing a new [Scope], we record the current point in the graph (a
10basic block); this marks the entry to the scope. We then generate more
11stuff in the control-flow graph. Whenever the scope is exited, either
12via a `break` or `return` or just by fallthrough, that marks an exit
13from the scope. Each lexical scope thus corresponds to a single-entry,
14multiple-exit (SEME) region in the control-flow graph.
15
16For now, we record the `region::Scope` to each SEME region for later reference
17(see caveat in next paragraph). This is because destruction scopes are tied to
18them. This may change in the future so that MIR lowering determines its own
19destruction scopes.
20
21### Not so SEME Regions
22
23In the course of building matches, it sometimes happens that certain code
24(namely guards) gets executed multiple times. This means that the scope lexical
25scope may in fact correspond to multiple, disjoint SEME regions. So in fact our
26mapping is from one scope to a vector of SEME regions. Since the SEME regions
27are disjoint, the mapping is still one-to-one for the set of SEME regions that
28we're currently in.
29
30Also in matches, the scopes assigned to arms are not always even SEME regions!
31Each arm has a single region with one entry for each pattern. We manually
32manipulate the scheduled drops in this scope to avoid dropping things multiple
33times.
34
35### Drops
36
37The primary purpose for scopes is to insert drops: while building
38the contents, we also accumulate places that need to be dropped upon
39exit from each scope. This is done by calling `schedule_drop`. Once a
40drop is scheduled, whenever we branch out we will insert drops of all
41those places onto the outgoing edge. Note that we don't know the full
42set of scheduled drops up front, and so whenever we exit from the
43scope we only drop the values scheduled thus far. For example, consider
44the scope S corresponding to this loop:
45
46```
47# let cond = true;
48loop {
49    let x = ..;
50    if cond { break; }
51    let y = ..;
52}
53```
54
55When processing the `let x`, we will add one drop to the scope for
56`x`. The break will then insert a drop for `x`. When we process `let
57y`, we will add another drop (in fact, to a subscope, but let's ignore
58that for now); any later drops would also drop `y`.
59
60### Early exit
61
62There are numerous "normal" ways to early exit a scope: `break`,
63`continue`, `return` (panics are handled separately). Whenever an
64early exit occurs, the method `break_scope` is called. It is given the
65current point in execution where the early exit occurs, as well as the
66scope you want to branch to (note that all early exits from to some
67other enclosing scope). `break_scope` will record the set of drops currently
68scheduled in a [DropTree]. Later, before `in_breakable_scope` exits, the drops
69will be added to the CFG.
70
71Panics are handled in a similar fashion, except that the drops are added to the
72MIR once the rest of the function has finished being lowered. If a terminator
73can panic, call `diverge_from(block)` with the block containing the terminator
74`block`.
75
76### Breakable scopes
77
78In addition to the normal scope stack, we track a loop scope stack
79that contains only loops and breakable blocks. It tracks where a `break`,
80`continue` or `return` should go to.
81
82*/
83
84use std::mem;
85
86use interpret::ErrorHandled;
87use rustc_attr_ir::find_attr;
88use rustc_data_structures::fx::FxHashMap;
89use rustc_hir::HirId;
90use rustc_index::{IndexSlice, IndexVec};
91use rustc_middle::middle::region;
92use rustc_middle::mir::{self, *};
93use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind};
94use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, ValTree};
95use rustc_pattern_analysis::rustc::RustcPatCtxt;
96use rustc_span::{DUMMY_SP, Span, Spanned, bug, span_bug};
97use tracing::{debug, instrument};
98
99use super::matches::BuiltMatchTree;
100use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG};
101use crate::diagnostics::{
102    ConstContinueBadConst, ConstContinueNotMonomorphicConst, ConstContinueUnknownJumpTarget,
103};
104
105#[derive(#[automatically_derived]impl<'tcx> ::core::fmt::Debug for Scopes<'tcx> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            "Scopes scopes breakable_scopes const_continuable_scopes if_then_scope unwind_drops coroutine_drops";
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.scopes, &self.breakable_scopes,
                        &self.const_continuable_scopes, &self.if_then_scope,
                        &self.unwind_drops, &&self.coroutine_drops];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, names, values)
    }
}Debug)]
106pub(crate) struct Scopes<'tcx> {
107    scopes: Vec<Scope>,
108
109    /// The current set of breakable scopes. See module comment for more details.
110    breakable_scopes: Vec<BreakableScope<'tcx>>,
111
112    const_continuable_scopes: Vec<ConstContinuableScope<'tcx>>,
113
114    /// The scope of the innermost if-then currently being lowered.
115    if_then_scope: Option<IfThenScope>,
116
117    /// Drops that need to be done on unwind paths. See the comment on
118    /// [DropTree] for more details.
119    unwind_drops: DropTree,
120
121    /// Drops that need to be done on paths to the `CoroutineDrop` terminator.
122    coroutine_drops: DropTree,
123}
124
125#[derive(#[automatically_derived]impl ::core::fmt::Debug for Scope {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            "Scope source_scope region_scope drops moved_locals cached_unwind_block cached_coroutine_drop_block";
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.source_scope, &self.region_scope, &self.drops,
                        &self.moved_locals, &self.cached_unwind_block,
                        &&self.cached_coroutine_drop_block];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, names, values)
    }
}Debug)]
126struct Scope {
127    /// The source scope this scope was created in.
128    source_scope: SourceScope,
129
130    /// the region span of this scope within source code.
131    region_scope: region::Scope,
132
133    /// set of places to drop when exiting this scope. This starts
134    /// out empty but grows as variables are declared during the
135    /// building process. This is a stack, so we always drop from the
136    /// end of the vector (top of the stack) first.
137    drops: Vec<DropData>,
138
139    moved_locals: Vec<Local>,
140
141    /// The drop index that will drop everything in and below this scope on an
142    /// unwind path.
143    cached_unwind_block: Option<DropIdx>,
144
145    /// The drop index that will drop everything in and below this scope on a
146    /// coroutine drop path.
147    cached_coroutine_drop_block: Option<DropIdx>,
148}
149
150#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for DropData {
}
#[automatically_derived]impl ::core::clone::Clone for DropData {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<SourceInfo>;
        let _: ::core::clone::AssertParamIsClone<Local>;
        let _: ::core::clone::AssertParamIsClone<DropKind>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::marker::Copy for DropData { }Copy, #[automatically_derived]impl ::core::fmt::Debug for DropData {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f,
            "DropData source_info local kind", &self.source_info, &self.local,
            &&self.kind)
    }
}Debug)]
151struct DropData {
152    /// The `Span` where drop obligation was incurred (typically where place was
153    /// declared)
154    source_info: SourceInfo,
155
156    /// local to drop
157    local: Local,
158
159    /// Whether this is a value Drop or a StorageDead.
160    kind: DropKind,
161}
162
163#[derive(#[automatically_derived]impl ::core::fmt::Debug for DropKind {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                DropKind::Value => "Value",
                DropKind::Storage => "Storage",
                DropKind::ForLint => "ForLint",
            })
    }
}Debug, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for DropKind {
}
#[automatically_derived]impl ::core::clone::Clone for DropKind {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::Copy for DropKind { }Copy, #[automatically_derived]impl ::core::marker::StructuralPartialEq for DropKind
    {
}
#[automatically_derived]impl ::core::cmp::PartialEq for DropKind {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
            ::core::intrinsics::discriminant_value(other)
    }
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for DropKind { }Eq, #[automatically_derived]impl ::core::hash::Hash for DropKind {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
            state)
    }
}Hash)]
164enum DropKind {
165    Value,
166    Storage,
167    ForLint,
168}
169
170#[derive(#[automatically_derived]impl<'tcx> ::core::fmt::Debug for BreakableScope<'tcx>
    {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f,
            "BreakableScope region_scope break_destination break_drops continue_drops",
            &self.region_scope, &self.break_destination, &self.break_drops,
            &&self.continue_drops)
    }
}Debug)]
171struct BreakableScope<'tcx> {
172    /// Region scope of the loop
173    region_scope: region::Scope,
174    /// The destination of the loop/block expression itself (i.e., where to put
175    /// the result of a `break` or `return` expression)
176    break_destination: Place<'tcx>,
177    /// Drops that happen on the `break`/`return` path.
178    break_drops: DropTree,
179    /// Drops that happen on the `continue` path.
180    continue_drops: Option<DropTree>,
181}
182
183#[derive(#[automatically_derived]impl<'tcx> ::core::fmt::Debug for
    ConstContinuableScope<'tcx> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field5_finish(f,
            "ConstContinuableScope region_scope state_place arms built_match_tree const_continue_drops",
            &self.region_scope, &self.state_place, &self.arms,
            &self.built_match_tree, &&self.const_continue_drops)
    }
}Debug)]
184struct ConstContinuableScope<'tcx> {
185    /// The scope for the `#[loop_match]` which its `#[const_continue]`s will jump to.
186    region_scope: region::Scope,
187    /// The place of the state of a `#[loop_match]`, which a `#[const_continue]` must update.
188    state_place: Place<'tcx>,
189
190    arms: Box<[ArmId]>,
191    built_match_tree: BuiltMatchTree<'tcx>,
192
193    /// Drops that happen on a `#[const_continue]`
194    const_continue_drops: DropTree,
195}
196
197#[derive(#[automatically_derived]impl ::core::fmt::Debug for IfThenScope {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "IfThenScope region_scope else_drops", &self.region_scope,
            &&self.else_drops)
    }
}Debug)]
198struct IfThenScope {
199    /// The if-then scope or arm scope
200    region_scope: region::Scope,
201    /// Drops that happen on the `else` path.
202    else_drops: DropTree,
203}
204
205/// The target of an expression that breaks out of a scope
206#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for BreakableTarget {
}
#[automatically_derived]impl ::core::clone::Clone for BreakableTarget {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<region::Scope>;
        let _: ::core::clone::AssertParamIsClone<region::Scope>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::marker::Copy for BreakableTarget { }Copy, #[automatically_derived]impl ::core::fmt::Debug for BreakableTarget {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::Continue(__self_0) =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Continue", &__self_0),
            Self::Break(__self_0) =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Break",
                    &__self_0),
            Self::Return => ::core::fmt::Formatter::write_str(f, "Return"),
        }
    }
}Debug)]
207pub(crate) enum BreakableTarget {
208    Continue(region::Scope),
209    Break(region::Scope),
210    Return,
211}
212
213#[automatically_derived]impl ::core::marker::Copy for DropIdx { }
impl DropIdx {
    #[doc = r" Maximum value the index can take, as a `u32`."]
    const MAX_AS_U32: u32 = 0xFFFF_FF00;
    #[doc = r" Maximum value the index can take."]
    const MAX: Self = Self::from_u32(0xFFFF_FF00);
    #[doc = r" Zero value of the index."]
    const ZERO: Self = Self::from_u32(0);
    #[doc = r" Creates a new index from a given `usize`."]
    #[doc = r""]
    #[doc = r" # Panics"]
    #[doc = r""]
    #[doc = r" Will panic if `value` exceeds `MAX`."]
    #[inline]
    const fn from_usize(value: usize) -> Self {
        if !(value <= (0xFFFF_FF00 as usize)) {
            ::core::panicking::panic("assertion failed: value <= (0xFFFF_FF00 as usize)")
        };
        unsafe { Self::from_u32_unchecked(value as u32) }
    }
    #[doc = r" Creates a new index from a given `u32`."]
    #[doc = r""]
    #[doc = r" # Panics"]
    #[doc = r""]
    #[doc = r" Will panic if `value` exceeds `MAX`."]
    #[inline]
    const fn from_u32(value: u32) -> Self {
        if !(value <= 0xFFFF_FF00) {
            ::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
        };
        unsafe { Self::from_u32_unchecked(value) }
    }
    #[doc = r" Creates a new index from a given `u16`."]
    #[doc = r""]
    #[doc = r" # Panics"]
    #[doc = r""]
    #[doc = r" Will panic if `value` exceeds `MAX`."]
    #[inline]
    const fn from_u16(value: u16) -> Self {
        let value = value as u32;
        if !(value <= 0xFFFF_FF00) {
            ::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
        };
        unsafe { Self::from_u32_unchecked(value) }
    }
    #[doc = r" Creates a new index from a given `u32`."]
    #[doc = r""]
    #[doc = r" # Safety"]
    #[doc = r""]
    #[doc =
    r" The provided value must be less than or equal to the maximum value for the newtype."]
    #[doc =
    r" Providing a value outside this range is undefined due to layout restrictions."]
    #[doc = r""]
    #[doc = r" Prefer using `from_u32`."]
    #[inline]
    const unsafe fn from_u32_unchecked(value: u32) -> Self {
        Self {
            private_use_as_methods_instead: unsafe {
                std::mem::transmute(value)
            },
        }
    }
    #[doc = r" Extracts the value of this index as a `usize`."]
    #[inline]
    const fn index(self) -> usize { self.as_usize() }
    #[doc = r" Extracts the value of this index as a `u32`."]
    #[inline]
    const fn as_u32(self) -> u32 {
        unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
    }
    #[doc = r" Extracts the value of this index as a `usize`."]
    #[inline]
    const fn as_usize(self) -> usize { self.as_u32() as usize }
}
impl std::ops::Add<usize> for DropIdx {
    type Output = Self;
    #[inline]
    fn add(self, other: usize) -> Self {
        Self::from_usize(self.index() + other)
    }
}
impl std::ops::AddAssign<usize> for DropIdx {
    #[inline]
    fn add_assign(&mut self, other: usize) { *self = *self + other; }
}
impl rustc_index::Idx for DropIdx {
    #[inline]
    fn new(value: usize) -> Self { Self::from_usize(value) }
    #[inline]
    fn index(self) -> usize { self.as_usize() }
}
impl ::std::iter::Step for DropIdx {
    #[inline]
    fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
        <usize as
                ::std::iter::Step>::steps_between(&Self::index(*start),
            &Self::index(*end))
    }
    #[inline]
    fn forward_checked(start: Self, u: usize) -> Option<Self> {
        Self::index(start).checked_add(u).map(Self::from_usize)
    }
    #[inline]
    fn backward_checked(start: Self, u: usize) -> Option<Self> {
        Self::index(start).checked_sub(u).map(Self::from_usize)
    }
    #[inline]
    fn forward_overflowing(start: Self, u: usize) -> (Self, bool) {
        let (s, o) = Self::index(start).overflowing_add(u);
        (Self::from_usize(s), o)
    }
    #[inline]
    fn backward_overflowing(start: Self, u: usize) -> (Self, bool) {
        let (s, o) = Self::index(start).overflowing_sub(u);
        (Self::from_usize(s), o)
    }
}
impl ::std::cmp::Ord for DropIdx {
    #[inline]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.as_u32().cmp(&other.as_u32())
    }
}
impl ::std::cmp::PartialOrd for DropIdx {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl From<DropIdx> for u32 {
    #[inline]
    fn from(v: DropIdx) -> u32 { v.as_u32() }
}
impl From<DropIdx> for usize {
    #[inline]
    fn from(v: DropIdx) -> usize { v.as_usize() }
}
impl From<usize> for DropIdx {
    #[inline]
    fn from(value: usize) -> Self { Self::from_usize(value) }
}
impl From<u32> for DropIdx {
    #[inline]
    fn from(value: u32) -> Self { Self::from_u32(value) }
}
impl ::std::cmp::Eq for DropIdx {}
impl ::std::cmp::PartialEq for DropIdx {
    fn eq(&self, other: &Self) -> bool { self.as_u32().eq(&other.as_u32()) }
}
impl ::std::marker::StructuralPartialEq for DropIdx {}
impl ::std::hash::Hash for DropIdx {
    fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
        self.as_u32().hash(state)
    }
}
impl ::std::fmt::Debug for DropIdx {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
        fmt.write_fmt(format_args!("{0}", self.as_u32()))
    }
}rustc_index::newtype_index! {
214    #[orderable]
215    struct DropIdx {}
216}
217
218const ROOT_NODE: DropIdx = DropIdx::ZERO;
219
220/// A tree of drops that we have deferred lowering. It's used for:
221///
222/// * Drops on unwind paths
223/// * Drops on coroutine drop paths (when a suspended coroutine is dropped)
224/// * Drops on return and loop exit paths
225/// * Drops on the else path in an `if let` chain
226///
227/// Once no more nodes could be added to the tree, we lower it to MIR in one go
228/// in `build_mir`.
229#[derive(#[automatically_derived]impl ::core::fmt::Debug for DropTree {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f,
            "DropTree drop_nodes existing_drops_map entry_points",
            &self.drop_nodes, &self.existing_drops_map, &&self.entry_points)
    }
}Debug)]
230struct DropTree {
231    /// Nodes in the drop tree, containing drop data and a link to the next node.
232    drop_nodes: IndexVec<DropIdx, DropNode>,
233    /// Map for finding the index of an existing node, given its contents.
234    existing_drops_map: FxHashMap<DropNodeKey, DropIdx>,
235    /// Edges into the `DropTree` that need to be added once it's lowered.
236    entry_points: Vec<(DropIdx, BasicBlock)>,
237}
238
239/// A single node in the drop tree.
240#[derive(#[automatically_derived]impl ::core::fmt::Debug for DropNode {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "DropNode data next", &self.data, &&self.next)
    }
}Debug)]
241struct DropNode {
242    /// Info about the drop to be performed at this node in the drop tree.
243    data: DropData,
244    /// Index of the "next" drop to perform (in drop order, not declaration order).
245    next: DropIdx,
246}
247
248/// Subset of [`DropNode`] used for reverse lookup in a hash table.
249#[derive(#[automatically_derived]impl ::core::fmt::Debug for DropNodeKey {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "DropNodeKey next local", &self.next, &&self.local)
    }
}Debug, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
    DropNodeKey {
}
#[automatically_derived]impl ::core::cmp::PartialEq for DropNodeKey {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.next == other.next && self.local == other.local
    }
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for DropNodeKey {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<DropIdx>;
        let _: ::core::cmp::AssertParamIsEq<Local>;
    }
}Eq, #[automatically_derived]impl ::core::hash::Hash for DropNodeKey {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.next, state);
        ::core::hash::Hash::hash(&self.local, state)
    }
}Hash)]
250struct DropNodeKey {
251    next: DropIdx,
252    local: Local,
253}
254
255impl Scope {
256    /// Whether there's anything to do for the cleanup path, that is,
257    /// when unwinding through this scope. This includes destructors,
258    /// but not StorageDead statements, which don't get emitted at all
259    /// for unwinding, for several reasons:
260    ///  * clang doesn't emit llvm.lifetime.end for C++ unwinding
261    ///  * LLVM's memory dependency analysis can't handle it atm
262    ///  * polluting the cleanup MIR with StorageDead creates
263    ///    landing pads even though there's no actual destructors
264    ///  * freeing up stack space has no effect during unwinding
265    /// Note that for coroutines we do emit StorageDeads, for the
266    /// use of optimizations in the MIR coroutine transform.
267    fn needs_cleanup(&self) -> bool {
268        self.drops.iter().any(|drop| match drop.kind {
269            DropKind::Value | DropKind::ForLint => true,
270            DropKind::Storage => false,
271        })
272    }
273
274    fn invalidate_cache(&mut self) {
275        self.cached_unwind_block = None;
276        self.cached_coroutine_drop_block = None;
277    }
278}
279
280/// A trait that determined how [DropTree] creates its blocks and
281/// links to any entry nodes.
282trait DropTreeBuilder<'tcx> {
283    /// Create a new block for the tree. This should call either
284    /// `cfg.start_new_block()` or `cfg.start_new_cleanup_block()`.
285    fn make_block(cfg: &mut CFG<'tcx>) -> BasicBlock;
286
287    /// Links a block outside the drop tree, `from`, to the block `to` inside
288    /// the drop tree.
289    fn link_entry_point(cfg: &mut CFG<'tcx>, from: BasicBlock, to: BasicBlock);
290}
291
292impl DropTree {
293    fn new() -> Self {
294        // The root node of the tree doesn't represent a drop, but instead
295        // represents the block in the tree that should be jumped to once all
296        // of the required drops have been performed.
297        let fake_source_info = SourceInfo::outermost(DUMMY_SP);
298        let fake_data =
299            DropData { source_info: fake_source_info, local: Local::MAX, kind: DropKind::Storage };
300        let drop_nodes = IndexVec::from_raw(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [DropNode { data: fake_data, next: DropIdx::MAX }]))vec![DropNode { data: fake_data, next: DropIdx::MAX }]);
301        Self { drop_nodes, entry_points: Vec::new(), existing_drops_map: FxHashMap::default() }
302    }
303
304    /// Adds a node to the drop tree, consisting of drop data and the index of
305    /// the "next" drop (in drop order), which could be the sentinel [`ROOT_NODE`].
306    ///
307    /// If there is already an equivalent node in the tree, nothing is added, and
308    /// that node's index is returned. Otherwise, the new node's index is returned.
309    fn add_drop(&mut self, data: DropData, next: DropIdx) -> DropIdx {
310        let drop_nodes = &mut self.drop_nodes;
311        *self
312            .existing_drops_map
313            .entry(DropNodeKey { next, local: data.local })
314            // Create a new node, and also add its index to the map.
315            .or_insert_with(|| drop_nodes.push(DropNode { data, next }))
316    }
317
318    /// Registers `from` as an entry point to this drop tree, at `to`.
319    ///
320    /// During [`Self::build_mir`], `from` will be linked to the corresponding
321    /// block within the drop tree.
322    fn add_entry_point(&mut self, from: BasicBlock, to: DropIdx) {
323        if true {
    if !(to < self.drop_nodes.next_index()) {
        ::core::panicking::panic("assertion failed: to < self.drop_nodes.next_index()")
    };
};debug_assert!(to < self.drop_nodes.next_index());
324        self.entry_points.push((to, from));
325    }
326
327    /// Builds the MIR for a given drop tree.
328    fn build_mir<'tcx, T: DropTreeBuilder<'tcx>>(
329        &mut self,
330        cfg: &mut CFG<'tcx>,
331        root_node: Option<BasicBlock>,
332    ) -> IndexVec<DropIdx, Option<BasicBlock>> {
333        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs:333",
                        "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                        ::tracing_core::__macro_support::Option::Some(333u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("DropTree::build_mir(drops = {0:#?})",
                                                    self) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("DropTree::build_mir(drops = {:#?})", self);
334
335        let mut blocks = self.assign_blocks::<T>(cfg, root_node);
336        self.link_blocks(cfg, &mut blocks);
337
338        blocks
339    }
340
341    /// Assign blocks for all of the drops in the drop tree that need them.
342    fn assign_blocks<'tcx, T: DropTreeBuilder<'tcx>>(
343        &mut self,
344        cfg: &mut CFG<'tcx>,
345        root_node: Option<BasicBlock>,
346    ) -> IndexVec<DropIdx, Option<BasicBlock>> {
347        // StorageDead statements can share blocks with each other and also with
348        // a Drop terminator. We iterate through the drops to find which drops
349        // need their own block.
350        #[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for Block {
}
#[automatically_derived]impl ::core::clone::Clone for Block {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<DropIdx>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::marker::Copy for Block { }Copy)]
351        enum Block {
352            // This drop is unreachable
353            None,
354            // This drop is only reachable through the `StorageDead` with the
355            // specified index.
356            Shares(DropIdx),
357            // This drop has more than one way of being reached, or it is
358            // branched to from outside the tree, or its predecessor is a
359            // `Value` drop.
360            Own,
361        }
362
363        let mut blocks = IndexVec::from_elem(None, &self.drop_nodes);
364        blocks[ROOT_NODE] = root_node;
365
366        let mut needs_block = IndexVec::from_elem(Block::None, &self.drop_nodes);
367        if root_node.is_some() {
368            // In some cases (such as drops for `continue`) the root node
369            // already has a block. In this case, make sure that we don't
370            // override it.
371            needs_block[ROOT_NODE] = Block::Own;
372        }
373
374        // Sort so that we only need to check the last value.
375        let entry_points = &mut self.entry_points;
376        entry_points.sort();
377
378        for (drop_idx, drop_node) in self.drop_nodes.iter_enumerated().rev() {
379            if entry_points.last().is_some_and(|entry_point| entry_point.0 == drop_idx) {
380                let block = *blocks[drop_idx].get_or_insert_with(|| T::make_block(cfg));
381                needs_block[drop_idx] = Block::Own;
382                while entry_points.last().is_some_and(|entry_point| entry_point.0 == drop_idx) {
383                    let entry_block = entry_points.pop().unwrap().1;
384                    T::link_entry_point(cfg, entry_block, block);
385                }
386            }
387            match needs_block[drop_idx] {
388                Block::None => continue,
389                Block::Own => {
390                    blocks[drop_idx].get_or_insert_with(|| T::make_block(cfg));
391                }
392                Block::Shares(pred) => {
393                    blocks[drop_idx] = blocks[pred];
394                }
395            }
396            if let DropKind::Value = drop_node.data.kind {
397                needs_block[drop_node.next] = Block::Own;
398            } else if drop_idx != ROOT_NODE {
399                match &mut needs_block[drop_node.next] {
400                    pred @ Block::None => *pred = Block::Shares(drop_idx),
401                    pred @ Block::Shares(_) => *pred = Block::Own,
402                    Block::Own => (),
403                }
404            }
405        }
406
407        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs:407",
                        "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                        ::tracing_core::__macro_support::Option::Some(407u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("assign_blocks: blocks = {0:#?}",
                                                    blocks) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("assign_blocks: blocks = {:#?}", blocks);
408        if !entry_points.is_empty() {
    ::core::panicking::panic("assertion failed: entry_points.is_empty()")
};assert!(entry_points.is_empty());
409
410        blocks
411    }
412
413    fn link_blocks<'tcx>(
414        &self,
415        cfg: &mut CFG<'tcx>,
416        blocks: &IndexSlice<DropIdx, Option<BasicBlock>>,
417    ) {
418        for (drop_idx, drop_node) in self.drop_nodes.iter_enumerated().rev() {
419            let Some(block) = blocks[drop_idx] else { continue };
420            match drop_node.data.kind {
421                DropKind::Value => {
422                    let terminator = TerminatorKind::Drop {
423                        target: blocks[drop_node.next].unwrap(),
424                        // The caller will handle this if needed.
425                        unwind: UnwindAction::Terminate(UnwindTerminateReason::InCleanup),
426                        place: drop_node.data.local.into(),
427                        replace: false,
428                        drop: None,
429                    };
430                    cfg.terminate(block, drop_node.data.source_info, terminator);
431                }
432                DropKind::ForLint => {
433                    let stmt = Statement::new(
434                        drop_node.data.source_info,
435                        StatementKind::BackwardIncompatibleDropHint {
436                            place: Box::new(drop_node.data.local.into()),
437                            reason: BackwardIncompatibleDropReason::Edition2024,
438                        },
439                    );
440                    cfg.push(block, stmt);
441                    let target = blocks[drop_node.next].unwrap();
442                    if target != block {
443                        // Diagnostics don't use this `Span` but debuginfo
444                        // might. Since we don't want breakpoints to be placed
445                        // here, especially when this is on an unwind path, we
446                        // use `DUMMY_SP`.
447                        let source_info =
448                            SourceInfo { span: DUMMY_SP, ..drop_node.data.source_info };
449                        let terminator = TerminatorKind::goto(target);
450                        cfg.terminate(block, source_info, terminator);
451                    }
452                }
453                // Root nodes don't correspond to a drop.
454                DropKind::Storage if drop_idx == ROOT_NODE => {}
455                DropKind::Storage => {
456                    let stmt = Statement::new(
457                        drop_node.data.source_info,
458                        StatementKind::StorageDead(drop_node.data.local),
459                    );
460                    cfg.push(block, stmt);
461                    let target = blocks[drop_node.next].unwrap();
462                    if target != block {
463                        // Diagnostics don't use this `Span` but debuginfo
464                        // might. Since we don't want breakpoints to be placed
465                        // here, especially when this is on an unwind path, we
466                        // use `DUMMY_SP`.
467                        let source_info =
468                            SourceInfo { span: DUMMY_SP, ..drop_node.data.source_info };
469                        let terminator = TerminatorKind::goto(target);
470                        cfg.terminate(block, source_info, terminator);
471                    }
472                }
473            }
474        }
475    }
476}
477
478impl<'tcx> Scopes<'tcx> {
479    pub(crate) fn new() -> Self {
480        Self {
481            scopes: Vec::new(),
482            breakable_scopes: Vec::new(),
483            const_continuable_scopes: Vec::new(),
484            if_then_scope: None,
485            unwind_drops: DropTree::new(),
486            coroutine_drops: DropTree::new(),
487        }
488    }
489
490    fn push_scope(&mut self, region_scope: region::Scope, vis_scope: SourceScope) {
491        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs:491",
                        "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                        ::tracing_core::__macro_support::Option::Some(491u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("push_scope({0:?})",
                                                    region_scope) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("push_scope({:?})", region_scope);
492        self.scopes.push(Scope {
493            source_scope: vis_scope,
494            region_scope,
495            drops: ::alloc::vec::Vec::new()vec![],
496            moved_locals: ::alloc::vec::Vec::new()vec![],
497            cached_unwind_block: None,
498            cached_coroutine_drop_block: None,
499        });
500    }
501
502    fn pop_scope(&mut self, region_scope: region::Scope) {
503        let scope = self.scopes.pop().unwrap();
504        {
    match (&scope.region_scope, &region_scope) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(scope.region_scope, region_scope);
505    }
506
507    /// Returns the position in the scope stack of `region_scope`.
508    fn stack_index(&self, region_scope: region::Scope, span: Span) -> usize {
509        self.scopes
510            .iter()
511            .rposition(|scope| scope.region_scope == region_scope)
512            .unwrap_or_else(|| ::rustc_span::macros::bug_impl(Some(span),
    format_args!("region_scope {0:?} does not enclose", region_scope),
    Location::caller())span_bug!(span, "region_scope {:?} does not enclose", region_scope))
513    }
514
515    /// Returns the topmost active scope, which is known to be alive until
516    /// the next scope expression.
517    fn topmost(&self) -> region::Scope {
518        self.scopes.last().expect("topmost_scope: no scopes present").region_scope
519    }
520}
521
522/// Used by [`Builder::in_scope`] to create source scopes mapping from MIR back to HIR at points
523/// where lint levels change.
524#[derive(#[automatically_derived]impl ::core::marker::Copy for LintLevel { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for LintLevel {
}
#[automatically_derived]impl ::core::clone::Clone for LintLevel {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<HirId>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for LintLevel {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::Inherited =>
                ::core::fmt::Formatter::write_str(f, "Inherited"),
            Self::Explicit(__self_0) =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Explicit", &__self_0),
        }
    }
}Debug)]
525pub(crate) enum LintLevel {
526    Inherited,
527    Explicit(HirId),
528}
529
530impl<'a, 'tcx> Builder<'a, 'tcx> {
531    // Adding and removing scopes
532    // ==========================
533
534    ///  Start a breakable scope, which tracks where `continue`, `break` and
535    ///  `return` should branch to.
536    pub(crate) fn in_breakable_scope<F>(
537        &mut self,
538        loop_block: Option<BasicBlock>,
539        break_destination: Place<'tcx>,
540        span: Span,
541        f: F,
542    ) -> BlockAnd<()>
543    where
544        F: FnOnce(&mut Builder<'a, 'tcx>) -> Option<BlockAnd<()>>,
545    {
546        let region_scope = self.scopes.topmost();
547        let scope = BreakableScope {
548            region_scope,
549            break_destination,
550            break_drops: DropTree::new(),
551            continue_drops: loop_block.map(|_| DropTree::new()),
552        };
553        self.scopes.breakable_scopes.push(scope);
554        let normal_exit_block = f(self);
555        let breakable_scope = self.scopes.breakable_scopes.pop().unwrap();
556        if !(breakable_scope.region_scope == region_scope) {
    ::core::panicking::panic("assertion failed: breakable_scope.region_scope == region_scope")
};assert!(breakable_scope.region_scope == region_scope);
557        let break_block =
558            self.build_exit_tree(breakable_scope.break_drops, region_scope, span, None);
559        if let Some(drops) = breakable_scope.continue_drops {
560            self.build_exit_tree(drops, region_scope, span, loop_block);
561        }
562        match (normal_exit_block, break_block) {
563            (Some(block), None) | (None, Some(block)) => block,
564            (None, None) => self.cfg.start_new_block().unit(),
565            (Some(normal_block), Some(exit_block)) => {
566                let target = self.cfg.start_new_block();
567                let source_info = self.source_info(span);
568                self.cfg.terminate(
569                    normal_block.into_block(),
570                    source_info,
571                    TerminatorKind::goto(target),
572                );
573                self.cfg.terminate(
574                    exit_block.into_block(),
575                    source_info,
576                    TerminatorKind::goto(target),
577                );
578                target.unit()
579            }
580        }
581    }
582
583    /// Start a const-continuable scope, which tracks where `#[const_continue] break` should
584    /// branch to.
585    pub(crate) fn in_const_continuable_scope<F>(
586        &mut self,
587        arms: Box<[ArmId]>,
588        built_match_tree: BuiltMatchTree<'tcx>,
589        state_place: Place<'tcx>,
590        span: Span,
591        f: F,
592    ) -> BlockAnd<()>
593    where
594        F: FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<()>,
595    {
596        let region_scope = self.scopes.topmost();
597        let scope = ConstContinuableScope {
598            region_scope,
599            state_place,
600            const_continue_drops: DropTree::new(),
601            arms,
602            built_match_tree,
603        };
604        self.scopes.const_continuable_scopes.push(scope);
605        let normal_exit_block = f(self);
606        let const_continue_scope = self.scopes.const_continuable_scopes.pop().unwrap();
607        if !(const_continue_scope.region_scope == region_scope) {
    ::core::panicking::panic("assertion failed: const_continue_scope.region_scope == region_scope")
};assert!(const_continue_scope.region_scope == region_scope);
608
609        let break_block = self.build_exit_tree(
610            const_continue_scope.const_continue_drops,
611            region_scope,
612            span,
613            None,
614        );
615
616        match (normal_exit_block, break_block) {
617            (block, None) => block,
618            (normal_block, Some(exit_block)) => {
619                let target = self.cfg.start_new_block();
620                let source_info = self.source_info(span);
621                self.cfg.terminate(
622                    normal_block.into_block(),
623                    source_info,
624                    TerminatorKind::goto(target),
625                );
626                self.cfg.terminate(
627                    exit_block.into_block(),
628                    source_info,
629                    TerminatorKind::goto(target),
630                );
631                target.unit()
632            }
633        }
634    }
635
636    /// Start an if-then scope which tracks drop for `if` expressions and `if`
637    /// guards.
638    ///
639    /// For an if-let chain:
640    /// ```rust,ignore(illustrative)
641    ///     if let Some(x) = a && let Some(y) = b && let Some(z) = c { ... }
642    /// ```
643    /// There are three possible ways the condition can be false and we may have
644    /// to drop `x`, `x` and `y`, or neither depending on which binding fails.
645    /// To handle this correctly we use a `DropTree` in a similar way to a
646    /// `loop` expression and 'break' out on all of the 'else' paths.
647    ///
648    /// Notes:
649    /// - We don't need to keep a stack of scopes in the `Builder` because the
650    ///   'else' paths will only leave the innermost scope.
651    /// - This is also used for match guards.
652    ///
653    /// Returns blocks for the two condition outcomes, `(true_block, false_block)`.
654    pub(crate) fn in_if_then_scope(
655        &mut self,
656        region_scope: region::Scope,
657        span: Span,
658        // Closure that will lower the condition(s), register breaks, and return `true_block`.
659        f: impl FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<()>,
660    ) -> (BasicBlock, BasicBlock) {
661        let scope = IfThenScope { region_scope, else_drops: DropTree::new() };
662        let previous_scope = mem::replace(&mut self.scopes.if_then_scope, Some(scope));
663
664        let true_block = f(self).into_block();
665
666        let if_then_scope = mem::replace(&mut self.scopes.if_then_scope, previous_scope).unwrap();
667        if !(if_then_scope.region_scope == region_scope) {
    ::core::panicking::panic("assertion failed: if_then_scope.region_scope == region_scope")
};assert!(if_then_scope.region_scope == region_scope);
668
669        // Lower any break paths (where the condition was false)
670        // into a drop tree that ends in `false_block`.
671        let false_block = self
672            .build_exit_tree(if_then_scope.else_drops, region_scope, span, None)
673            .map(|false_block: BlockAnd<()>| false_block.into_block())
674            .unwrap_or_else(|| self.cfg.start_new_block());
675
676        (true_block, false_block)
677    }
678
679    /// Convenience wrapper that pushes a scope and then executes `f`
680    /// to build its contents, popping the scope afterwards.
681    {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("in_scope",
                                    "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                                    ::tracing_core::__macro_support::Option::Some(681u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("region_scope")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("region_scope");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("source_info")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("source_info");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("lint_level")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("lint_level");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&region_scope)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source_info)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&lint_level)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: BlockAnd<R> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let source_scope = self.source_scope;
            if let LintLevel::Explicit(current_hir_id) = lint_level {
                let parent_id =
                    self.source_scopes[source_scope].local_data.as_ref().unwrap_crate_local().lint_root;
                self.maybe_new_source_scope(source_info.span, current_hir_id,
                    parent_id);
            }
            self.push_scope(region_scope);
            let mut block;
            let rv = { let BlockAnd(b, v) = f(self); block = b; v };
            block = self.pop_scope(region_scope, block).into_block();
            self.source_scope = source_scope;
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event /rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs:702",
                                    "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                                    ::tracing_core::__macro_support::Option::Some(702u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("block")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("block");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&block)
                                                        as &dyn ::tracing::field::Value))])
                        });
                } else { ; }
            };
            block.and(rv)
        }
    }
}#[instrument(skip(self, f), level = "debug")]
682    pub(crate) fn in_scope<F, R>(
683        &mut self,
684        (region_scope, source_info): (region::Scope, SourceInfo),
685        lint_level: LintLevel,
686        f: F,
687    ) -> BlockAnd<R>
688    where
689        F: FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<R>,
690    {
691        let source_scope = self.source_scope;
692        if let LintLevel::Explicit(current_hir_id) = lint_level {
693            let parent_id =
694                self.source_scopes[source_scope].local_data.as_ref().unwrap_crate_local().lint_root;
695            self.maybe_new_source_scope(source_info.span, current_hir_id, parent_id);
696        }
697        self.push_scope(region_scope);
698        let mut block;
699        let rv = unpack!(block = f(self));
700        block = self.pop_scope(region_scope, block).into_block();
701        self.source_scope = source_scope;
702        debug!(?block);
703        block.and(rv)
704    }
705
706    /// Convenience wrapper that executes `f` either within the current scope or a new scope.
707    /// Used for pattern matching, which introduces an additional scope for patterns with guards.
708    pub(crate) fn opt_in_scope<R>(
709        &mut self,
710        opt_region_scope: Option<(region::Scope, SourceInfo)>,
711        f: impl FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<R>,
712    ) -> BlockAnd<R> {
713        if let Some(region_scope) = opt_region_scope {
714            self.in_scope(region_scope, LintLevel::Inherited, f)
715        } else {
716            f(self)
717        }
718    }
719
720    /// Push a scope onto the stack. You can then build code in this
721    /// scope and call `pop_scope` afterwards. Note that these two
722    /// calls must be paired; using `in_scope` as a convenience
723    /// wrapper maybe preferable.
724    pub(crate) fn push_scope(&mut self, region_scope: region::Scope) {
725        self.scopes.push_scope(region_scope, self.source_scope);
726    }
727
728    /// Pops a scope, which should have region scope `region_scope`,
729    /// adding any drops onto the end of `block` that are needed.
730    /// This must match 1-to-1 with `push_scope`.
731    pub(crate) fn pop_scope(
732        &mut self,
733        region_scope: region::Scope,
734        mut block: BasicBlock,
735    ) -> BlockAnd<()> {
736        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs:736",
                        "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                        ::tracing_core::__macro_support::Option::Some(736u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("pop_scope({0:?}, {1:?})",
                                                    region_scope, block) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("pop_scope({:?}, {:?})", region_scope, block);
737
738        block = self.leave_top_scope(block);
739        self.scopes.pop_scope(region_scope);
740
741        block.unit()
742    }
743
744    /// Sets up the drops for breaking from `block` to `target`.
745    pub(crate) fn break_scope(
746        &mut self,
747        mut block: BasicBlock,
748        value: Option<ExprId>,
749        target: BreakableTarget,
750        source_info: SourceInfo,
751    ) -> BlockAnd<()> {
752        let span = source_info.span;
753
754        let get_scope_index = |scope: region::Scope| {
755            // find the loop-scope by its `region::Scope`.
756            self.scopes
757                .breakable_scopes
758                .iter()
759                .rposition(|breakable_scope| breakable_scope.region_scope == scope)
760                .unwrap_or_else(|| ::rustc_span::macros::bug_impl(Some(span),
    format_args!("no enclosing breakable scope found"), Location::caller())span_bug!(span, "no enclosing breakable scope found"))
761        };
762        let (break_index, destination) = match target {
763            BreakableTarget::Return => {
764                let scope = &self.scopes.breakable_scopes[0];
765                if scope.break_destination != Place::return_place() {
766                    ::rustc_span::macros::bug_impl(Some(span),
    format_args!("`return` in item with no return scope"),
    Location::caller());span_bug!(span, "`return` in item with no return scope");
767                }
768                (0, Some(scope.break_destination))
769            }
770            BreakableTarget::Break(scope) => {
771                let break_index = get_scope_index(scope);
772                let scope = &self.scopes.breakable_scopes[break_index];
773                (break_index, Some(scope.break_destination))
774            }
775            BreakableTarget::Continue(scope) => {
776                let break_index = get_scope_index(scope);
777                (break_index, None)
778            }
779        };
780
781        match (destination, value) {
782            (Some(destination), Some(value)) => {
783                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs:783",
                        "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                        ::tracing_core::__macro_support::Option::Some(783u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("stmt_expr Break val block_context.push(SubExpr)")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("stmt_expr Break val block_context.push(SubExpr)");
784                self.block_context.push(BlockFrame::SubExpr);
785                block = self.expr_into_dest(destination, block, value).into_block();
786                self.block_context.pop();
787            }
788            (Some(destination), None) => {
789                self.cfg.push_assign_unit(block, source_info, destination, self.tcx)
790            }
791            (None, Some(_)) => {
792                {
    ::core::panicking::panic_fmt(format_args!("`return`, `become` and `break` with value and must have a destination"));
}panic!("`return`, `become` and `break` with value and must have a destination")
793            }
794            (None, None) => {}
795        }
796
797        let region_scope = self.scopes.breakable_scopes[break_index].region_scope;
798        let stack_index = self.scopes.stack_index(region_scope, span);
799        let drops = if destination.is_some() {
800            &mut self.scopes.breakable_scopes[break_index].break_drops
801        } else {
802            let Some(drops) = self.scopes.breakable_scopes[break_index].continue_drops.as_mut()
803            else {
804                self.tcx.dcx().span_delayed_bug(
805                    source_info.span,
806                    "unlabelled `continue` within labelled block",
807                );
808                self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
809
810                return self.cfg.start_new_block().unit();
811            };
812            drops
813        };
814
815        let mut drop_idx = ROOT_NODE;
816        for scope in &self.scopes.scopes[stack_index + 1..] {
817            for drop in &scope.drops {
818                drop_idx = drops.add_drop(*drop, drop_idx);
819            }
820        }
821        drops.add_entry_point(block, drop_idx);
822
823        // `build_drop_trees` doesn't have access to our source_info, so we
824        // create a dummy terminator now. `TerminatorKind::UnwindResume` is used
825        // because MIR type checking will panic if it hasn't been overwritten.
826        // (See `<ExitScopes as DropTreeBuilder>::link_entry_point`.)
827        self.cfg.terminate(block, source_info, TerminatorKind::UnwindResume);
828
829        self.cfg.start_new_block().unit()
830    }
831
832    /// Based on `FunctionCx::eval_unevaluated_mir_constant_to_valtree`.
833    fn eval_unevaluated_mir_constant_to_valtree(
834        &self,
835        constant: ConstOperand<'tcx>,
836    ) -> Result<(ty::ValTree<'tcx>, Ty<'tcx>), interpret::ErrorHandled> {
837        if !!constant.const_.ty().has_param() {
    ::core::panicking::panic("assertion failed: !constant.const_.ty().has_param()")
};assert!(!constant.const_.ty().has_param());
838        let (uv, ty) = match constant.const_ {
839            mir::Const::Unevaluated(uv, ty) => (uv.shrink(self.tcx), ty),
840            mir::Const::Ty(_, c) => match c.kind() {
841                // A constant that came from a const generic but was then used as an argument to
842                // old-style simd_shuffle (passing as argument instead of as a generic param).
843                ty::ConstKind::Value(cv) => return Ok((cv.valtree, cv.ty)),
844                other => ::rustc_span::macros::bug_impl(Some(constant.span),
    format_args!("{0:#?}", other), Location::caller())span_bug!(constant.span, "{other:#?}"),
845            },
846            mir::Const::Val(mir::ConstValue::Scalar(mir::interpret::Scalar::Int(val)), ty) => {
847                return Ok((ValTree::from_scalar_int(self.tcx, val), ty));
848            }
849            // We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
850            // a constant and write that value back into `Operand`s. This could happen, but is
851            // unlikely. Also: all users of `simd_shuffle` are on unstable and already need to take
852            // a lot of care around intrinsics. For an issue to happen here, it would require a
853            // macro expanding to a `simd_shuffle` call without wrapping the constant argument in a
854            // `const {}` block, but the user pass through arbitrary expressions.
855
856            // FIXME(oli-obk): Replace the magic const generic argument of `simd_shuffle` with a
857            // real const generic, and get rid of this entire function.
858            other => ::rustc_span::macros::bug_impl(Some(constant.span),
    format_args!("{0:#?}", other), Location::caller())span_bug!(constant.span, "{other:#?}"),
859        };
860
861        match self.tcx.const_eval_resolve_for_typeck(self.typing_env(), uv, constant.span) {
862            Ok(Ok(valtree)) => Ok((valtree, ty)),
863            Ok(Err(ty)) => ::rustc_span::macros::bug_impl(Some(constant.span),
    format_args!("could not convert {0:?} to a valtree", ty),
    Location::caller())span_bug!(constant.span, "could not convert {ty:?} to a valtree"),
864            Err(e) => Err(e),
865        }
866    }
867
868    /// Sets up the drops for jumping from `block` to `scope`.
869    pub(crate) fn break_const_continuable_scope(
870        &mut self,
871        mut block: BasicBlock,
872        value: ExprId,
873        scope: region::Scope,
874        source_info: SourceInfo,
875    ) -> BlockAnd<()> {
876        let span = source_info.span;
877
878        // A break can only break out of a scope, so the value should be a scope.
879        let rustc_middle::thir::ExprKind::Scope { value, .. } = self.thir[value].kind else {
880            ::rustc_span::macros::bug_impl(Some(span),
    format_args!("break value must be a scope"), Location::caller())span_bug!(span, "break value must be a scope")
881        };
882
883        let expr = &self.thir[value];
884        let constant = match &expr.kind {
885            ExprKind::Adt(AdtExpr { variant_index, fields, base, .. }) => {
886                if !#[allow(non_exhaustive_omitted_patterns)] match base {
            AdtExprBase::None => true,
            _ => false,
        } {
    ::core::panicking::panic("assertion failed: matches!(base, AdtExprBase::None)")
};assert!(matches!(base, AdtExprBase::None));
887                if !fields.is_empty() {
    ::core::panicking::panic("assertion failed: fields.is_empty()")
};assert!(fields.is_empty());
888                ConstOperand {
889                    span: self.thir[value].span,
890                    user_ty: None,
891                    const_: Const::Ty(
892                        self.thir[value].ty,
893                        ty::Const::new_value(
894                            self.tcx,
895                            ValTree::from_branches(
896                                self.tcx,
897                                [ty::Const::new_value(
898                                    self.tcx,
899                                    ValTree::from_scalar_int(
900                                        self.tcx,
901                                        variant_index.as_u32().into(),
902                                    ),
903                                    self.tcx.types.u32,
904                                )],
905                            ),
906                            self.thir[value].ty,
907                        ),
908                    ),
909                }
910            }
911
912            ExprKind::Literal { .. }
913            | ExprKind::NonHirLiteral { .. }
914            | ExprKind::ZstLiteral { .. }
915            | ExprKind::NamedConst { .. } => self.as_constant(&self.thir[value]),
916
917            other => {
918                use crate::diagnostics::ConstContinueNotMonomorphicConstReason as Reason;
919
920                let span = expr.span;
921                let reason = match other {
922                    ExprKind::ConstParam { .. } => Reason::ConstantParameter { span },
923                    ExprKind::ConstBlock { .. } => Reason::ConstBlock { span },
924                    _ => Reason::Other { span },
925                };
926
927                self.tcx
928                    .dcx()
929                    .emit_err(ConstContinueNotMonomorphicConst { span: expr.span, reason });
930                return block.unit();
931            }
932        };
933
934        let break_index = self
935            .scopes
936            .const_continuable_scopes
937            .iter()
938            .rposition(|const_continuable_scope| const_continuable_scope.region_scope == scope)
939            .unwrap_or_else(|| ::rustc_span::macros::bug_impl(Some(span),
    format_args!("no enclosing const-continuable scope found"),
    Location::caller())span_bug!(span, "no enclosing const-continuable scope found"));
940
941        let scope = &self.scopes.const_continuable_scopes[break_index];
942
943        let state_decl = &self.local_decls[scope.state_place.as_local().unwrap()];
944        let state_ty = state_decl.ty;
945        let (discriminant_ty, rvalue) = match state_ty.kind() {
946            ty::Adt(adt_def, _) if adt_def.is_enum() => {
947                (state_ty.discriminant_ty(self.tcx), Rvalue::Discriminant(scope.state_place))
948            }
949            ty::Uint(_) | ty::Int(_) | ty::Float(_) | ty::Bool | ty::Char => {
950                (state_ty, Rvalue::Use(Operand::Copy(scope.state_place), WithRetag::Yes))
951            }
952            _ => ::rustc_span::macros::bug_impl(Some(state_decl.source_info.span),
    format_args!("unsupported #[loop_match] state"), Location::caller())span_bug!(state_decl.source_info.span, "unsupported #[loop_match] state"),
953        };
954
955        // The `PatCtxt` is normally used in pattern exhaustiveness checking, but reused
956        // here because it performs normalization and const evaluation.
957        let dropless_arena = rustc_arena::DroplessArena::default();
958        let typeck_results = self.tcx.typeck(self.def_id);
959        let cx = RustcPatCtxt {
960            tcx: self.tcx,
961            typeck_results,
962            module: self.tcx.parent_module(self.hir_id),
963            typing_env: ty::TypingEnv::post_typeck_through_borrowck_for_mir_build(
964                self.tcx,
965                self.def_id,
966            ),
967            dropless_arena: &dropless_arena,
968            match_lint_level: self.hir_id,
969            whole_match_span: Some(rustc_span::Span::default()),
970            scrut_span: rustc_span::Span::default(),
971            refutable: true,
972            known_valid_scrutinee: true,
973            internal_state: Default::default(),
974        };
975
976        let valtree = match self.eval_unevaluated_mir_constant_to_valtree(constant) {
977            Ok((valtree, ty)) => {
978                // Defensively check that the type is monomorphic.
979                if !!ty.has_param() {
    ::core::panicking::panic("assertion failed: !ty.has_param()")
};assert!(!ty.has_param());
980
981                valtree
982            }
983            Err(ErrorHandled::Reported(..)) => {
984                return block.unit();
985            }
986            Err(ErrorHandled::TooGeneric(_)) => {
987                self.tcx.dcx().emit_fatal(ConstContinueBadConst { span: constant.span });
988            }
989        };
990
991        let Some(real_target) =
992            self.static_pattern_match(&cx, valtree, &*scope.arms, &scope.built_match_tree)
993        else {
994            self.tcx.dcx().emit_fatal(ConstContinueUnknownJumpTarget { span })
995        };
996
997        self.block_context.push(BlockFrame::SubExpr);
998        let state_place = scope.state_place;
999        block = self.expr_into_dest(state_place, block, value).into_block();
1000        self.block_context.pop();
1001
1002        let discr = self.temp(discriminant_ty, source_info.span);
1003        let stack_index = self
1004            .scopes
1005            .stack_index(self.scopes.const_continuable_scopes[break_index].region_scope, span);
1006        let scope = &mut self.scopes.const_continuable_scopes[break_index];
1007        self.cfg.push_assign(block, source_info, discr, rvalue);
1008        let drop_and_continue_block = self.cfg.start_new_block();
1009        let imaginary_target = self.cfg.start_new_block();
1010        self.cfg.terminate(
1011            block,
1012            source_info,
1013            TerminatorKind::FalseEdge { real_target: drop_and_continue_block, imaginary_target },
1014        );
1015
1016        let drops = &mut scope.const_continue_drops;
1017
1018        let drop_idx = self.scopes.scopes[stack_index + 1..]
1019            .iter()
1020            .flat_map(|scope| &scope.drops)
1021            .fold(ROOT_NODE, |drop_idx, &drop| drops.add_drop(drop, drop_idx));
1022
1023        drops.add_entry_point(imaginary_target, drop_idx);
1024
1025        self.cfg.terminate(imaginary_target, source_info, TerminatorKind::UnwindResume);
1026
1027        let region_scope = scope.region_scope;
1028        let stack_index = self.scopes.stack_index(region_scope, span);
1029        let mut drops = DropTree::new();
1030
1031        let drop_idx = self.scopes.scopes[stack_index + 1..]
1032            .iter()
1033            .flat_map(|scope| &scope.drops)
1034            .fold(ROOT_NODE, |drop_idx, &drop| drops.add_drop(drop, drop_idx));
1035
1036        drops.add_entry_point(drop_and_continue_block, drop_idx);
1037
1038        // `build_drop_trees` doesn't have access to our source_info, so we
1039        // create a dummy terminator now. `TerminatorKind::UnwindResume` is used
1040        // because MIR type checking will panic if it hasn't been overwritten.
1041        // (See `<ExitScopes as DropTreeBuilder>::link_entry_point`.)
1042        self.cfg.terminate(drop_and_continue_block, source_info, TerminatorKind::UnwindResume);
1043
1044        self.build_exit_tree(drops, region_scope, span, Some(real_target));
1045
1046        return self.cfg.start_new_block().unit();
1047    }
1048
1049    /// Breaks out of the enclosing [`Builder::in_if_then_scope`] due to a
1050    /// condition being false.
1051    ///
1052    /// This adds relevant drops in the drop tree, and adds a dummy terminator
1053    /// that will become a real `goto` when the scope's drop tree is built.
1054    ///
1055    /// Must be called in the context of [`Builder::in_if_then_scope`], so that
1056    /// there is an if-then scope to tell us what the target scope is.
1057    pub(crate) fn break_from_if_then_scope(&mut self, block: BasicBlock, source_info: SourceInfo) {
1058        let if_then_scope = self
1059            .scopes
1060            .if_then_scope
1061            .as_ref()
1062            .unwrap_or_else(|| ::rustc_span::macros::bug_impl(Some(source_info.span),
    format_args!("no if-then scope found"), Location::caller())span_bug!(source_info.span, "no if-then scope found"));
1063
1064        let target = if_then_scope.region_scope;
1065        let stack_index = self.scopes.stack_index(target, source_info.span);
1066
1067        // Upgrade `if_then_scope` to `&mut`.
1068        let if_then_scope = self.scopes.if_then_scope.as_mut().expect("upgrading & to &mut");
1069
1070        let mut drop_idx = ROOT_NODE;
1071        let drops = &mut if_then_scope.else_drops;
1072        for scope in &self.scopes.scopes[stack_index + 1..] {
1073            for drop in &scope.drops {
1074                drop_idx = drops.add_drop(*drop, drop_idx);
1075            }
1076        }
1077        drops.add_entry_point(block, drop_idx);
1078
1079        // `build_drop_trees` doesn't have access to our source_info, so we
1080        // create a dummy terminator now. `TerminatorKind::UnwindResume` is used
1081        // because MIR type checking will panic if it hasn't been overwritten.
1082        // (See `<ExitScopes as DropTreeBuilder>::link_entry_point`.)
1083        self.cfg.terminate(block, source_info, TerminatorKind::UnwindResume);
1084    }
1085
1086    /// Sets up the drops for explicit tail calls.
1087    ///
1088    /// Unlike other kinds of early exits, tail calls do not go through the drop tree.
1089    /// Instead, all scheduled drops are immediately added to the CFG.
1090    pub(crate) fn break_for_tail_call(
1091        &mut self,
1092        mut block: BasicBlock,
1093        args: &[Spanned<Operand<'tcx>>],
1094        source_info: SourceInfo,
1095    ) -> BlockAnd<()> {
1096        let arg_drops: Vec<_> = args
1097            .iter()
1098            .rev()
1099            .filter_map(|arg| match &arg.node {
1100                Operand::Copy(_) => ::rustc_span::macros::bug_impl(None,
    format_args!("copy op in tail call args"), Location::caller())bug!("copy op in tail call args"),
1101                Operand::Move(place) => {
1102                    let local =
1103                        place.as_local().unwrap_or_else(|| ::rustc_span::macros::bug_impl(None,
    format_args!("projection in tail call args"), Location::caller())bug!("projection in tail call args"));
1104
1105                    if !self.local_decls[local].ty.needs_drop(self.tcx, self.typing_env()) {
1106                        return None;
1107                    }
1108
1109                    Some(DropData { source_info, local, kind: DropKind::Value })
1110                }
1111                Operand::Constant(_) | Operand::RuntimeChecks(_) => None,
1112            })
1113            .collect();
1114
1115        let mut unwind_to = self.diverge_cleanup_target(
1116            self.scopes.scopes.iter().rev().nth(1).unwrap().region_scope,
1117            DUMMY_SP,
1118        );
1119        let typing_env = self.typing_env();
1120        let unwind_drops = &mut self.scopes.unwind_drops;
1121
1122        // the innermost scope contains only the destructors for the tail call arguments
1123        // we only want to drop these in case of a panic, so we skip it
1124        for scope in self.scopes.scopes[1..].iter().rev().skip(1) {
1125            // FIXME(explicit_tail_calls) code duplication with `build_scope_drops`
1126            for drop_data in scope.drops.iter().rev() {
1127                let source_info = drop_data.source_info;
1128                let local = drop_data.local;
1129
1130                if !self.local_decls[local].ty.needs_drop(self.tcx, typing_env) {
1131                    continue;
1132                }
1133
1134                match drop_data.kind {
1135                    DropKind::Value => {
1136                        // `unwind_to` should drop the value that we're about to
1137                        // schedule. If dropping this value panics, then we continue
1138                        // with the *next* value on the unwind path.
1139                        if true {
    {
        match (&unwind_drops.drop_nodes[unwind_to].data.local,
                &drop_data.local) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(
1140                            unwind_drops.drop_nodes[unwind_to].data.local,
1141                            drop_data.local
1142                        );
1143                        if true {
    {
        match (&unwind_drops.drop_nodes[unwind_to].data.kind, &drop_data.kind)
            {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(
1144                            unwind_drops.drop_nodes[unwind_to].data.kind,
1145                            drop_data.kind
1146                        );
1147                        unwind_to = unwind_drops.drop_nodes[unwind_to].next;
1148
1149                        let mut unwind_entry_point = unwind_to;
1150
1151                        // the tail call arguments must be dropped if any of these drops panic
1152                        for drop in arg_drops.iter().copied() {
1153                            unwind_entry_point = unwind_drops.add_drop(drop, unwind_entry_point);
1154                        }
1155
1156                        unwind_drops.add_entry_point(block, unwind_entry_point);
1157
1158                        let next = self.cfg.start_new_block();
1159                        self.cfg.terminate(
1160                            block,
1161                            source_info,
1162                            TerminatorKind::Drop {
1163                                place: local.into(),
1164                                target: next,
1165                                unwind: UnwindAction::Continue,
1166                                replace: false,
1167                                drop: None,
1168                            },
1169                        );
1170                        block = next;
1171                    }
1172                    DropKind::ForLint => {
1173                        self.cfg.push(
1174                            block,
1175                            Statement::new(
1176                                source_info,
1177                                StatementKind::BackwardIncompatibleDropHint {
1178                                    place: Box::new(local.into()),
1179                                    reason: BackwardIncompatibleDropReason::Edition2024,
1180                                },
1181                            ),
1182                        );
1183                    }
1184                    DropKind::Storage => {
1185                        // Only temps and vars need their storage dead.
1186                        if !(local.index() > self.arg_count) {
    ::core::panicking::panic("assertion failed: local.index() > self.arg_count")
};assert!(local.index() > self.arg_count);
1187                        self.cfg.push(
1188                            block,
1189                            Statement::new(source_info, StatementKind::StorageDead(local)),
1190                        );
1191                    }
1192                }
1193            }
1194        }
1195
1196        block.unit()
1197    }
1198
1199    fn is_async_drop_impl(
1200        tcx: TyCtxt<'tcx>,
1201        local_decls: &IndexVec<Local, LocalDecl<'tcx>>,
1202        typing_env: ty::TypingEnv<'tcx>,
1203        local: Local,
1204    ) -> bool {
1205        let ty = local_decls[local].ty;
1206        if ty.is_async_drop(tcx, typing_env) || ty.is_coroutine() {
1207            return true;
1208        }
1209        ty.needs_async_drop(tcx, typing_env)
1210    }
1211    fn is_async_drop(&self, local: Local) -> bool {
1212        Self::is_async_drop_impl(self.tcx, &self.local_decls, self.typing_env(), local)
1213    }
1214
1215    fn leave_top_scope(&mut self, block: BasicBlock) -> BasicBlock {
1216        // If we are emitting a `drop` statement, we need to have the cached
1217        // diverge cleanup pads ready in case that drop panics.
1218        let needs_cleanup = self.scopes.scopes.last().is_some_and(|scope| scope.needs_cleanup());
1219        let is_coroutine = self.coroutine.is_some();
1220        let unwind_to = if needs_cleanup { self.diverge_cleanup() } else { DropIdx::MAX };
1221
1222        let scope = self.scopes.scopes.last().expect("leave_top_scope called with no scopes");
1223        let has_async_drops = is_coroutine
1224            && scope.drops.iter().any(|v| v.kind == DropKind::Value && self.is_async_drop(v.local));
1225        let dropline_to = if has_async_drops { Some(self.diverge_dropline()) } else { None };
1226        let scope = self.scopes.scopes.last().expect("leave_top_scope called with no scopes");
1227        let typing_env = self.typing_env();
1228        build_scope_drops(
1229            &mut self.cfg,
1230            &mut self.scopes.unwind_drops,
1231            &mut self.scopes.coroutine_drops,
1232            scope,
1233            block,
1234            unwind_to,
1235            dropline_to,
1236            is_coroutine && needs_cleanup,
1237            self.arg_count,
1238            |v: Local| Self::is_async_drop_impl(self.tcx, &self.local_decls, typing_env, v),
1239        )
1240        .into_block()
1241    }
1242
1243    /// Possibly creates a new source scope if `current_root` and `parent_root`
1244    /// are different, or if -Zmaximal-hir-to-mir-coverage is enabled.
1245    pub(crate) fn maybe_new_source_scope(
1246        &mut self,
1247        span: Span,
1248        current_id: HirId,
1249        parent_id: HirId,
1250    ) {
1251        let (current_root, parent_root) =
1252            if self.tcx.sess.opts.unstable_opts.maximal_hir_to_mir_coverage {
1253                // Some consumers of rustc need to map MIR locations back to HIR nodes. Currently
1254                // the only part of rustc that tracks MIR -> HIR is the
1255                // `SourceScopeLocalData::lint_root` field that tracks lint levels for MIR
1256                // locations. Normally the number of source scopes is limited to the set of nodes
1257                // with lint annotations. The -Zmaximal-hir-to-mir-coverage flag changes this
1258                // behavior to maximize the number of source scopes, increasing the granularity of
1259                // the MIR->HIR mapping.
1260                (current_id, parent_id)
1261            } else {
1262                // Use `maybe_lint_level_root_bounded` to avoid adding Hir dependencies on our
1263                // parents. We estimate the true lint roots here to avoid creating a lot of source
1264                // scopes.
1265                (
1266                    self.maybe_lint_level_root_bounded(current_id),
1267                    if parent_id == self.hir_id {
1268                        parent_id // this is very common
1269                    } else {
1270                        self.maybe_lint_level_root_bounded(parent_id)
1271                    },
1272                )
1273            };
1274
1275        if current_root != parent_root {
1276            let lint_level = LintLevel::Explicit(current_root);
1277            self.source_scope = self.new_source_scope(span, lint_level);
1278        }
1279    }
1280
1281    /// Walks upwards from `orig_id` to find a node which might change lint levels with attributes.
1282    /// It stops at `self.hir_id` and just returns it if reached.
1283    fn maybe_lint_level_root_bounded(&mut self, orig_id: HirId) -> HirId {
1284        // This assertion lets us just store `ItemLocalId` in the cache, rather
1285        // than the full `HirId`.
1286        {
    match (&orig_id.owner, &self.hir_id.owner) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(orig_id.owner, self.hir_id.owner);
1287
1288        let mut id = orig_id;
1289        loop {
1290            if id == self.hir_id {
1291                // This is a moderately common case, mostly hit for previously unseen nodes.
1292                break;
1293            }
1294            if {
        {
            'done:
                {
                for i in ::rustc_attr_ir::HasAttrs::get_attrs(id, &self.tcx) {
                    #[allow(unused_imports)]
                    use ::rustc_attr_ir::AttributeKind::*;
                    let i: &::rustc_attr_ir::Attribute = i;
                    match i {
                        ::rustc_attr_ir::Attribute::Parsed(LintCheck(_)) => {
                            break 'done Some(());
                        }
                        ::rustc_attr_ir::Attribute::Tool(..) |
                            ::rustc_attr_ir::Attribute::DeriveHelper(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }.is_some()find_attr!(self.tcx, id, LintCheck(_)) {
1295                // This is a rare case. It's for a node path that doesn't reach the root due to an
1296                // intervening lint level attribute. This result doesn't get cached.
1297                return id;
1298            }
1299
1300            let next = self.tcx.parent_hir_id(id);
1301            if next == id {
1302                ::rustc_span::macros::bug_impl(None,
    format_args!("lint traversal reached the root of the crate"),
    Location::caller());bug!("lint traversal reached the root of the crate");
1303            }
1304            id = next;
1305
1306            // This lookup is just an optimization; it can be removed without affecting
1307            // functionality. It might seem strange to see this at the end of this loop, but the
1308            // `orig_id` passed in to this function is almost always previously unseen, for which a
1309            // lookup will be a miss. So we only do lookups for nodes up the parent chain, where
1310            // cache lookups have a very high hit rate.
1311            if self.lint_level_roots_cache.contains(id.local_id) {
1312                break;
1313            }
1314        }
1315
1316        // `orig_id` traced to `self_id`; record this fact. If `orig_id` is a leaf node it will
1317        // rarely (never?) subsequently be searched for, but it's hard to know if that is the case.
1318        // The performance wins from the cache all come from caching non-leaf nodes.
1319        self.lint_level_roots_cache.insert(orig_id.local_id);
1320        self.hir_id
1321    }
1322
1323    /// Creates a new source scope, nested in the current one.
1324    pub(crate) fn new_source_scope(&mut self, span: Span, lint_level: LintLevel) -> SourceScope {
1325        let parent = self.source_scope;
1326        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs:1326",
                        "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                        ::tracing_core::__macro_support::Option::Some(1326u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("new_source_scope({0:?}, {1:?}) - parent({2:?})={3:?}",
                                                    span, lint_level, parent, self.source_scopes.get(parent)) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
1327            "new_source_scope({:?}, {:?}) - parent({:?})={:?}",
1328            span,
1329            lint_level,
1330            parent,
1331            self.source_scopes.get(parent)
1332        );
1333        let scope_local_data = SourceScopeLocalData {
1334            lint_root: if let LintLevel::Explicit(lint_root) = lint_level {
1335                lint_root
1336            } else {
1337                self.source_scopes[parent].local_data.as_ref().unwrap_crate_local().lint_root
1338            },
1339        };
1340        self.source_scopes.push(SourceScopeData {
1341            span,
1342            parent_scope: Some(parent),
1343            inlined: None,
1344            inlined_parent_scope: None,
1345            local_data: ClearCrossCrate::Set(scope_local_data),
1346        })
1347    }
1348
1349    /// Given a span and the current source scope, make a SourceInfo.
1350    pub(crate) fn source_info(&self, span: Span) -> SourceInfo {
1351        SourceInfo { span, scope: self.source_scope }
1352    }
1353
1354    // Finding scopes
1355    // ==============
1356
1357    /// Returns the scope that we should use as the lifetime of an
1358    /// operand. Basically, an operand must live until it is consumed.
1359    /// This is similar to, but not quite the same as, the temporary
1360    /// scope (which can be larger or smaller).
1361    ///
1362    /// Consider:
1363    /// ```ignore (illustrative)
1364    /// let x = foo(bar(X, Y));
1365    /// ```
1366    /// We wish to pop the storage for X and Y after `bar()` is
1367    /// called, not after the whole `let` is completed.
1368    ///
1369    /// As another example, if the second argument diverges:
1370    /// ```ignore (illustrative)
1371    /// foo(Box::new(2), panic!())
1372    /// ```
1373    /// We would allocate the box but then free it on the unwinding
1374    /// path; we would also emit a free on the 'success' path from
1375    /// panic, but that will turn out to be removed as dead-code.
1376    pub(crate) fn local_scope(&self) -> region::Scope {
1377        self.scopes.topmost()
1378    }
1379
1380    // Scheduling drops
1381    // ================
1382
1383    /// Indicates that `place` should be dropped on exit from `region_scope`.
1384    ///
1385    /// When called with `DropKind::Storage`, `place` shouldn't be the return
1386    /// place, or a function parameter.
1387    fn schedule_drop(
1388        &mut self,
1389        span: Span,
1390        region_scope: region::Scope,
1391        local: Local,
1392        drop_kind: DropKind,
1393    ) {
1394        // When building drops, we try to cache chains of drops to reduce the
1395        // number of `DropTree::add_drop` calls. This, however, means that
1396        // whenever we add a drop into a scope which already had some entries
1397        // in the drop tree built (and thus, cached) for it, we must invalidate
1398        // all caches which might branch into the scope which had a drop just
1399        // added to it. This is necessary, because otherwise some other code
1400        // might use the cache to branch into already built chain of drops,
1401        // essentially ignoring the newly added drop.
1402        //
1403        // For example consider there’s two scopes with a drop in each. These
1404        // are built and thus the caches are filled:
1405        //
1406        // +--------------------------------------------------------+
1407        // | +---------------------------------+                    |
1408        // | | +--------+     +-------------+  |  +---------------+ |
1409        // | | | return | <-+ | drop(outer) | <-+ |  drop(middle) | |
1410        // | | +--------+     +-------------+  |  +---------------+ |
1411        // | +------------|outer_scope cache|--+                    |
1412        // +------------------------------|middle_scope cache|------+
1413        //
1414        // Now, a new, innermost scope is added along with a new drop into
1415        // both innermost and outermost scopes:
1416        //
1417        // +------------------------------------------------------------+
1418        // | +----------------------------------+                       |
1419        // | | +--------+      +-------------+  |   +---------------+   | +-------------+
1420        // | | | return | <+   | drop(new)   | <-+  |  drop(middle) | <--+| drop(inner) |
1421        // | | +--------+  |   | drop(outer) |  |   +---------------+   | +-------------+
1422        // | |             +-+ +-------------+  |                       |
1423        // | +---|invalid outer_scope cache|----+                       |
1424        // +----=----------------|invalid middle_scope cache|-----------+
1425        //
1426        // If, when adding `drop(new)` we do not invalidate the cached blocks for both
1427        // outer_scope and middle_scope, then, when building drops for the inner (rightmost)
1428        // scope, the old, cached blocks, without `drop(new)` will get used, producing the
1429        // wrong results.
1430        //
1431        // Note that this code iterates scopes from the innermost to the outermost,
1432        // invalidating caches of each scope visited. This way bare minimum of the
1433        // caches gets invalidated. i.e., if a new drop is added into the middle scope, the
1434        // cache of outer scope stays intact.
1435        //
1436        // Since we only cache drops for the unwind path and the coroutine drop
1437        // path, we only need to invalidate the cache for drops that happen on
1438        // the unwind or coroutine drop paths. This means that for
1439        // non-coroutines we don't need to invalidate caches for `DropKind::Storage`.
1440        let invalidate_caches = match drop_kind {
1441            DropKind::Value | DropKind::ForLint => true,
1442            DropKind::Storage => self.coroutine.is_some(),
1443        };
1444        for scope in self.scopes.scopes.iter_mut().rev() {
1445            if invalidate_caches {
1446                scope.invalidate_cache();
1447            }
1448
1449            if scope.region_scope == region_scope {
1450                let region_scope_span = region_scope.span(self.tcx, self.region_scope_tree);
1451                // Attribute scope exit drops to scope's closing brace.
1452                let scope_end = self.tcx.sess.source_map().end_point(region_scope_span);
1453
1454                scope.drops.push(DropData {
1455                    source_info: SourceInfo { span: scope_end, scope: scope.source_scope },
1456                    local,
1457                    kind: drop_kind,
1458                });
1459
1460                return;
1461            }
1462        }
1463
1464        ::rustc_span::macros::bug_impl(Some(span),
    format_args!("region scope {0:?} not in scope to drop {1:?}",
        region_scope, local), Location::caller());span_bug!(span, "region scope {:?} not in scope to drop {:?}", region_scope, local);
1465    }
1466
1467    /// Indicates that `place` should be marked `StorageDead` on exit from `region_scope`.
1468    ///
1469    /// `place` must not be the return place, or a function parameter.
1470    pub(crate) fn schedule_drop_storage(
1471        &mut self,
1472        span: Span,
1473        region_scope: region::Scope,
1474        local: Local,
1475    ) {
1476        if local.index() <= self.arg_count {
1477            ::rustc_span::macros::bug_impl(Some(span),
    format_args!("`schedule_drop` called with body argument {0:?} but its storage does not require a drop",
        local), Location::caller())span_bug!(
1478                span,
1479                "`schedule_drop` called with body argument {:?} \
1480                but its storage does not require a drop",
1481                local,
1482            )
1483        }
1484        self.schedule_drop(span, region_scope, local, DropKind::Storage);
1485    }
1486
1487    /// Indicates that `place` should be dropped on exit from `region_scope`.
1488    pub(crate) fn schedule_drop_value(
1489        &mut self,
1490        span: Span,
1491        region_scope: region::Scope,
1492        local: Local,
1493    ) {
1494        if !self.local_decls[local].ty.needs_drop(self.tcx, self.typing_env()) {
1495            return;
1496        }
1497        self.schedule_drop(span, region_scope, local, DropKind::Value);
1498    }
1499
1500    /// Schedule emission of a backwards incompatible drop lint hint.
1501    /// Applicable only to temporary values for now.
1502    {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("schedule_backwards_incompatible_drop",
                                    "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1502u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("span")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("span");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("region_scope")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("region_scope");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("local")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("local");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&region_scope)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&local)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        { self.schedule_drop(span, region_scope, local, DropKind::ForLint); }
    }
}#[instrument(level = "debug", skip(self))]
1503    pub(crate) fn schedule_backwards_incompatible_drop(
1504        &mut self,
1505        span: Span,
1506        region_scope: region::Scope,
1507        local: Local,
1508    ) {
1509        // Note that we are *not* gating BIDs here on whether they have significant destructor.
1510        // We need to know all of them so that we can capture potential borrow-checking errors.
1511        self.schedule_drop(span, region_scope, local, DropKind::ForLint);
1512    }
1513
1514    /// Indicates that the "local operand" stored in `local` is
1515    /// *moved* at some point during execution (see `local_scope` for
1516    /// more information about what a "local operand" is -- in short,
1517    /// it's an intermediate operand created as part of preparing some
1518    /// MIR instruction). We use this information to suppress
1519    /// redundant drops on the non-unwind paths. This results in less
1520    /// MIR, but also avoids spurious borrow check errors
1521    /// (c.f. #64391).
1522    ///
1523    /// Example: when compiling the call to `foo` here:
1524    ///
1525    /// ```ignore (illustrative)
1526    /// foo(bar(), ...)
1527    /// ```
1528    ///
1529    /// we would evaluate `bar()` to an operand `_X`. We would also
1530    /// schedule `_X` to be dropped when the expression scope for
1531    /// `foo(bar())` is exited. This is relevant, for example, if the
1532    /// later arguments should unwind (it would ensure that `_X` gets
1533    /// dropped). However, if no unwind occurs, then `_X` will be
1534    /// unconditionally consumed by the `call`:
1535    ///
1536    /// ```ignore (illustrative)
1537    /// bb {
1538    ///   ...
1539    ///   _R = CALL(foo, _X, ...)
1540    /// }
1541    /// ```
1542    ///
1543    /// However, `_X` is still registered to be dropped, and so if we
1544    /// do nothing else, we would generate a `DROP(_X)` that occurs
1545    /// after the call. This will later be optimized out by the
1546    /// drop-elaboration code, but in the meantime it can lead to
1547    /// spurious borrow-check errors -- the problem, ironically, is
1548    /// not the `DROP(_X)` itself, but the (spurious) unwind pathways
1549    /// that it creates. See #64391 for an example.
1550    pub(crate) fn record_operands_moved(&mut self, operands: &[Spanned<Operand<'tcx>>]) {
1551        let local_scope = self.local_scope();
1552        let scope = self.scopes.scopes.last_mut().unwrap();
1553
1554        {
    match (&scope.region_scope, &local_scope) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("local scope is not the topmost scope!")));
            }
        }
    }
};assert_eq!(scope.region_scope, local_scope, "local scope is not the topmost scope!",);
1555
1556        // look for moves of a local variable, like `MOVE(_X)`
1557        let locals_moved = operands.iter().flat_map(|operand| match operand.node {
1558            Operand::Copy(_) | Operand::Constant(_) | Operand::RuntimeChecks(_) => None,
1559            Operand::Move(place) => place.as_local(),
1560        });
1561
1562        for local in locals_moved {
1563            // check if we have a Drop for this operand and -- if so
1564            // -- add it to the list of moved operands. Note that this
1565            // local might not have been an operand created for this
1566            // call, it could come from other places too.
1567            if scope.drops.iter().any(|drop| drop.local == local && drop.kind == DropKind::Value) {
1568                scope.moved_locals.push(local);
1569            }
1570        }
1571    }
1572
1573    // Other
1574    // =====
1575
1576    /// Returns the [DropIdx] for the innermost drop if the function unwound at
1577    /// this point. The `DropIdx` will be created if it doesn't already exist.
1578    fn diverge_cleanup(&mut self) -> DropIdx {
1579        // It is okay to use dummy span because the getting scope index on the topmost scope
1580        // must always succeed.
1581        self.diverge_cleanup_target(self.scopes.topmost(), DUMMY_SP)
1582    }
1583
1584    /// This is similar to [diverge_cleanup](Self::diverge_cleanup) except its target is set to
1585    /// some ancestor scope instead of the current scope.
1586    /// It is possible to unwind to some ancestor scope if some drop panics as
1587    /// the program breaks out of a if-then scope.
1588    fn diverge_cleanup_target(&mut self, target_scope: region::Scope, span: Span) -> DropIdx {
1589        let target = self.scopes.stack_index(target_scope, span);
1590        let (uncached_scope, mut cached_drop) = self.scopes.scopes[..=target]
1591            .iter()
1592            .enumerate()
1593            .rev()
1594            .find_map(|(scope_idx, scope)| {
1595                scope.cached_unwind_block.map(|cached_block| (scope_idx + 1, cached_block))
1596            })
1597            .unwrap_or((0, ROOT_NODE));
1598
1599        if uncached_scope > target {
1600            return cached_drop;
1601        }
1602
1603        let is_coroutine = self.coroutine.is_some();
1604        for scope in &mut self.scopes.scopes[uncached_scope..=target] {
1605            for drop in &scope.drops {
1606                if is_coroutine || drop.kind == DropKind::Value {
1607                    cached_drop = self.scopes.unwind_drops.add_drop(*drop, cached_drop);
1608                }
1609            }
1610            scope.cached_unwind_block = Some(cached_drop);
1611        }
1612
1613        cached_drop
1614    }
1615
1616    /// Prepares to create a path that performs all required cleanup for a
1617    /// terminator that can unwind at the given basic block.
1618    ///
1619    /// This path terminates in Resume. The path isn't created until after all
1620    /// of the non-unwind paths in this item have been lowered.
1621    pub(crate) fn diverge_from(&mut self, start: BasicBlock) {
1622        if true {
    if !#[allow(non_exhaustive_omitted_patterns)] match self.cfg.assert_terminator(start).kind
                {
                TerminatorKind::Assert { .. } | TerminatorKind::Call { .. } |
                    TerminatorKind::Drop { .. } | TerminatorKind::FalseUnwind {
                    .. } | TerminatorKind::InlineAsm { .. } => true,
                _ => false,
            } {
        {
            ::core::panicking::panic_fmt(format_args!("diverge_from called on block with terminator that cannot unwind."));
        }
    };
};debug_assert!(
1623            matches!(
1624                self.cfg.assert_terminator(start).kind,
1625                TerminatorKind::Assert { .. }
1626                    | TerminatorKind::Call { .. }
1627                    | TerminatorKind::Drop { .. }
1628                    | TerminatorKind::FalseUnwind { .. }
1629                    | TerminatorKind::InlineAsm { .. }
1630            ),
1631            "diverge_from called on block with terminator that cannot unwind."
1632        );
1633
1634        let next_drop = self.diverge_cleanup();
1635        self.scopes.unwind_drops.add_entry_point(start, next_drop);
1636    }
1637
1638    /// Returns the [DropIdx] for the innermost drop for dropline (coroutine drop path).
1639    /// The `DropIdx` will be created if it doesn't already exist.
1640    fn diverge_dropline(&mut self) -> DropIdx {
1641        // It is okay to use dummy span because the getting scope index on the topmost scope
1642        // must always succeed.
1643        self.diverge_dropline_target(self.scopes.topmost(), DUMMY_SP)
1644    }
1645
1646    /// Similar to diverge_cleanup_target, but for dropline (coroutine drop path)
1647    fn diverge_dropline_target(&mut self, target_scope: region::Scope, span: Span) -> DropIdx {
1648        if true {
    if !self.coroutine.is_some() {
        {
            ::core::panicking::panic_fmt(format_args!("diverge_dropline_target is valid only for coroutine"));
        }
    };
};debug_assert!(
1649            self.coroutine.is_some(),
1650            "diverge_dropline_target is valid only for coroutine"
1651        );
1652        let target = self.scopes.stack_index(target_scope, span);
1653        let (uncached_scope, mut cached_drop) = self.scopes.scopes[..=target]
1654            .iter()
1655            .enumerate()
1656            .rev()
1657            .find_map(|(scope_idx, scope)| {
1658                scope.cached_coroutine_drop_block.map(|cached_block| (scope_idx + 1, cached_block))
1659            })
1660            .unwrap_or((0, ROOT_NODE));
1661
1662        if uncached_scope > target {
1663            return cached_drop;
1664        }
1665
1666        for scope in &mut self.scopes.scopes[uncached_scope..=target] {
1667            for drop in &scope.drops {
1668                cached_drop = self.scopes.coroutine_drops.add_drop(*drop, cached_drop);
1669            }
1670            scope.cached_coroutine_drop_block = Some(cached_drop);
1671        }
1672
1673        cached_drop
1674    }
1675
1676    /// Sets up a path that performs all required cleanup for dropping a
1677    /// coroutine, starting from the given block that ends in
1678    /// [TerminatorKind::Yield].
1679    ///
1680    /// This path terminates in CoroutineDrop.
1681    pub(crate) fn coroutine_drop_cleanup(&mut self, yield_block: BasicBlock) {
1682        if true {
    if !#[allow(non_exhaustive_omitted_patterns)] match self.cfg.assert_terminator(yield_block).kind
                {
                TerminatorKind::Yield { .. } => true,
                _ => false,
            } {
        {
            ::core::panicking::panic_fmt(format_args!("coroutine_drop_cleanup called on block with non-yield terminator."));
        }
    };
};debug_assert!(
1683            matches!(self.cfg.assert_terminator(yield_block).kind, TerminatorKind::Yield { .. }),
1684            "coroutine_drop_cleanup called on block with non-yield terminator."
1685        );
1686        let cached_drop = self.diverge_dropline();
1687        self.scopes.coroutine_drops.add_entry_point(yield_block, cached_drop);
1688    }
1689
1690    /// Utility function for *non*-scope code to build their own drops
1691    /// Force a drop at this point in the MIR by creating a new block.
1692    pub(crate) fn build_drop_and_replace(
1693        &mut self,
1694        block: BasicBlock,
1695        span: Span,
1696        place: Place<'tcx>,
1697        value: Rvalue<'tcx>,
1698    ) -> BlockAnd<()> {
1699        let source_info = self.source_info(span);
1700
1701        // create the new block for the assignment
1702        let assign = self.cfg.start_new_block();
1703        self.cfg.push_assign(assign, source_info, place, value.clone());
1704
1705        // create the new block for the assignment in the case of unwinding
1706        let assign_unwind = self.cfg.start_new_cleanup_block();
1707        self.cfg.push_assign(assign_unwind, source_info, place, value.clone());
1708
1709        self.cfg.terminate(
1710            block,
1711            source_info,
1712            TerminatorKind::Drop {
1713                place,
1714                target: assign,
1715                unwind: UnwindAction::Cleanup(assign_unwind),
1716                replace: true,
1717                drop: None,
1718            },
1719        );
1720        self.diverge_from(block);
1721
1722        assign.unit()
1723    }
1724
1725    /// Creates an `Assert` terminator and return the success block.
1726    /// If the boolean condition operand is not the expected value,
1727    /// a runtime panic will be caused with the given message.
1728    pub(crate) fn assert(
1729        &mut self,
1730        block: BasicBlock,
1731        cond: Operand<'tcx>,
1732        expected: bool,
1733        msg: AssertMessage<'tcx>,
1734        span: Span,
1735    ) -> BasicBlock {
1736        let source_info = self.source_info(span);
1737        let success_block = self.cfg.start_new_block();
1738
1739        self.cfg.terminate(
1740            block,
1741            source_info,
1742            TerminatorKind::Assert {
1743                cond,
1744                expected,
1745                msg: Box::new(msg),
1746                target: success_block,
1747                unwind: UnwindAction::Continue,
1748            },
1749        );
1750        self.diverge_from(block);
1751
1752        success_block
1753    }
1754
1755    /// Unschedules any drops in the top two scopes.
1756    ///
1757    /// This is only needed for pattern-matches combining guards and or-patterns: or-patterns lead
1758    /// to guards being lowered multiple times before lowering the arm body, so we unschedle drops
1759    /// for guards' temporaries and bindings between lowering each instance of an match arm's guard.
1760    pub(crate) fn clear_match_arm_and_guard_scopes(&mut self, region_scope: region::Scope) {
1761        let [.., arm_scope, guard_scope] = &mut *self.scopes.scopes else {
1762            ::rustc_span::macros::bug_impl(None,
    format_args!("matches with guards should introduce separate scopes for the pattern and guard"),
    Location::caller());bug!("matches with guards should introduce separate scopes for the pattern and guard");
1763        };
1764
1765        {
    match (&arm_scope.region_scope, &region_scope) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(arm_scope.region_scope, region_scope);
1766        {
    match (&guard_scope.region_scope.data, &region::ScopeData::MatchGuard) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(guard_scope.region_scope.data, region::ScopeData::MatchGuard);
1767        {
    match (&guard_scope.region_scope.local_id, &region_scope.local_id) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(guard_scope.region_scope.local_id, region_scope.local_id);
1768
1769        arm_scope.drops.clear();
1770        arm_scope.invalidate_cache();
1771        guard_scope.drops.clear();
1772        guard_scope.invalidate_cache();
1773    }
1774}
1775
1776/// Builds drops for `pop_scope` and `leave_top_scope`.
1777///
1778/// # Parameters
1779///
1780/// * `unwind_drops`, the drop tree data structure storing what needs to be cleaned up if unwind occurs
1781/// * `scope`, describes the drops that will occur on exiting the scope in regular execution
1782/// * `block`, the block to branch to once drops are complete (assuming no unwind occurs)
1783/// * `unwind_to`, describes the drops that would occur at this point in the code if a
1784///   panic occurred (a subset of the drops in `scope`, since we sometimes elide StorageDead and other
1785///   instructions on unwinding)
1786/// * `dropline_to`, describes the drops that would occur at this point in the code if a
1787///    coroutine drop occurred.
1788/// * `storage_dead_on_unwind`, if true, then we should emit `StorageDead` even when unwinding
1789/// * `arg_count`, number of MIR local variables corresponding to fn arguments (used to assert that we don't drop those)
1790fn build_scope_drops<'tcx, F>(
1791    cfg: &mut CFG<'tcx>,
1792    unwind_drops: &mut DropTree,
1793    coroutine_drops: &mut DropTree,
1794    scope: &Scope,
1795    block: BasicBlock,
1796    unwind_to: DropIdx,
1797    dropline_to: Option<DropIdx>,
1798    storage_dead_on_unwind: bool,
1799    arg_count: usize,
1800    is_async_drop: F,
1801) -> BlockAnd<()>
1802where
1803    F: Fn(Local) -> bool,
1804{
1805    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs:1805",
                        "rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/59cbdee2e7b3ee50e44b460561c43c9e3023d9f0/compiler/rustc_mir_build/src/builder/scope.rs"),
                        ::tracing_core::__macro_support::Option::Some(1805u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("build_scope_drops({0:?} -> {1:?}), dropline_to={2:?}",
                                                    block, scope, dropline_to) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("build_scope_drops({:?} -> {:?}), dropline_to={:?}", block, scope, dropline_to);
1806
1807    // Build up the drops in evaluation order. The end result will
1808    // look like:
1809    //
1810    // [SDs, drops[n]] --..> [SDs, drop[1]] -> [SDs, drop[0]] -> [[SDs]]
1811    //               |                    |                 |
1812    //               :                    |                 |
1813    //                                    V                 V
1814    // [drop[n]] -...-> [drop[1]] ------> [drop[0]] ------> [last_unwind_to]
1815    //
1816    // The horizontal arrows represent the execution path when the drops return
1817    // successfully. The downwards arrows represent the execution path when the
1818    // drops panic (panicking while unwinding will abort, so there's no need for
1819    // another set of arrows).
1820    //
1821    // For coroutines, we unwind from a drop on a local to its StorageDead
1822    // statement. For other functions we don't worry about StorageDead. The
1823    // drops for the unwind path should have already been generated by
1824    // `diverge_cleanup_gen`.
1825
1826    // `unwind_to` indicates what needs to be dropped should unwinding occur.
1827    // This is a subset of what needs to be dropped when exiting the scope.
1828    // As we unwind the scope, we will also move `unwind_to` backwards to match,
1829    // so that we can use it should a destructor panic.
1830    let mut unwind_to = unwind_to;
1831
1832    // The block that we should jump to after drops complete. We start by building the final drop (`drops[n]`
1833    // in the diagram above) and then build the drops (e.g., `drop[1]`, `drop[0]`) that come before it.
1834    // block begins as the successor of `drops[n]` and then becomes `drops[n]` so that `drops[n-1]`
1835    // will branch to `drops[n]`.
1836    let mut block = block;
1837
1838    // `dropline_to` indicates what needs to be dropped should coroutine drop occur.
1839    let mut dropline_to = dropline_to;
1840
1841    for drop_data in scope.drops.iter().rev() {
1842        let source_info = drop_data.source_info;
1843        let local = drop_data.local;
1844
1845        match drop_data.kind {
1846            DropKind::Value => {
1847                // `unwind_to` should drop the value that we're about to
1848                // schedule. If dropping this value panics, then we continue
1849                // with the *next* value on the unwind path.
1850                //
1851                // We adjust this BEFORE we create the drop (e.g., `drops[n]`)
1852                // because `drops[n]` should unwind to `drops[n-1]`.
1853                if true {
    {
        match (&unwind_drops.drop_nodes[unwind_to].data.local,
                &drop_data.local) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(unwind_drops.drop_nodes[unwind_to].data.local, drop_data.local);
1854                if true {
    {
        match (&unwind_drops.drop_nodes[unwind_to].data.kind, &drop_data.kind)
            {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(unwind_drops.drop_nodes[unwind_to].data.kind, drop_data.kind);
1855                unwind_to = unwind_drops.drop_nodes[unwind_to].next;
1856
1857                if let Some(idx) = dropline_to {
1858                    if true {
    {
        match (&coroutine_drops.drop_nodes[idx].data.local, &drop_data.local)
            {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.local, drop_data.local);
1859                    if true {
    {
        match (&coroutine_drops.drop_nodes[idx].data.kind, &drop_data.kind) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.kind, drop_data.kind);
1860                    dropline_to = Some(coroutine_drops.drop_nodes[idx].next);
1861                }
1862
1863                // If the operand has been moved, and we are not on an unwind
1864                // path, then don't generate the drop. (We only take this into
1865                // account for non-unwind paths so as not to disturb the
1866                // caching mechanism.)
1867                if scope.moved_locals.contains(&local) {
1868                    continue;
1869                }
1870
1871                unwind_drops.add_entry_point(block, unwind_to);
1872                if let Some(to) = dropline_to
1873                    && is_async_drop(local)
1874                {
1875                    coroutine_drops.add_entry_point(block, to);
1876                }
1877
1878                let next = cfg.start_new_block();
1879                cfg.terminate(
1880                    block,
1881                    source_info,
1882                    TerminatorKind::Drop {
1883                        place: local.into(),
1884                        target: next,
1885                        unwind: UnwindAction::Continue,
1886                        replace: false,
1887                        drop: None,
1888                    },
1889                );
1890                block = next;
1891            }
1892            DropKind::ForLint => {
1893                // As in the `DropKind::Storage` case below:
1894                // normally lint-related drops are not emitted for unwind,
1895                // so we can just leave `unwind_to` unmodified, but in some
1896                // cases we emit things ALSO on the unwind path, so we need to adjust
1897                // `unwind_to` in that case.
1898                if storage_dead_on_unwind {
1899                    if true {
    {
        match (&unwind_drops.drop_nodes[unwind_to].data.local,
                &drop_data.local) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(
1900                        unwind_drops.drop_nodes[unwind_to].data.local,
1901                        drop_data.local
1902                    );
1903                    if true {
    {
        match (&unwind_drops.drop_nodes[unwind_to].data.kind, &drop_data.kind)
            {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(unwind_drops.drop_nodes[unwind_to].data.kind, drop_data.kind);
1904                    unwind_to = unwind_drops.drop_nodes[unwind_to].next;
1905                }
1906
1907                if let Some(idx) = dropline_to {
1908                    if true {
    {
        match (&coroutine_drops.drop_nodes[idx].data.local, &drop_data.local)
            {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.local, drop_data.local);
1909                    if true {
    {
        match (&coroutine_drops.drop_nodes[idx].data.kind, &drop_data.kind) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.kind, drop_data.kind);
1910                    dropline_to = Some(coroutine_drops.drop_nodes[idx].next);
1911                }
1912
1913                // If the operand has been moved, and we are not on an unwind
1914                // path, then don't generate the drop. (We only take this into
1915                // account for non-unwind paths so as not to disturb the
1916                // caching mechanism.)
1917                if scope.moved_locals.contains(&local) {
1918                    continue;
1919                }
1920
1921                cfg.push(
1922                    block,
1923                    Statement::new(
1924                        source_info,
1925                        StatementKind::BackwardIncompatibleDropHint {
1926                            place: Box::new(local.into()),
1927                            reason: BackwardIncompatibleDropReason::Edition2024,
1928                        },
1929                    ),
1930                );
1931            }
1932            DropKind::Storage => {
1933                // Ordinarily, storage-dead nodes are not emitted on unwind, so we don't
1934                // need to adjust `unwind_to` on this path. However, in some specific cases
1935                // we *do* emit storage-dead nodes on the unwind path, and in that case now that
1936                // the storage-dead has completed, we need to adjust the `unwind_to` pointer
1937                // so that any future drops we emit will not register storage-dead.
1938                if storage_dead_on_unwind {
1939                    if true {
    {
        match (&unwind_drops.drop_nodes[unwind_to].data.local,
                &drop_data.local) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(
1940                        unwind_drops.drop_nodes[unwind_to].data.local,
1941                        drop_data.local
1942                    );
1943                    if true {
    {
        match (&unwind_drops.drop_nodes[unwind_to].data.kind, &drop_data.kind)
            {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(unwind_drops.drop_nodes[unwind_to].data.kind, drop_data.kind);
1944                    unwind_to = unwind_drops.drop_nodes[unwind_to].next;
1945                }
1946                if let Some(idx) = dropline_to {
1947                    if true {
    {
        match (&coroutine_drops.drop_nodes[idx].data.local, &drop_data.local)
            {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.local, drop_data.local);
1948                    if true {
    {
        match (&coroutine_drops.drop_nodes[idx].data.kind, &drop_data.kind) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.kind, drop_data.kind);
1949                    dropline_to = Some(coroutine_drops.drop_nodes[idx].next);
1950                }
1951                // Only temps and vars need their storage dead.
1952                if !(local.index() > arg_count) {
    ::core::panicking::panic("assertion failed: local.index() > arg_count")
};assert!(local.index() > arg_count);
1953                cfg.push(block, Statement::new(source_info, StatementKind::StorageDead(local)));
1954            }
1955        }
1956    }
1957    block.unit()
1958}
1959
1960impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
1961    /// Build a drop tree for a breakable scope.
1962    ///
1963    /// If `continue_block` is `Some`, then the tree is for `continue` inside a
1964    /// loop. Otherwise this is for `break`, `return`, or `if`.
1965    fn build_exit_tree(
1966        &mut self,
1967        mut drops: DropTree,
1968        else_scope: region::Scope,
1969        span: Span,
1970        continue_block: Option<BasicBlock>,
1971    ) -> Option<BlockAnd<()>> {
1972        let blocks = drops.build_mir::<ExitScopes>(&mut self.cfg, continue_block);
1973        let is_coroutine = self.coroutine.is_some();
1974
1975        // Link the exit drop tree to unwind drop tree.
1976        if drops.drop_nodes.iter().any(|drop_node| drop_node.data.kind == DropKind::Value) {
1977            let unwind_target = self.diverge_cleanup_target(else_scope, span);
1978            let mut unwind_indices = IndexVec::from_elem_n(unwind_target, 1);
1979            for (drop_idx, drop_node) in drops.drop_nodes.iter_enumerated().skip(1) {
1980                match drop_node.data.kind {
1981                    DropKind::Storage | DropKind::ForLint => {
1982                        if is_coroutine {
1983                            let unwind_drop = self
1984                                .scopes
1985                                .unwind_drops
1986                                .add_drop(drop_node.data, unwind_indices[drop_node.next]);
1987                            unwind_indices.push(unwind_drop);
1988                        } else {
1989                            unwind_indices.push(unwind_indices[drop_node.next]);
1990                        }
1991                    }
1992                    DropKind::Value => {
1993                        let unwind_drop = self
1994                            .scopes
1995                            .unwind_drops
1996                            .add_drop(drop_node.data, unwind_indices[drop_node.next]);
1997                        self.scopes.unwind_drops.add_entry_point(
1998                            blocks[drop_idx].unwrap(),
1999                            unwind_indices[drop_node.next],
2000                        );
2001                        unwind_indices.push(unwind_drop);
2002                    }
2003                }
2004            }
2005        }
2006        // Link the exit drop tree to dropline drop tree (coroutine drop path) for async drops
2007        if is_coroutine
2008            && drops.drop_nodes.iter().any(|DropNode { data, next: _ }| {
2009                data.kind == DropKind::Value && self.is_async_drop(data.local)
2010            })
2011        {
2012            let dropline_target = self.diverge_dropline_target(else_scope, span);
2013            let mut dropline_indices = IndexVec::from_elem_n(dropline_target, 1);
2014            for (drop_idx, drop_data) in drops.drop_nodes.iter_enumerated().skip(1) {
2015                let coroutine_drop = self
2016                    .scopes
2017                    .coroutine_drops
2018                    .add_drop(drop_data.data, dropline_indices[drop_data.next]);
2019                match drop_data.data.kind {
2020                    DropKind::Storage | DropKind::ForLint => {}
2021                    DropKind::Value => {
2022                        if self.is_async_drop(drop_data.data.local) {
2023                            self.scopes.coroutine_drops.add_entry_point(
2024                                blocks[drop_idx].unwrap(),
2025                                dropline_indices[drop_data.next],
2026                            );
2027                        }
2028                    }
2029                }
2030                dropline_indices.push(coroutine_drop);
2031            }
2032        }
2033        blocks[ROOT_NODE].map(BasicBlock::unit)
2034    }
2035
2036    /// Build the unwind and coroutine drop trees.
2037    pub(crate) fn build_drop_trees(&mut self) {
2038        if self.coroutine.is_some() {
2039            self.build_coroutine_drop_trees();
2040        } else {
2041            Self::build_unwind_tree(
2042                &mut self.cfg,
2043                &mut self.scopes.unwind_drops,
2044                self.fn_span,
2045                &mut None,
2046            );
2047        }
2048    }
2049
2050    fn build_coroutine_drop_trees(&mut self) {
2051        // Build the drop tree for dropping the coroutine while it's suspended.
2052        let drops = &mut self.scopes.coroutine_drops;
2053        let cfg = &mut self.cfg;
2054        let fn_span = self.fn_span;
2055        let blocks = drops.build_mir::<CoroutineDrop>(cfg, None);
2056        if let Some(root_block) = blocks[ROOT_NODE] {
2057            cfg.terminate(
2058                root_block,
2059                SourceInfo::outermost(fn_span),
2060                TerminatorKind::CoroutineDrop,
2061            );
2062        }
2063
2064        // Build the drop tree for unwinding in the normal control flow paths.
2065        let resume_block = &mut None;
2066        let unwind_drops = &mut self.scopes.unwind_drops;
2067        Self::build_unwind_tree(cfg, unwind_drops, fn_span, resume_block);
2068
2069        // Build the drop tree for unwinding when dropping a suspended
2070        // coroutine.
2071        //
2072        // This is a different tree to the standard unwind paths here to
2073        // prevent drop elaboration from creating drop flags that would have
2074        // to be captured by the coroutine. I'm not sure how important this
2075        // optimization is, but it is here.
2076        for (drop_idx, drop_node) in drops.drop_nodes.iter_enumerated() {
2077            if let DropKind::Value = drop_node.data.kind
2078                && let Some(bb) = blocks[drop_idx]
2079            {
2080                if true {
    if !(drop_node.next < drops.drop_nodes.next_index()) {
        ::core::panicking::panic("assertion failed: drop_node.next < drops.drop_nodes.next_index()")
    };
};debug_assert!(drop_node.next < drops.drop_nodes.next_index());
2081                drops.entry_points.push((drop_node.next, bb));
2082            }
2083        }
2084        Self::build_unwind_tree(cfg, drops, fn_span, resume_block);
2085    }
2086
2087    fn build_unwind_tree(
2088        cfg: &mut CFG<'tcx>,
2089        drops: &mut DropTree,
2090        fn_span: Span,
2091        resume_block: &mut Option<BasicBlock>,
2092    ) {
2093        let blocks = drops.build_mir::<Unwind>(cfg, *resume_block);
2094        if let (None, Some(resume)) = (*resume_block, blocks[ROOT_NODE]) {
2095            cfg.terminate(resume, SourceInfo::outermost(fn_span), TerminatorKind::UnwindResume);
2096
2097            *resume_block = blocks[ROOT_NODE];
2098        }
2099    }
2100}
2101
2102// DropTreeBuilder implementations.
2103
2104struct ExitScopes;
2105
2106impl<'tcx> DropTreeBuilder<'tcx> for ExitScopes {
2107    fn make_block(cfg: &mut CFG<'tcx>) -> BasicBlock {
2108        cfg.start_new_block()
2109    }
2110    fn link_entry_point(cfg: &mut CFG<'tcx>, from: BasicBlock, to: BasicBlock) {
2111        // There should be an existing terminator with real source info and a
2112        // dummy TerminatorKind. Replace it with a proper goto.
2113        // (The dummy is added by `break_scope` and `break_from_if_then_scope`.)
2114        let term = cfg.assert_terminator_mut(from);
2115        if let TerminatorKind::UnwindResume = term.kind {
2116            term.kind = TerminatorKind::goto(to);
2117        } else {
2118            ::rustc_span::macros::bug_impl(Some(term.source_info.span),
    format_args!("unexpected dummy terminator kind: {0:?}", term.kind),
    Location::caller());span_bug!(term.source_info.span, "unexpected dummy terminator kind: {:?}", term.kind);
2119        }
2120    }
2121}
2122
2123struct CoroutineDrop;
2124
2125impl<'tcx> DropTreeBuilder<'tcx> for CoroutineDrop {
2126    fn make_block(cfg: &mut CFG<'tcx>) -> BasicBlock {
2127        cfg.start_new_block()
2128    }
2129    fn link_entry_point(cfg: &mut CFG<'tcx>, from: BasicBlock, to: BasicBlock) {
2130        let term = cfg.assert_terminator_mut(from);
2131        if let TerminatorKind::Yield { ref mut drop, .. } = term.kind {
2132            *drop = Some(to);
2133        } else if let TerminatorKind::Drop { ref mut drop, .. } = term.kind {
2134            *drop = Some(to);
2135        } else {
2136            ::rustc_span::macros::bug_impl(Some(term.source_info.span),
    format_args!("cannot enter coroutine drop tree from {0:?}", term.kind),
    Location::caller())span_bug!(
2137                term.source_info.span,
2138                "cannot enter coroutine drop tree from {:?}",
2139                term.kind
2140            )
2141        }
2142    }
2143}
2144
2145struct Unwind;
2146
2147impl<'tcx> DropTreeBuilder<'tcx> for Unwind {
2148    fn make_block(cfg: &mut CFG<'tcx>) -> BasicBlock {
2149        cfg.start_new_cleanup_block()
2150    }
2151    fn link_entry_point(cfg: &mut CFG<'tcx>, from: BasicBlock, to: BasicBlock) {
2152        let term = &mut cfg.assert_terminator_mut(from);
2153        match &mut term.kind {
2154            TerminatorKind::Drop { unwind, .. } => {
2155                if let UnwindAction::Cleanup(unwind) = *unwind {
2156                    let source_info = term.source_info;
2157                    cfg.terminate(unwind, source_info, TerminatorKind::goto(to));
2158                } else {
2159                    *unwind = UnwindAction::Cleanup(to);
2160                }
2161            }
2162            TerminatorKind::FalseUnwind { unwind, .. }
2163            | TerminatorKind::Call { unwind, .. }
2164            | TerminatorKind::Assert { unwind, .. }
2165            | TerminatorKind::InlineAsm { unwind, .. } => {
2166                *unwind = UnwindAction::Cleanup(to);
2167            }
2168            TerminatorKind::Goto { .. }
2169            | TerminatorKind::SwitchInt { .. }
2170            | TerminatorKind::UnwindResume
2171            | TerminatorKind::UnwindTerminate(_)
2172            | TerminatorKind::Return
2173            | TerminatorKind::TailCall { .. }
2174            | TerminatorKind::Unreachable
2175            | TerminatorKind::Yield { .. }
2176            | TerminatorKind::CoroutineDrop
2177            | TerminatorKind::FalseEdge { .. } => {
2178                ::rustc_span::macros::bug_impl(Some(term.source_info.span),
    format_args!("cannot unwind from {0:?}", term.kind), Location::caller())span_bug!(term.source_info.span, "cannot unwind from {:?}", term.kind)
2179            }
2180        }
2181    }
2182}