Skip to main content

rustc_mir_transform/
elaborate_drop.rs

1use std::{fmt, iter, mem};
2
3use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
4use rustc_hir::def::DefKind;
5use rustc_hir::lang_items::LangItem;
6use rustc_index::Idx;
7use rustc_middle::mir::*;
8use rustc_middle::ty::adjustment::PointerCoercion;
9use rustc_middle::ty::util::IntTypeExt;
10use rustc_middle::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt};
11use rustc_middle::{bug, span_bug, traits};
12use rustc_span::DUMMY_SP;
13use rustc_span::source_map::{Spanned, dummy_spanned};
14use tracing::{debug, instrument};
15
16use crate::patch::MirPatch;
17
18/// Describes how/if a value should be dropped.
19#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DropStyle {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                DropStyle::Dead => "Dead",
                DropStyle::Static => "Static",
                DropStyle::Conditional => "Conditional",
                DropStyle::Open => "Open",
            })
    }
}Debug)]
20pub(crate) enum DropStyle {
21    /// The value is already dead at the drop location, no drop will be executed.
22    Dead,
23
24    /// The value is known to always be initialized at the drop location, drop will always be
25    /// executed.
26    Static,
27
28    /// Whether the value needs to be dropped depends on its drop flag.
29    Conditional,
30
31    /// An "open" drop is one where only the fields of a value are dropped.
32    ///
33    /// For example, this happens when moving out of a struct field: The rest of the struct will be
34    /// dropped in such an "open" drop. It is also used to generate drop glue for the individual
35    /// components of a value, for example for dropping array elements.
36    Open,
37}
38
39/// Which drop flags to affect/check with an operation.
40#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DropFlagMode {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                DropFlagMode::Shallow => "Shallow",
                DropFlagMode::Deep => "Deep",
            })
    }
}Debug)]
41pub(crate) enum DropFlagMode {
42    /// Only affect the top-level drop flag, not that of any contained fields.
43    Shallow,
44    /// Affect all nested drop flags in addition to the top-level one.
45    Deep,
46}
47
48/// Describes if unwinding is necessary and where to unwind to if a panic occurs.
49#[derive(#[automatically_derived]
impl ::core::marker::Copy for Unwind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Unwind {
    #[inline]
    fn clone(&self) -> Unwind {
        let _: ::core::clone::AssertParamIsClone<BasicBlock>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Unwind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Unwind::To(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "To",
                    &__self_0),
            Unwind::InCleanup =>
                ::core::fmt::Formatter::write_str(f, "InCleanup"),
        }
    }
}Debug)]
50pub(crate) enum Unwind {
51    /// Unwind to this block.
52    To(BasicBlock),
53    /// Already in an unwind path, any panic will cause an abort.
54    InCleanup,
55}
56
57impl Unwind {
58    fn is_cleanup(self) -> bool {
59        match self {
60            Unwind::To(..) => false,
61            Unwind::InCleanup => true,
62        }
63    }
64
65    fn into_action(self) -> UnwindAction {
66        match self {
67            Unwind::To(bb) => UnwindAction::Cleanup(bb),
68            Unwind::InCleanup => UnwindAction::Terminate(UnwindTerminateReason::InCleanup),
69        }
70    }
71
72    fn map<F>(self, f: F) -> Self
73    where
74        F: FnOnce(BasicBlock) -> BasicBlock,
75    {
76        match self {
77            Unwind::To(bb) => Unwind::To(f(bb)),
78            Unwind::InCleanup => Unwind::InCleanup,
79        }
80    }
81}
82
83pub(crate) trait DropElaborator<'a, 'tcx>: fmt::Debug {
84    /// The type representing paths that can be moved out of.
85    ///
86    /// Users can move out of individual fields of a struct, such as `a.b.c`. This type is used to
87    /// represent such move paths. Sometimes tracking individual move paths is not necessary, in
88    /// which case this may be set to (for example) `()`.
89    type Path: Copy + fmt::Debug;
90
91    // Accessors
92
93    fn patch_ref(&self) -> &MirPatch<'tcx>;
94    fn patch(&mut self) -> &mut MirPatch<'tcx>;
95    fn body(&self) -> &'a Body<'tcx>;
96    fn tcx(&self) -> TyCtxt<'tcx>;
97    fn typing_env(&self) -> ty::TypingEnv<'tcx>;
98    fn allow_async_drops(&self) -> bool;
99
100    fn terminator_loc(&self, bb: BasicBlock) -> Location;
101
102    // Drop logic
103
104    /// Returns how `path` should be dropped, given `mode`.
105    fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle;
106
107    /// Returns the drop flag of `path` as a MIR `Operand` (or `None` if `path` has no drop flag).
108    fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>>;
109
110    /// Modifies the MIR patch so that the drop flag of `path` (if any) is cleared at `location`.
111    ///
112    /// If `mode` is deep, drop flags of all child paths should also be cleared by inserting
113    /// additional statements.
114    fn clear_drop_flag(&mut self, location: Location, path: Self::Path, mode: DropFlagMode);
115
116    // Subpaths
117
118    /// Returns the subpath of a field of `path` (or `None` if there is no dedicated subpath).
119    ///
120    /// If this returns `None`, `field` will not get a dedicated drop flag.
121    fn field_subpath(&self, path: Self::Path, field: FieldIdx) -> Option<Self::Path>;
122
123    /// Returns the subpath of a dereference of `path` (or `None` if there is no dedicated subpath).
124    ///
125    /// If this returns `None`, `*path` will not get a dedicated drop flag.
126    ///
127    /// This is only relevant for `Box<T>`, where the contained `T` can be moved out of the box.
128    fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path>;
129
130    /// Returns the subpath of downcasting `path` to one of its variants.
131    ///
132    /// If this returns `None`, the downcast of `path` will not get a dedicated drop flag.
133    fn downcast_subpath(&self, path: Self::Path, variant: VariantIdx) -> Option<Self::Path>;
134
135    /// Returns the subpath of indexing a fixed-size array `path`.
136    ///
137    /// If this returns `None`, elements of `path` will not get a dedicated drop flag.
138    ///
139    /// This is only relevant for array patterns, which can move out of individual array elements.
140    fn array_subpath(&self, path: Self::Path, index: u64, size: u64) -> Option<Self::Path>;
141}
142
143#[derive(#[automatically_derived]
impl<'a, 'b, 'tcx, D: ::core::fmt::Debug> ::core::fmt::Debug for
    DropCtxt<'a, 'b, 'tcx, D> where D: DropElaborator<'b, 'tcx>,
    D::Path: ::core::fmt::Debug {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["elaborator", "source_info", "place", "path", "succ", "unwind",
                        "dropline"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.elaborator, &self.source_info, &self.place, &self.path,
                        &self.succ, &self.unwind, &&self.dropline];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, "DropCtxt",
            names, values)
    }
}Debug)]
144struct DropCtxt<'a, 'b, 'tcx, D>
145where
146    D: DropElaborator<'b, 'tcx>,
147{
148    elaborator: &'a mut D,
149
150    source_info: SourceInfo,
151
152    place: Place<'tcx>,
153    path: D::Path,
154    succ: BasicBlock,
155    unwind: Unwind,
156    dropline: Option<BasicBlock>,
157}
158
159/// "Elaborates" a drop of `place`/`path` and patches `bb`'s terminator to execute it.
160///
161/// The passed `elaborator` is used to determine what should happen at the drop terminator. It
162/// decides whether the drop can be statically determined or whether it needs a dynamic drop flag,
163/// and whether the drop is "open", ie. should be expanded to drop all subfields of the dropped
164/// value.
165///
166/// When this returns, the MIR patch in the `elaborator` contains the necessary changes.
167pub(crate) fn elaborate_drop<'b, 'tcx, D>(
168    elaborator: &mut D,
169    source_info: SourceInfo,
170    place: Place<'tcx>,
171    path: D::Path,
172    succ: BasicBlock,
173    unwind: Unwind,
174    bb: BasicBlock,
175    dropline: Option<BasicBlock>,
176) where
177    D: DropElaborator<'b, 'tcx>,
178    'tcx: 'b,
179{
180    DropCtxt { elaborator, source_info, place, path, succ, unwind, dropline }.elaborate_drop(bb)
181}
182
183impl<'a, 'b, 'tcx, D> DropCtxt<'a, 'b, 'tcx, D>
184where
185    D: DropElaborator<'b, 'tcx>,
186    'tcx: 'b,
187{
188    x;#[instrument(level = "trace", skip(self), ret)]
189    fn place_ty(&self, place: Place<'tcx>) -> Ty<'tcx> {
190        if place.local < self.elaborator.body().local_decls.next_index() {
191            place.ty(self.elaborator.body(), self.tcx()).ty
192        } else {
193            // We don't have a slice with all the locals, since some are in the patch.
194            PlaceTy::from_ty(self.elaborator.patch_ref().local_ty(place.local))
195                .multi_projection_ty(self.elaborator.tcx(), place.projection)
196                .ty
197        }
198    }
199
200    fn tcx(&self) -> TyCtxt<'tcx> {
201        self.elaborator.tcx()
202    }
203
204    // Generates three blocks:
205    // * #1:pin_obj_bb:   call Pin<ObjTy>::new_unchecked(&mut obj)
206    // * #2:call_drop_bb: fut = call obj.<AsyncDrop::drop>() OR call async_drop_in_place<T>(obj)
207    // * #3:drop_term_bb: drop (obj, fut, ...)
208    // We keep async drop unexpanded to poll-loop here, to expand it later, at StateTransform -
209    //   into states expand.
210    // call_destructor_only - to call only AsyncDrop::drop, not full async_drop_in_place glue
211    fn build_async_drop(
212        &mut self,
213        place: Place<'tcx>,
214        drop_ty: Ty<'tcx>,
215        bb: Option<BasicBlock>,
216        succ: BasicBlock,
217        unwind: Unwind,
218        dropline: Option<BasicBlock>,
219        call_destructor_only: bool,
220    ) -> BasicBlock {
221        let tcx = self.tcx();
222        let span = self.source_info.span;
223
224        let pin_obj_bb = bb.unwrap_or_else(|| {
225            self.elaborator.patch().new_block(BasicBlockData::new(
226                Some(Terminator {
227                    // Temporary terminator, will be replaced by patch
228                    source_info: self.source_info,
229                    kind: TerminatorKind::Return,
230                }),
231                false,
232            ))
233        });
234
235        let (fut_ty, drop_fn_def_id, trait_args) = if call_destructor_only {
236            // Resolving obj.<AsyncDrop::drop>()
237            let trait_ref =
238                ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::AsyncDrop, span), [drop_ty]);
239            let (drop_trait, trait_args) = match tcx.codegen_select_candidate(
240                ty::TypingEnv::fully_monomorphized().as_query_input(trait_ref),
241            ) {
242                Ok(traits::ImplSource::UserDefined(traits::ImplSourceUserDefinedData {
243                    impl_def_id,
244                    args,
245                    ..
246                })) => (*impl_def_id, *args),
247                impl_source => {
248                    ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("invalid `AsyncDrop` impl_source: {0:?}", impl_source));span_bug!(span, "invalid `AsyncDrop` impl_source: {:?}", impl_source);
249                }
250            };
251            // impl_item_refs may be empty if drop fn is not implemented in 'impl AsyncDrop for ...'
252            // (#140974).
253            // Such code will report error, so just generate sync drop here and return
254            let Some(drop_fn_def_id) =
255                tcx.associated_item_def_ids(drop_trait).first().and_then(|&def_id| {
256                    if tcx.def_kind(def_id) == DefKind::AssocFn
257                        && tcx.check_args_compatible(def_id, trait_args)
258                    {
259                        Some(def_id)
260                    } else {
261                        None
262                    }
263                })
264            else {
265                tcx.dcx().span_delayed_bug(
266                    self.elaborator.body().span,
267                    "AsyncDrop type without correct `async fn drop(...)`.",
268                );
269                self.elaborator.patch().patch_terminator(
270                    pin_obj_bb,
271                    TerminatorKind::Drop {
272                        place,
273                        target: succ,
274                        unwind: unwind.into_action(),
275                        replace: false,
276                        drop: None,
277                        async_fut: None,
278                    },
279                );
280                return pin_obj_bb;
281            };
282            let drop_fn = Ty::new_fn_def(tcx, drop_fn_def_id, trait_args);
283            let sig = drop_fn.fn_sig(tcx);
284            let sig = tcx.instantiate_bound_regions_with_erased(sig);
285            (sig.output(), drop_fn_def_id, trait_args)
286        } else {
287            // Resolving async_drop_in_place<T> function for drop_ty
288            let drop_fn_def_id = tcx.require_lang_item(LangItem::AsyncDropInPlace, span);
289            let trait_args = tcx.mk_args(&[drop_ty.into()]);
290            let sig = tcx.fn_sig(drop_fn_def_id).instantiate(tcx, trait_args);
291            let sig = tcx.instantiate_bound_regions_with_erased(sig);
292            (sig.output(), drop_fn_def_id, trait_args)
293        };
294
295        let fut = Place::from(self.new_temp(fut_ty));
296
297        // #1:pin_obj_bb >>> obj_ref = &mut obj
298        let obj_ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, drop_ty);
299        let obj_ref_place = Place::from(self.new_temp(obj_ref_ty));
300
301        let term_loc = self.elaborator.terminator_loc(pin_obj_bb);
302        self.elaborator.patch().add_assign(
303            term_loc,
304            obj_ref_place,
305            Rvalue::Ref(
306                tcx.lifetimes.re_erased,
307                BorrowKind::Mut { kind: MutBorrowKind::Default },
308                place,
309            ),
310        );
311
312        // pin_obj_place preparation
313        let pin_obj_new_unchecked_fn = Ty::new_fn_def(
314            tcx,
315            tcx.require_lang_item(LangItem::PinNewUnchecked, span),
316            [GenericArg::from(obj_ref_ty)],
317        );
318        let pin_obj_ty = pin_obj_new_unchecked_fn.fn_sig(tcx).output().no_bound_vars().unwrap();
319        let pin_obj_place = Place::from(self.new_temp(pin_obj_ty));
320        let pin_obj_new_unchecked_fn = Operand::Constant(Box::new(ConstOperand {
321            span,
322            user_ty: None,
323            const_: Const::zero_sized(pin_obj_new_unchecked_fn),
324        }));
325
326        // Create an intermediate block that does StorageDead(fut) then jumps to succ.
327        // This is necessary because `succ` may differ from `self.succ` (e.g. when
328        // build_async_drop is called from drop_loop, `succ` is the loop header).
329        // Placing StorageDead directly at `self.succ` would miss the loop-back edge,
330        // causing StorageLive(fut) to fire again without a preceding StorageDead.
331        let succ_with_dead = self.new_block_with_statements(
332            unwind,
333            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [Statement::new(self.source_info,
                    StatementKind::StorageDead(fut.local))]))vec![Statement::new(self.source_info, StatementKind::StorageDead(fut.local))],
