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