rustc_mir_transform/
elaborate_drop.rs

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