334            TerminatorKind::Goto { target: succ },
335        );
336
337        // #3:drop_term_bb
338        let drop_term_bb = self.new_block(
339            unwind,
340            TerminatorKind::Drop {
341                place,
342                target: succ_with_dead,
343                unwind: unwind.into_action(),
344                replace: false,
345                drop: dropline,
346                async_fut: Some(fut.local),
347            },
348        );
349
350        // #2:call_drop_bb
351        let mut call_statements = Vec::new();
352        let drop_arg = if call_destructor_only {
353            pin_obj_place
354        } else {
355            let ty::Adt(adt_def, adt_args) = pin_obj_ty.kind() else {
356                ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
357            };
358            let obj_ptr_ty = Ty::new_mut_ptr(tcx, drop_ty);
359            let unwrap_ty = adt_def.non_enum_variant().fields[FieldIdx::ZERO].ty(tcx, adt_args);
360            let obj_ref_place = Place::from(self.new_temp(unwrap_ty));
361            call_statements.push(self.assign(
362                obj_ref_place,
363                Rvalue::Use(Operand::Copy(tcx.mk_place_field(
364                    pin_obj_place,
365                    FieldIdx::ZERO,
366                    unwrap_ty,
367                ))),
368            ));
369
370            let obj_ptr_place = Place::from(self.new_temp(obj_ptr_ty));
371
372            let addr = Rvalue::RawPtr(RawPtrKind::Mut, tcx.mk_place_deref(obj_ref_place));
373            call_statements.push(self.assign(obj_ptr_place, addr));
374            obj_ptr_place
375        };
376        call_statements
377            .push(Statement::new(self.source_info, StatementKind::StorageLive(fut.local)));
378
379        let call_drop_bb = self.new_block_with_statements(
380            unwind,
381            call_statements,
382            TerminatorKind::Call {
383                func: Operand::function_handle(tcx, drop_fn_def_id, trait_args, span),
384                args: [Spanned { node: Operand::Move(drop_arg), span: DUMMY_SP }].into(),
385                destination: fut,
386                target: Some(drop_term_bb),
387                unwind: unwind.into_action(),
388                call_source: CallSource::Misc,
389                fn_span: self.source_info.span,
390            },
391        );
392        // StorageDead(fut) in unwind block (at the begin)
393        if let Unwind::To(block) = unwind {
394            self.elaborator.patch().add_statement(
395                Location { block, statement_index: 0 },
396                StatementKind::StorageDead(fut.local),
397            );
398        }
399        // StorageDead(fut) in dropline block (at the begin)
400        if let Some(block) = dropline {
401            self.elaborator.patch().add_statement(
402                Location { block, statement_index: 0 },
403                StatementKind::StorageDead(fut.local),
404            );
405        }
406
407        // #1:pin_obj_bb >>> call Pin<ObjTy>::new_unchecked(&mut obj)
408        self.elaborator.patch().patch_terminator(
409            pin_obj_bb,
410            TerminatorKind::Call {
411                func: pin_obj_new_unchecked_fn,
412                args: [dummy_spanned(Operand::Move(obj_ref_place))].into(),
413                destination: pin_obj_place,
414                target: Some(call_drop_bb),
415                unwind: unwind.into_action(),
416                call_source: CallSource::Misc,
417                fn_span: span,
418            },
419        );
420        pin_obj_bb
421    }
422
423    fn build_drop(&mut self, bb: BasicBlock) {
424        let drop_ty = self.place_ty(self.place);
425        if self.tcx().features().async_drop()
426            && self.elaborator.body().coroutine.is_some()
427            && self.elaborator.allow_async_drops()
428            && !self.elaborator.patch_ref().block(self.elaborator.body(), bb).is_cleanup
429            && drop_ty.needs_async_drop(self.tcx(), self.elaborator.typing_env())
430        {
431            self.build_async_drop(
432                self.place,
433                drop_ty,
434                Some(bb),
435                self.succ,
436                self.unwind,
437                self.dropline,
438                false,
439            );
440        } else {
441            self.elaborator.patch().patch_terminator(
442                bb,
443                TerminatorKind::Drop {
444                    place: self.place,
445                    target: self.succ,
446                    unwind: self.unwind.into_action(),
447                    replace: false,
448                    drop: None,
449                    async_fut: None,
450                },
451            );
452        }
453    }
454
455    /// This elaborates a single drop instruction, located at `bb`, and
456    /// patches over it.
457    ///
458    /// The elaborated drop checks the drop flags to only drop what
459    /// is initialized.
460    ///
461    /// In addition, the relevant drop flags also need to be cleared
462    /// to avoid double-drops. However, in the middle of a complex
463    /// drop, one must avoid clearing some of the flags before they
464    /// are read, as that would cause a memory leak.
465    ///
466    /// In particular, when dropping an ADT, multiple fields may be
467    /// joined together under the `rest` subpath. They are all controlled
468    /// by the primary drop flag, but only the last rest-field dropped
469    /// should clear it (and it must also not clear anything else).
470    //
471    // FIXME: I think we should just control the flags externally,
472    // and then we do not need this machinery.
473    #[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("elaborate_drop",
                                    "rustc_mir_transform::elaborate_drop",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                                    ::tracing_core::__macro_support::Option::Some(473u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                                    ::tracing_core::field::FieldSet::new(&["self", "bb"],
                                        ::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};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&bb)
                                                            as &dyn 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;
        }
        {
            match self.elaborator.drop_style(self.path, DropFlagMode::Deep) {
                DropStyle::Dead => {
                    self.elaborator.patch().patch_terminator(bb,
                        TerminatorKind::Goto { target: self.succ });
                }
                DropStyle::Static => { self.build_drop(bb); }
                DropStyle::Conditional => {
                    let drop_bb = self.complete_drop(self.succ, self.unwind);
                    self.elaborator.patch().patch_terminator(bb,
                        TerminatorKind::Goto { target: drop_bb });
                }
                DropStyle::Open => {
                    let drop_bb = self.open_drop();
                    self.elaborator.patch().patch_terminator(bb,
                        TerminatorKind::Goto { target: drop_bb });
                }
            }
        }
    }
}#[instrument(level = "debug")]
474    fn elaborate_drop(&mut self, bb: BasicBlock) {
475        match self.elaborator.drop_style(self.path, DropFlagMode::Deep) {
476            DropStyle::Dead => {
477                self.elaborator
478                    .patch()
479                    .patch_terminator(bb, TerminatorKind::Goto { target: self.succ });
480            }
481            DropStyle::Static => {
482                self.build_drop(bb);
483            }
484            DropStyle::Conditional => {
485                let drop_bb = self.complete_drop(self.succ, self.unwind);
486                self.elaborator
487                    .patch()
488                    .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
489            }
490            DropStyle::Open => {
491                let drop_bb = self.open_drop();
492                self.elaborator
493                    .patch()
494                    .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
495            }
496        }
497    }
498
499    /// Returns the place and move path for each field of `variant`,
500    /// (the move path is `None` if the field is a rest field).
501    fn move_paths_for_fields(
502        &self,
503        base_place: Place<'tcx>,
504        variant_path: D::Path,
505        variant: &'tcx ty::VariantDef,
506        args: GenericArgsRef<'tcx>,
507    ) -> Vec<(Place<'tcx>, Option<D::Path>)> {
508        variant
509            .fields
510            .iter_enumerated()
511            .map(|(field_idx, field)| {
512                let subpath = self.elaborator.field_subpath(variant_path, field_idx);
513                let tcx = self.tcx();
514
515                match (&self.elaborator.typing_env().typing_mode,
        &ty::TypingMode::PostAnalysis) {
    (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!(self.elaborator.typing_env().typing_mode, ty::TypingMode::PostAnalysis);
516                let field_ty = field.ty(tcx, args);
517                // We silently leave an unnormalized type here to support polymorphic drop
518                // elaboration for users of rustc internal APIs
519                let field_ty = tcx
520                    .try_normalize_erasing_regions(self.elaborator.typing_env(), field_ty)
521                    .unwrap_or(field_ty);
522
523                (tcx.mk_place_field(base_place, field_idx, field_ty), subpath)
524            })
525            .collect()
526    }
527
528    fn drop_subpath(
529        &mut self,
530        place: Place<'tcx>,
531        path: Option<D::Path>,
532        succ: BasicBlock,
533        unwind: Unwind,
534        dropline: Option<BasicBlock>,
535    ) -> BasicBlock {
536        if let Some(path) = path {
537            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:537",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(537u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("drop_subpath: for std field {0:?}",
                                                    place) as &dyn Value))])
            });
    } else { ; }
};debug!("drop_subpath: for std field {:?}", place);
538
539            DropCtxt {
540                elaborator: self.elaborator,
541                source_info: self.source_info,
542                path,
543                place,
544                succ,
545                unwind,
546                dropline,
547            }
548            .elaborated_drop_block()
549        } else {
550            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:550",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(550u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("drop_subpath: for rest field {0:?}",
                                                    place) as &dyn Value))])
            });
    } else { ; }
};debug!("drop_subpath: for rest field {:?}", place);
551
552            DropCtxt {
553                elaborator: self.elaborator,
554                source_info: self.source_info,
555                place,
556                succ,
557                unwind,
558                dropline,
559                // Using `self.path` here to condition the drop on
560                // our own drop flag.
561                path: self.path,
562            }
563            .complete_drop(succ, unwind)
564        }
565    }
566
567    /// Creates one-half of the drop ladder for a list of fields, and return
568    /// the list of steps in it in reverse order, with the first step
569    /// dropping 0 fields and so on.
570    ///
571    /// `unwind_ladder` is such a list of steps in reverse order,
572    /// which is called if the matching step of the drop glue panics.
573    ///
574    /// `dropline_ladder` is a similar list of steps in reverse order,
575    /// which is called if the matching step of the drop glue will contain async drop
576    /// (expanded later to Yield) and the containing coroutine will be dropped at this point.
577    fn drop_halfladder(
578        &mut self,
579        unwind_ladder: &[Unwind],
580        dropline_ladder: &[Option<BasicBlock>],
581        mut succ: BasicBlock,
582        fields: &[(Place<'tcx>, Option<D::Path>)],
583    ) -> Vec<BasicBlock> {
584        iter::once(succ)
585            .chain(::itertools::__std_iter::IntoIterator::into_iter(fields.iter().rev()).zip(unwind_ladder).zip(dropline_ladder).map(|((a,
            b), b)| (a, b, b))itertools::izip!(fields.iter().rev(), unwind_ladder, dropline_ladder).map(
586                |(&(place, path), &unwind_succ, &dropline_to)| {
587                    succ = self.drop_subpath(place, path, succ, unwind_succ, dropline_to);
588                    succ
589                },
590            ))
591            .collect()
592    }
593
594    fn drop_ladder_bottom(&mut self) -> (BasicBlock, Unwind, Option<BasicBlock>) {
595        // Clear the "master" drop flag at the end. This is needed
596        // because the "master" drop protects the ADT's discriminant,
597        // which is invalidated after the ADT is dropped.
598        (
599            self.drop_flag_reset_block(DropFlagMode::Shallow, self.succ, self.unwind),
600            self.unwind,
601            self.dropline,
602        )
603    }
604
605    /// Creates a full drop ladder, consisting of 2 connected half-drop-ladders
606    ///
607    /// For example, with 3 fields, the drop ladder is
608    ///
609    /// ```text
610    /// .d0:
611    ///     ELAB(drop location.0 [target=.d1, unwind=.c1])
612    /// .d1:
613    ///     ELAB(drop location.1 [target=.d2, unwind=.c2])
614    /// .d2:
615    ///     ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`])
616    /// .c1:
617    ///     ELAB(drop location.1 [target=.c2])
618    /// .c2:
619    ///     ELAB(drop location.2 [target=`self.unwind`])
620    /// ```
621    ///
622    /// For possible-async drops in coroutines we also need dropline ladder
623    /// ```text
624    /// .d0 (mainline):
625    ///     ELAB(drop location.0 [target=.d1, unwind=.c1, drop=.e1])
626    /// .d1 (mainline):
627    ///     ELAB(drop location.1 [target=.d2, unwind=.c2, drop=.e2])
628    /// .d2 (mainline):
629    ///     ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`, drop=`self.drop`])
630    /// .c1 (unwind):
631    ///     ELAB(drop location.1 [target=.c2])
632    /// .c2 (unwind):
633    ///     ELAB(drop location.2 [target=`self.unwind`])
634    /// .e1 (dropline):
635    ///     ELAB(drop location.1 [target=.e2, unwind=.c2])
636    /// .e2 (dropline):
637    ///     ELAB(drop location.2 [target=`self.drop`, unwind=`self.unwind`])
638    /// ```
639    ///
640    /// NOTE: this does not clear the master drop flag, so you need
641    /// to point succ/unwind on a `drop_ladder_bottom`.
642    fn drop_ladder(
643        &mut self,
644        fields: Vec<(Place<'tcx>, Option<D::Path>)>,
645        succ: BasicBlock,
646        unwind: Unwind,
647        dropline: Option<BasicBlock>,
648    ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
649        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:649",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(649u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("drop_ladder({0:?}, {1:?})",
                                                    self, fields) as &dyn Value))])
            });
    } else { ; }
};debug!("drop_ladder({:?}, {:?})", self, fields);
650        if !if unwind.is_cleanup() { dropline.is_none() } else { true } {
    {
        ::core::panicking::panic_fmt(format_args!("Dropline is set for cleanup drop ladder"));
    }
};assert!(
651            if unwind.is_cleanup() { dropline.is_none() } else { true },
652            "Dropline is set for cleanup drop ladder"
653        );
654
655        let mut fields = fields;
656        fields.retain(|&(place, _)| {
657            self.place_ty(place).needs_drop(self.tcx(), self.elaborator.typing_env())
658        });
659
660        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:660",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(660u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("drop_ladder - fields needing drop: {0:?}",
                                                    fields) as &dyn Value))])
            });
    } else { ; }
};debug!("drop_ladder - fields needing drop: {:?}", fields);
661
662        let dropline_ladder: Vec<Option<BasicBlock>> = ::alloc::vec::from_elem(None, fields.len() + 1)vec![None; fields.len() + 1];
663        let unwind_ladder = ::alloc::vec::from_elem(Unwind::InCleanup, fields.len() + 1)vec![Unwind::InCleanup; fields.len() + 1];
664        let unwind_ladder: Vec<_> = if let Unwind::To(succ) = unwind {
665            let halfladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
666            halfladder.into_iter().map(Unwind::To).collect()
667        } else {
668            unwind_ladder
669        };
670        let dropline_ladder: Vec<_> = if let Some(succ) = dropline {
671            let halfladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
672            halfladder.into_iter().map(Some).collect()
673        } else {
674            dropline_ladder
675        };
676
677        let normal_ladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
678
679        (
680            *normal_ladder.last().unwrap(),
681            *unwind_ladder.last().unwrap(),
682            *dropline_ladder.last().unwrap(),
683        )
684    }
685
686    fn open_drop_for_tuple(&mut self, tys: &[Ty<'tcx>]) -> BasicBlock {
687        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:687",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(687u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("open_drop_for_tuple({0:?}, {1:?})",
                                                    self, tys) as &dyn Value))])
            });
    } else { ; }
};debug!("open_drop_for_tuple({:?}, {:?})", self, tys);
688
689        let fields = tys
690            .iter()
691            .enumerate()
692            .map(|(i, &ty)| {
693                (
694                    self.tcx().mk_place_field(self.place, FieldIdx::new(i), ty),
695                    self.elaborator.field_subpath(self.path, FieldIdx::new(i)),
696                )
697            })
698            .collect();
699
700        let (succ, unwind, dropline) = self.drop_ladder_bottom();
701        self.drop_ladder(fields, succ, unwind, dropline).0
702    }
703
704    /// Drops the T contained in a `Box<T>` if it has not been moved out of
705    x;#[instrument(level = "debug", ret)]
706    fn open_drop_for_box_contents(
707        &mut self,
708        adt: ty::AdtDef<'tcx>,
709        args: GenericArgsRef<'tcx>,
710        succ: BasicBlock,
711        unwind: Unwind,
712        dropline: Option<BasicBlock>,
713    ) -> BasicBlock {
714        // drop glue is sent straight to codegen
715        // box cannot be directly dereferenced
716        let unique_ty = adt.non_enum_variant().fields[FieldIdx::ZERO].ty(self.tcx(), args);
717        let unique_variant = unique_ty.ty_adt_def().unwrap().non_enum_variant();
718        let nonnull_ty = unique_variant.fields[FieldIdx::ZERO].ty(self.tcx(), args);
719        let ptr_ty = Ty::new_imm_ptr(self.tcx(), args[0].expect_ty());
720
721        let unique_place = self.tcx().mk_place_field(self.place, FieldIdx::ZERO, unique_ty);
722        let nonnull_place = self.tcx().mk_place_field(unique_place, FieldIdx::ZERO, nonnull_ty);
723
724        let ptr_local = self.new_temp(ptr_ty);
725
726        let interior = self.tcx().mk_place_deref(Place::from(ptr_local));
727        let interior_path = self.elaborator.deref_subpath(self.path);
728
729        let do_drop_bb = self.drop_subpath(interior, interior_path, succ, unwind, dropline);
730
731        let setup_bbd = BasicBlockData::new_stmts(
732            vec![self.assign(
733                Place::from(ptr_local),
734                Rvalue::Cast(CastKind::Transmute, Operand::Copy(nonnull_place), ptr_ty),
735            )],
736            Some(Terminator {
737                kind: TerminatorKind::Goto { target: do_drop_bb },
738                source_info: self.source_info,
739            }),
740            unwind.is_cleanup(),
741        );
742        self.elaborator.patch().new_block(setup_bbd)
743    }
744
745    x;#[instrument(level = "debug", ret)]
746    fn open_drop_for_adt(
747        &mut self,
748        adt: ty::AdtDef<'tcx>,
749        args: GenericArgsRef<'tcx>,
750    ) -> BasicBlock {
751        if adt.variants().is_empty() {
752            return self.elaborator.patch().new_block(BasicBlockData::new(
753                Some(Terminator {
754                    source_info: self.source_info,
755                    kind: TerminatorKind::Unreachable,
756                }),
757                self.unwind.is_cleanup(),
758            ));
759        }
760
761        let skip_contents = adt.is_union() || adt.is_manually_drop();
762        let contents_drop = if skip_contents {
763            if adt.has_dtor(self.tcx()) && self.elaborator.get_drop_flag(self.path).is_some() {
764                // the top-level drop flag is usually cleared by open_drop_for_adt_contents
765                // types with destructors would still need an empty drop ladder to clear it
766
767                // however, these types are only open dropped in `DropShimElaborator`
768                // which does not have drop flags
769                // a future box-like "DerefMove" trait would allow for this case to happen
770                span_bug!(self.source_info.span, "open dropping partially moved union");
771            }
772
773            (self.succ, self.unwind, self.dropline)
774        } else {
775            self.open_drop_for_adt_contents(adt, args)
776        };
777
778        if adt.has_dtor(self.tcx()) {
779            let destructor_block = if adt.is_box() {
780                // we need to drop the inside of the box before running the destructor
781                let succ = self.destructor_call_block_sync((contents_drop.0, contents_drop.1));
782                let unwind = contents_drop
783                    .1
784                    .map(|unwind| self.destructor_call_block_sync((unwind, Unwind::InCleanup)));
785                let dropline = contents_drop
786                    .2
787                    .map(|dropline| self.destructor_call_block_sync((dropline, contents_drop.1)));
788                self.open_drop_for_box_contents(adt, args, succ, unwind, dropline)
789            } else {
790                self.destructor_call_block(contents_drop)
791            };
792
793            self.drop_flag_test_block(destructor_block, contents_drop.0, contents_drop.1)
794        } else {
795            contents_drop.0
796        }
797    }
798
799    fn open_drop_for_adt_contents(
800        &mut self,
801        adt: ty::AdtDef<'tcx>,
802        args: GenericArgsRef<'tcx>,
803    ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
804        let (succ, unwind, dropline) = self.drop_ladder_bottom();
805        if !adt.is_enum() {
806            let fields =
807                self.move_paths_for_fields(self.place, self.path, adt.variant(FIRST_VARIANT), args);
808            self.drop_ladder(fields, succ, unwind, dropline)
809        } else {
810            self.open_drop_for_multivariant(adt, args, succ, unwind, dropline)
811        }
812    }
813
814    fn open_drop_for_multivariant(
815        &mut self,
816        adt: ty::AdtDef<'tcx>,
817        args: GenericArgsRef<'tcx>,
818        succ: BasicBlock,
819        unwind: Unwind,
820        dropline: Option<BasicBlock>,
821    ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
822        let mut values = Vec::with_capacity(adt.variants().len());
823        let mut normal_blocks = Vec::with_capacity(adt.variants().len());
824        let mut unwind_blocks =
825            if unwind.is_cleanup() { None } else { Some(Vec::with_capacity(adt.variants().len())) };
826        let mut dropline_blocks =
827            if dropline.is_none() { None } else { Some(Vec::with_capacity(adt.variants().len())) };
828
829        let mut have_otherwise_with_drop_glue = false;
830        let mut have_otherwise = false;
831        let tcx = self.tcx();
832
833        for (variant_index, discr) in adt.discriminants(tcx) {
834            let variant = &adt.variant(variant_index);
835            let subpath = self.elaborator.downcast_subpath(self.path, variant_index);
836
837            if let Some(variant_path) = subpath {
838                let base_place = tcx.mk_place_elem(
839                    self.place,
840                    ProjectionElem::Downcast(Some(variant.name), variant_index),
841                );
842                let fields = self.move_paths_for_fields(base_place, variant_path, variant, args);
843                values.push(discr.val);
844                if let Unwind::To(unwind) = unwind {
845                    // We can't use the half-ladder from the original
846                    // drop ladder, because this breaks the
847                    // "funclet can't have 2 successor funclets"
848                    // requirement from MSVC:
849                    //
850                    //           switch       unwind-switch
851                    //          /      \         /        \
852                    //         v1.0    v2.0  v2.0-unwind  v1.0-unwind
853                    //         |        |      /             |
854                    //    v1.1-unwind  v2.1-unwind           |
855                    //      ^                                |
856                    //       \-------------------------------/
857                    //
858                    // Create a duplicate half-ladder to avoid that. We
859                    // could technically only do this on MSVC, but I
860                    // I want to minimize the divergence between MSVC
861                    // and non-MSVC.
862
863                    let unwind_blocks = unwind_blocks.as_mut().unwrap();
864                    let unwind_ladder = ::alloc::vec::from_elem(Unwind::InCleanup, fields.len() + 1)vec![Unwind::InCleanup; fields.len() + 1];
865                    let dropline_ladder: Vec<Option<BasicBlock>> = ::alloc::vec::from_elem(None, fields.len() + 1)vec![None; fields.len() + 1];
866                    let halfladder =
867                        self.drop_halfladder(&unwind_ladder, &dropline_ladder, unwind, &fields);
868                    unwind_blocks.push(halfladder.last().cloned().unwrap());
869                }
870                let (normal, _, drop_bb) = self.drop_ladder(fields, succ, unwind, dropline);
871                normal_blocks.push(normal);
872                if dropline.is_some() {
873                    dropline_blocks.as_mut().unwrap().push(drop_bb.unwrap());
874                }
875            } else {
876                have_otherwise = true;
877
878                let typing_env = self.elaborator.typing_env();
879                let have_field_with_drop_glue = variant
880                    .fields
881                    .iter()
882                    .any(|field| field.ty(tcx, args).needs_drop(tcx, typing_env));
883                if have_field_with_drop_glue {
884                    have_otherwise_with_drop_glue = true;
885                }
886            }
887        }
888
889        if !have_otherwise {
890            values.pop();
891        } else if !have_otherwise_with_drop_glue {
892            normal_blocks.push(self.goto_block(succ, unwind));
893            if let Unwind::To(unwind) = unwind {
894                unwind_blocks.as_mut().unwrap().push(self.goto_block(unwind, Unwind::InCleanup));
895            }
896        } else {
897            normal_blocks.push(self.drop_block(succ, unwind));
898            if let Unwind::To(unwind) = unwind {
899                unwind_blocks.as_mut().unwrap().push(self.drop_block(unwind, Unwind::InCleanup));
900            }
901        }
902
903        (
904            self.adt_switch_block(adt, normal_blocks, &values, succ, unwind),
905            unwind.map(|unwind| {
906                self.adt_switch_block(
907                    adt,
908                    unwind_blocks.unwrap(),
909                    &values,
910                    unwind,
911                    Unwind::InCleanup,
912                )
913            }),
914            dropline.map(|dropline| {
915                self.adt_switch_block(adt, dropline_blocks.unwrap(), &values, dropline, unwind)
916            }),
917        )
918    }
919
920    fn adt_switch_block(
921        &mut self,
922        adt: ty::AdtDef<'tcx>,
923        blocks: Vec<BasicBlock>,
924        values: &[u128],
925        succ: BasicBlock,
926        unwind: Unwind,
927    ) -> BasicBlock {
928        // If there are multiple variants, then if something
929        // is present within the enum the discriminant, tracked
930        // by the rest path, must be initialized.
931        //
932        // Additionally, we do not want to switch on the
933        // discriminant after it is free-ed, because that
934        // way lies only trouble.
935        let discr_ty = adt.repr().discr_type().to_ty(self.tcx());
936        let discr = Place::from(self.new_temp(discr_ty));
937        let discr_rv = Rvalue::Discriminant(self.place);
938        let switch_block = BasicBlockData::new_stmts(
939            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.assign(discr, discr_rv)]))vec![self.assign(discr, discr_rv)],
940            Some(Terminator {
941                source_info: self.source_info,
942                kind: TerminatorKind::SwitchInt {
943                    discr: Operand::Move(discr),
944                    targets: SwitchTargets::new(
945                        values.iter().copied().zip(blocks.iter().copied()),
946                        *blocks.last().unwrap(),
947                    ),
948                },
949            }),
950            unwind.is_cleanup(),
951        );
952        let switch_block = self.elaborator.patch().new_block(switch_block);
953        self.drop_flag_test_block(switch_block, succ, unwind)
954    }
955
956    fn destructor_call_block_sync(&mut self, (succ, unwind): (BasicBlock, Unwind)) -> BasicBlock {
957        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:957",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(957u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("destructor_call_block_sync({0:?}, {1:?})",
                                                    self, succ) as &dyn Value))])
            });
    } else { ; }
};debug!("destructor_call_block_sync({:?}, {:?})", self, succ);
958        let tcx = self.tcx();
959        let drop_trait = tcx.require_lang_item(LangItem::Drop, DUMMY_SP);
960        let drop_fn = tcx.associated_item_def_ids(drop_trait)[0];
961        let ty = self.place_ty(self.place);
962
963        let ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
964        let ref_place = self.new_temp(ref_ty);
965        let unit_temp = Place::from(self.new_temp(tcx.types.unit));
966
967        let result = BasicBlockData::new_stmts(
968            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.assign(Place::from(ref_place),
                    Rvalue::Ref(tcx.lifetimes.re_erased,
                        BorrowKind::Mut { kind: MutBorrowKind::Default },
                        self.place))]))vec![self.assign(
969                Place::from(ref_place),
970                Rvalue::Ref(
971                    tcx.lifetimes.re_erased,
972                    BorrowKind::Mut { kind: MutBorrowKind::Default },
973                    self.place,
974                ),
975            )],
976            Some(Terminator {
977                kind: TerminatorKind::Call {
978                    func: Operand::function_handle(
979                        tcx,
980                        drop_fn,
981                        [ty.into()],
982                        self.source_info.span,
983                    ),
984                    args: [Spanned { node: Operand::Move(Place::from(ref_place)), span: DUMMY_SP }]
985                        .into(),
986                    destination: unit_temp,
987                    target: Some(succ),
988                    unwind: unwind.into_action(),
989                    call_source: CallSource::Misc,
990                    fn_span: self.source_info.span,
991                },
992                source_info: self.source_info,
993            }),
994            unwind.is_cleanup(),
995        );
996
997        self.elaborator.patch().new_block(result)
998    }
999
1000    fn destructor_call_block(
1001        &mut self,
1002        (succ, unwind, dropline): (BasicBlock, Unwind, Option<BasicBlock>),
1003    ) -> BasicBlock {
1004        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:1004",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(1004u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("destructor_call_block({0:?}, {1:?})",
                                                    self, succ) as &dyn Value))])
            });
    } else { ; }
};debug!("destructor_call_block({:?}, {:?})", self, succ);
1005        let ty = self.place_ty(self.place);
1006        if self.tcx().features().async_drop()
1007            && self.elaborator.body().coroutine.is_some()
1008            && self.elaborator.allow_async_drops()
1009            && !unwind.is_cleanup()
1010            && ty.is_async_drop(self.tcx(), self.elaborator.typing_env())
1011        {
1012            self.build_async_drop(self.place, ty, None, succ, unwind, dropline, true)
1013        } else {
1014            self.destructor_call_block_sync((succ, unwind))
1015        }
1016    }
1017
1018    /// Create a loop that drops an array:
1019    ///
1020    /// ```text
1021    /// loop-block:
1022    ///    can_go = cur == len
1023    ///    if can_go then succ else drop-block
1024    /// drop-block:
1025    ///    ptr = &raw mut P[cur]
1026    ///    cur = cur + 1
1027    ///    drop(ptr)
1028    /// ```
1029    fn drop_loop(
1030        &mut self,
1031        succ: BasicBlock,
1032        cur: Local,
1033        len: Local,
1034        ety: Ty<'tcx>,
1035        unwind: Unwind,
1036        dropline: Option<BasicBlock>,
1037    ) -> BasicBlock {
1038        let copy = |place: Place<'tcx>| Operand::Copy(place);
1039        let move_ = |place: Place<'tcx>| Operand::Move(place);
1040        let tcx = self.tcx();
1041
1042        let ptr_ty = Ty::new_mut_ptr(tcx, ety);
1043        let ptr = Place::from(self.new_temp(ptr_ty));
1044        let can_go = Place::from(self.new_temp(tcx.types.bool));
1045        let one = self.constant_usize(1);
1046
1047        let drop_block = BasicBlockData::new_stmts(
1048            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.assign(ptr,
                    Rvalue::RawPtr(RawPtrKind::Mut,
                        tcx.mk_place_index(self.place, cur))),
                self.assign(cur.into(),
                    Rvalue::BinaryOp(BinOp::Add,
                        Box::new((move_(cur.into()), one))))]))vec![
1049                self.assign(
1050                    ptr,
1051                    Rvalue::RawPtr(RawPtrKind::Mut, tcx.mk_place_index(self.place, cur)),
1052                ),
1053                self.assign(
1054                    cur.into(),
1055                    Rvalue::BinaryOp(BinOp::Add, Box::new((move_(cur.into()), one))),
1056                ),
1057            ],
1058            Some(Terminator {
1059                source_info: self.source_info,
1060                // this gets overwritten by drop elaboration.
1061                kind: TerminatorKind::Unreachable,
1062            }),
1063            unwind.is_cleanup(),
1064        );
1065        let drop_block = self.elaborator.patch().new_block(drop_block);
1066
1067        let loop_block = BasicBlockData::new_stmts(
1068            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.assign(can_go,
                    Rvalue::BinaryOp(BinOp::Eq,
                        Box::new((copy(Place::from(cur)), copy(len.into())))))]))vec![self.assign(
1069                can_go,
1070                Rvalue::BinaryOp(BinOp::Eq, Box::new((copy(Place::from(cur)), copy(len.into())))),
1071            )],
1072            Some(Terminator {
1073                source_info: self.source_info,
1074                kind: TerminatorKind::if_(move_(can_go), succ, drop_block),
1075            }),
1076            unwind.is_cleanup(),
1077        );
1078        let loop_block = self.elaborator.patch().new_block(loop_block);
1079
1080        let place = tcx.mk_place_deref(ptr);
1081        if self.tcx().features().async_drop()
1082            && self.elaborator.body().coroutine.is_some()
1083            && self.elaborator.allow_async_drops()
1084            && !unwind.is_cleanup()
1085            && ety.needs_async_drop(self.tcx(), self.elaborator.typing_env())
1086        {
1087            self.build_async_drop(
1088                place,
1089                ety,
1090                Some(drop_block),
1091                loop_block,
1092                unwind,
1093                dropline,
1094                false,
1095            );
1096        } else {
1097            self.elaborator.patch().patch_terminator(
1098                drop_block,
1099                TerminatorKind::Drop {
1100                    place,
1101                    target: loop_block,
1102                    unwind: unwind.into_action(),
1103                    replace: false,
1104                    drop: None,
1105                    async_fut: None,
1106                },
1107            );
1108        }
1109        loop_block
1110    }
1111
1112    fn open_drop_for_array(
1113        &mut self,
1114        array_ty: Ty<'tcx>,
1115        ety: Ty<'tcx>,
1116        opt_size: Option<u64>,
1117    ) -> BasicBlock {
1118        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:1118",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(1118u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("open_drop_for_array({0:?}, {1:?}, {2:?})",
                                                    array_ty, ety, opt_size) as &dyn Value))])
            });
    } else { ; }
};debug!("open_drop_for_array({:?}, {:?}, {:?})", array_ty, ety, opt_size);
1119        let tcx = self.tcx();
1120
1121        if let Some(size) = opt_size {
1122            enum ProjectionKind<Path> {
1123                Drop(std::ops::Range<u64>),
1124                Keep(u64, Path),
1125            }
1126            // Previously, we'd make a projection for every element in the array and create a drop
1127            // ladder if any `array_subpath` was `Some`, i.e. moving out with an array pattern.
1128            // This caused huge memory usage when generating the drops for large arrays, so we instead
1129            // record the *subslices* which are dropped and the *indexes* which are kept
1130            let mut drop_ranges = ::alloc::vec::Vec::new()vec![];
1131            let mut dropping = true;
1132            let mut start = 0;
1133            for i in 0..size {
1134                let path = self.elaborator.array_subpath(self.path, i, size);
1135                if dropping && path.is_some() {
1136                    drop_ranges.push(ProjectionKind::Drop(start..i));
1137                    dropping = false;
1138                } else if !dropping && path.is_none() {
1139                    dropping = true;
1140                    start = i;
1141                }
1142                if let Some(path) = path {
1143                    drop_ranges.push(ProjectionKind::Keep(i, path));
1144                }
1145            }
1146            if !drop_ranges.is_empty() {
1147                if dropping {
1148                    drop_ranges.push(ProjectionKind::Drop(start..size));
1149                }
1150                let fields = drop_ranges
1151                    .iter()
1152                    .rev()
1153                    .map(|p| {
1154                        let (project, path) = match p {
1155                            ProjectionKind::Drop(r) => (
1156                                ProjectionElem::Subslice {
1157                                    from: r.start,
1158                                    to: r.end,
1159                                    from_end: false,
1160                                },
1161                                None,
1162                            ),
1163                            &ProjectionKind::Keep(offset, path) => (
1164                                ProjectionElem::ConstantIndex {
1165                                    offset,
1166                                    min_length: size,
1167                                    from_end: false,
1168                                },
1169                                Some(path),
1170                            ),
1171                        };
1172                        (tcx.mk_place_elem(self.place, project), path)
1173                    })
1174                    .collect::<Vec<_>>();
1175                let (succ, unwind, dropline) = self.drop_ladder_bottom();
1176                return self.drop_ladder(fields, succ, unwind, dropline).0;
1177            }
1178        }
1179
1180        let array_ptr_ty = Ty::new_mut_ptr(tcx, array_ty);
1181        let array_ptr = self.new_temp(array_ptr_ty);
1182
1183        let slice_ty = Ty::new_slice(tcx, ety);
1184        let slice_ptr_ty = Ty::new_mut_ptr(tcx, slice_ty);
1185        let slice_ptr = self.new_temp(slice_ptr_ty);
1186
1187        let mut delegate_block = BasicBlockData::new_stmts(
1188            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.assign(Place::from(array_ptr),
                    Rvalue::RawPtr(RawPtrKind::Mut, self.place)),
                self.assign(Place::from(slice_ptr),
                    Rvalue::Cast(CastKind::PointerCoercion(PointerCoercion::Unsize,
                            CoercionSource::Implicit),
                        Operand::Move(Place::from(array_ptr)), slice_ptr_ty))]))vec![
1189                self.assign(Place::from(array_ptr), Rvalue::RawPtr(RawPtrKind::Mut, self.place)),
1190                self.assign(
1191                    Place::from(slice_ptr),
1192                    Rvalue::Cast(
1193                        CastKind::PointerCoercion(
1194                            PointerCoercion::Unsize,
1195                            CoercionSource::Implicit,
1196                        ),
1197                        Operand::Move(Place::from(array_ptr)),
1198                        slice_ptr_ty,
1199                    ),
1200                ),
1201            ],
1202            None,
1203            self.unwind.is_cleanup(),
1204        );
1205
1206        let array_place = mem::replace(
1207            &mut self.place,
1208            Place::from(slice_ptr).project_deeper(&[PlaceElem::Deref], tcx),
1209        );
1210        let slice_block = self.drop_loop_trio_for_slice(ety);
1211        self.place = array_place;
1212
1213        delegate_block.terminator = Some(Terminator {
1214            source_info: self.source_info,
1215            kind: TerminatorKind::Goto { target: slice_block },
1216        });
1217        self.elaborator.patch().new_block(delegate_block)
1218    }
1219
1220    /// Creates a trio of drop-loops of `place`, which drops its contents, even
1221    /// in the case of 1 panic or in the case of coroutine drop
1222    fn drop_loop_trio_for_slice(&mut self, ety: Ty<'tcx>) -> BasicBlock {
1223        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:1223",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(1223u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("drop_loop_trio_for_slice({0:?})",
                                                    ety) as &dyn Value))])
            });
    } else { ; }
};debug!("drop_loop_trio_for_slice({:?})", ety);
1224        let tcx = self.tcx();
1225        let len = self.new_temp(tcx.types.usize);
1226        let cur = self.new_temp(tcx.types.usize);
1227
1228        let unwind = self
1229            .unwind
1230            .map(|unwind| self.drop_loop(unwind, cur, len, ety, Unwind::InCleanup, None));
1231
1232        let dropline =
1233            self.dropline.map(|dropline| self.drop_loop(dropline, cur, len, ety, unwind, None));
1234
1235        let loop_block = self.drop_loop(self.succ, cur, len, ety, unwind, dropline);
1236
1237        let [PlaceElem::Deref] = self.place.projection.as_slice() else {
1238            ::rustc_middle::util::bug::span_bug_fmt(self.source_info.span,
    format_args!("Expected place for slice drop shim to be *_n, but it\'s {0:?}",
        self.place));span_bug!(
1239                self.source_info.span,
1240                "Expected place for slice drop shim to be *_n, but it's {:?}",
1241                self.place,
1242            );
1243        };
1244
1245        let zero = self.constant_usize(0);
1246        let block = BasicBlockData::new_stmts(
1247            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.assign(len.into(),
                    Rvalue::UnaryOp(UnOp::PtrMetadata,
                        Operand::Copy(Place::from(self.place.local)))),
                self.assign(cur.into(), Rvalue::Use(zero))]))vec![
1248                self.assign(
1249                    len.into(),
1250                    Rvalue::UnaryOp(
1251                        UnOp::PtrMetadata,
1252                        Operand::Copy(Place::from(self.place.local)),
1253                    ),
1254                ),
1255                self.assign(cur.into(), Rvalue::Use(zero)),
1256            ],
1257            Some(Terminator {
1258                source_info: self.source_info,
1259                kind: TerminatorKind::Goto { target: loop_block },
1260            }),
1261            unwind.is_cleanup(),
1262        );
1263
1264        let drop_block = self.elaborator.patch().new_block(block);
1265        // FIXME(#34708): handle partially-dropped array/slice elements.
1266        let reset_block = self.drop_flag_reset_block(DropFlagMode::Deep, drop_block, unwind);
1267        self.drop_flag_test_block(reset_block, self.succ, unwind)
1268    }
1269
1270    /// The slow-path - create an "open", elaborated drop for a type
1271    /// which is moved-out-of only partially, and patch `bb` to a jump
1272    /// to it. This must not be called on ADTs with a destructor,
1273    /// as these can't be moved-out-of, except for `Box<T>`, which is
1274    /// special-cased.
1275    ///
1276    /// This creates a "drop ladder" that drops the needed fields of the
1277    /// ADT, both in the success case or if one of the destructors fail.
1278    fn open_drop(&mut self) -> BasicBlock {
1279        let ty = self.place_ty(self.place);
1280        match ty.kind() {
1281            ty::Closure(_, args) => self.open_drop_for_tuple(args.as_closure().upvar_tys()),
1282            ty::CoroutineClosure(_, args) => {
1283                self.open_drop_for_tuple(args.as_coroutine_closure().upvar_tys())
1284            }
1285            // Note that `elaborate_drops` only drops the upvars of a coroutine,
1286            // and this is ok because `open_drop` here can only be reached
1287            // within that own coroutine's resume function.
1288            // This should only happen for the self argument on the resume function.
1289            // It effectively only contains upvars until the coroutine transformation runs.
1290            // See librustc_body/transform/coroutine.rs for more details.
1291            ty::Coroutine(_, args) => self.open_drop_for_tuple(args.as_coroutine().upvar_tys()),
1292            ty::Tuple(fields) => self.open_drop_for_tuple(fields),
1293            ty::Adt(def, args) => self.open_drop_for_adt(*def, args),
1294            ty::Dynamic(..) => self.complete_drop(self.succ, self.unwind),
1295            ty::Array(ety, size) => {
1296                let size = size.try_to_target_usize(self.tcx());
1297                self.open_drop_for_array(ty, *ety, size)
1298            }
1299            ty::Slice(ety) => self.drop_loop_trio_for_slice(*ety),
1300
1301            ty::UnsafeBinder(_) => {
1302                // Unsafe binders may elaborate drops if their inner type isn't copy.
1303                // This is enforced in typeck, so this should never happen.
1304                self.tcx().dcx().span_delayed_bug(
1305                    self.source_info.span,
1306                    "open drop for unsafe binder shouldn't be encountered",
1307                );
1308                self.elaborator.patch().new_block(BasicBlockData::new(
1309                    Some(Terminator {
1310                        source_info: self.source_info,
1311                        kind: TerminatorKind::Unreachable,
1312                    }),
1313                    self.unwind.is_cleanup(),
1314                ))
1315            }
1316
1317            _ => ::rustc_middle::util::bug::span_bug_fmt(self.source_info.span,
    format_args!("open drop from non-ADT `{0:?}`", ty))span_bug!(self.source_info.span, "open drop from non-ADT `{:?}`", ty),
1318        }
1319    }
1320
1321    fn complete_drop(&mut self, succ: BasicBlock, unwind: Unwind) -> BasicBlock {
1322        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:1322",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(1322u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("complete_drop(succ={0:?}, unwind={1:?})",
                                                    succ, unwind) as &dyn Value))])
            });
    } else { ; }
};debug!("complete_drop(succ={:?}, unwind={:?})", succ, unwind);
1323
1324        let drop_block = self.drop_block(succ, unwind);
1325
1326        self.drop_flag_test_block(drop_block, succ, unwind)
1327    }
1328
1329    /// Creates a block that resets the drop flag. If `mode` is deep, all children drop flags will
1330    /// also be cleared.
1331    fn drop_flag_reset_block(
1332        &mut self,
1333        mode: DropFlagMode,
1334        succ: BasicBlock,
1335        unwind: Unwind,
1336    ) -> BasicBlock {
1337        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:1337",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(1337u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("drop_flag_reset_block({0:?},{1:?})",
                                                    self, mode) as &dyn Value))])
            });
    } else { ; }
};debug!("drop_flag_reset_block({:?},{:?})", self, mode);
1338
1339        if unwind.is_cleanup() {
1340            // The drop flag isn't read again on the unwind path, so don't
1341            // bother setting it.
1342            return succ;
1343        }
1344        let block = self.new_block(unwind, TerminatorKind::Goto { target: succ });
1345        let block_start = Location { block, statement_index: 0 };
1346        self.elaborator.clear_drop_flag(block_start, self.path, mode);
1347        block
1348    }
1349
1350    fn elaborated_drop_block(&mut self) -> BasicBlock {
1351        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:1351",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(1351u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("elaborated_drop_block({0:?})",
                                                    self) as &dyn Value))])
            });
    } else { ; }
};debug!("elaborated_drop_block({:?})", self);
1352        let blk = self.drop_block_simple(self.succ, self.unwind);
1353        self.elaborate_drop(blk);
1354        blk
1355    }
1356
1357    fn drop_block_simple(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
1358        let block = TerminatorKind::Drop {
1359            place: self.place,
1360            target,
1361            unwind: unwind.into_action(),
1362            replace: false,
1363            drop: self.dropline,
1364            async_fut: None,
1365        };
1366        self.new_block(unwind, block)
1367    }
1368
1369    fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
1370        let drop_ty = self.place_ty(self.place);
1371        if self.tcx().features().async_drop()
1372            && self.elaborator.body().coroutine.is_some()
1373            && self.elaborator.allow_async_drops()
1374            && !unwind.is_cleanup()
1375            && drop_ty.needs_async_drop(self.tcx(), self.elaborator.typing_env())
1376        {
1377            self.build_async_drop(
1378                self.place,
1379                drop_ty,
1380                None,
1381                self.succ,
1382                unwind,
1383                self.dropline,
1384                false,
1385            )
1386        } else {
1387            let block = TerminatorKind::Drop {
1388                place: self.place,
1389                target,
1390                unwind: unwind.into_action(),
1391                replace: false,
1392                drop: None,
1393                async_fut: None,
1394            };
1395            self.new_block(unwind, block)
1396        }
1397    }
1398
1399    fn goto_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
1400        let block = TerminatorKind::Goto { target };
1401        self.new_block(unwind, block)
1402    }
1403
1404    /// Returns the block to jump to in order to test the drop flag and execute the drop.
1405    ///
1406    /// Depending on the required `DropStyle`, this might be a generated block with an `if`
1407    /// terminator (for dynamic/open drops), or it might be `on_set` or `on_unset` itself, in case
1408    /// the drop can be statically determined.
1409    fn drop_flag_test_block(
1410        &mut self,
1411        on_set: BasicBlock,
1412        on_unset: BasicBlock,
1413        unwind: Unwind,
1414    ) -> BasicBlock {
1415        let style = self.elaborator.drop_style(self.path, DropFlagMode::Shallow);
1416        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/elaborate_drop.rs:1416",
                        "rustc_mir_transform::elaborate_drop",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
                        ::tracing_core::__macro_support::Option::Some(1416u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("drop_flag_test_block({0:?},{1:?},{2:?},{3:?}) - {4:?}",
                                                    self, on_set, on_unset, unwind, style) as &dyn Value))])
            });
    } else { ; }
};debug!(
1417            "drop_flag_test_block({:?},{:?},{:?},{:?}) - {:?}",
1418            self, on_set, on_unset, unwind, style
1419        );
1420
1421        match style {
1422            DropStyle::Dead => on_unset,
1423            DropStyle::Static => on_set,
1424            DropStyle::Conditional | DropStyle::Open => {
1425                let flag = self.elaborator.get_drop_flag(self.path).unwrap();
1426                let term = TerminatorKind::if_(flag, on_set, on_unset);
1427                self.new_block(unwind, term)
1428            }
1429        }
1430    }
1431
1432    fn new_block(&mut self, unwind: Unwind, k: TerminatorKind<'tcx>) -> BasicBlock {
1433        self.elaborator.patch().new_block(BasicBlockData::new(
1434            Some(Terminator { source_info: self.source_info, kind: k }),
1435            unwind.is_cleanup(),
1436        ))
1437    }
1438
1439    fn new_block_with_statements(
1440        &mut self,
1441        unwind: Unwind,
1442        statements: Vec<Statement<'tcx>>,
1443        k: TerminatorKind<'tcx>,
1444    ) -> BasicBlock {
1445        self.elaborator.patch().new_block(BasicBlockData::new_stmts(
1446            statements,
1447            Some(Terminator { source_info: self.source_info, kind: k }),
1448            unwind.is_cleanup(),
1449        ))
1450    }
1451
1452    fn new_temp(&mut self, ty: Ty<'tcx>) -> Local {
1453        self.elaborator.patch().new_temp(ty, self.source_info.span)
1454    }
1455
1456    fn constant_usize(&self, val: u16) -> Operand<'tcx> {
1457        Operand::Constant(Box::new(ConstOperand {
1458            span: self.source_info.span,
1459            user_ty: None,
1460            const_: Const::from_usize(self.tcx(), val.into()),
1461        }))
1462    }
1463
1464    fn assign(&self, lhs: Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> {
1465        Statement::new(self.source_info, StatementKind::Assign(Box::new((lhs, rhs))))
1466    }
1467}