Skip to main content

rustc_mir_transform/
elaborate_drop.rs

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