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_data_structures::thin_vec::ThinVec;
5use rustc_hir::lang_items::LangItem;
6use rustc_hir::{CoroutineDesugaring, CoroutineKind};
7use rustc_index::Idx;
8use rustc_middle::mir::*;
9use rustc_middle::ty::adjustment::PointerCoercion;
10use rustc_middle::ty::util::{Discr, IntTypeExt};
11use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt};
12use rustc_middle::{bug, span_bug};
13use rustc_span::{DUMMY_SP, dummy_spanned};
14use tracing::{debug, instrument};
15
16use crate::coroutine::CTX_ARG;
17use crate::patch::MirPatch;
18
19/// Describes how/if a value should be dropped.
20#[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)]
21pub(crate) enum DropStyle {
22    /// The value is already dead at the drop location, no drop will be executed.
23    Dead,
24
25    /// The value is known to always be initialized at the drop location, drop will always be
26    /// executed.
27    Static,
28
29    /// Whether the value needs to be dropped depends on its drop flag.
30    Conditional,
31
32    /// An "open" drop is one where only the fields of a value are dropped.
33    ///
34    /// For example, this happens when moving out of a struct field: The rest of the struct will be
35    /// dropped in such an "open" drop. It is also used to generate drop glue for the individual
36    /// components of a value, for example for dropping array elements.
37    Open,
38}
39
40/// Which drop flags to affect/check with an operation.
41#[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)]
42pub(crate) enum DropFlagMode {
43    /// Only affect the top-level drop flag, not that of any contained fields.
44    Shallow,
45    /// Affect all nested drop flags in addition to the top-level one.
46    Deep,
47}
48
49/// Describes if unwinding is necessary and where to unwind to if a panic occurs.
50#[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)]
51pub(crate) enum Unwind {
52    /// Unwind to this block.
53    To(BasicBlock),
54    /// Already in an unwind path, any panic will cause an abort.
55    InCleanup,
56}
57
58impl Unwind {
59    fn is_cleanup(self) -> bool {
60        match self {
61            Unwind::To(..) => false,
62            Unwind::InCleanup => true,
63        }
64    }
65
66    fn into_action(self) -> UnwindAction {
67        match self {
68            Unwind::To(bb) => UnwindAction::Cleanup(bb),
69            Unwind::InCleanup => UnwindAction::Terminate(UnwindTerminateReason::InCleanup),
70        }
71    }
72
73    fn map<F>(self, f: F) -> Self
74    where
75        F: FnOnce(BasicBlock) -> BasicBlock,
76    {
77        match self {
78            Unwind::To(bb) => Unwind::To(f(bb)),
79            Unwind::InCleanup => Unwind::InCleanup,
80        }
81    }
82}
83
84pub(crate) trait DropElaborator<'a, 'tcx>: fmt::Debug {
85    /// The type representing paths that can be moved out of.
86    ///
87    /// Users can move out of individual fields of a struct, such as `a.b.c`. This type is used to
88    /// represent such move paths. Sometimes tracking individual move paths is not necessary, in
89    /// which case this may be set to (for example) `()`.
90    type Path: Copy + fmt::Debug;
91
92    // Accessors
93
94    fn patch_ref(&self) -> &MirPatch<'tcx>;
95    fn patch(&mut self) -> &mut MirPatch<'tcx>;
96    fn body(&self) -> &'a Body<'tcx>;
97    fn tcx(&self) -> TyCtxt<'tcx>;
98    fn typing_env(&self) -> ty::TypingEnv<'tcx>;
99    fn allow_async_drops(&self) -> bool;
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    /// Async-drop `place: drop_ty`.
204    ///
205    /// Conceptually, we want to run `async_drop_in_place(&mut obj).await`.
206    ///
207    /// Await syntax does not exist in MIR, so we need to manually expand it into a poll-yield
208    /// loop, essentially:
209    /// ```mir
210    ///   let fut = async_drop_in_place(&mut obj);
211    ///   loop {
212    ///     let pin_fut = Pin::new_unchecked(&mut fut);
213    ///     match Future::poll(pin_fut, CTX_ARG) {
214    ///       Poll::Ready => break,
215    ///       Poll::Pending(..) => CTX_ARG = yield (),
216    ///     }
217    ///   }
218    ///   // continue to `succ`
219    /// ```
220    ///
221    /// We also need to ensure that async drop also happens on the coroutine drop path, ie. when
222    /// `yield` branches along its `drop` target. This requires a second loop, this time jumping to
223    /// `dropline`.
224    ///
225    /// Arguments:
226    ///   `call_destructor_only`: call only `AsyncDrop::drop`, not full `async_drop_in_place` glue
227    x;#[instrument(level = "debug", skip(self), ret)]
228    fn build_async_drop(
229        &mut self,
230        place: Place<'tcx>,
231        drop_ty: Ty<'tcx>,
232        succ: BasicBlock,
233        unwind: Unwind,
234        dropline: Option<BasicBlock>,
235        call_destructor_only: bool,
236    ) -> BasicBlock {
237        let tcx = self.tcx();
238        let span = self.source_info.span;
239        let obj_ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, drop_ty);
240
241        let async_drop_fn_def_id = if call_destructor_only {
242            // Resolving obj.<AsyncDrop::drop>()
243            let async_drop_trait = tcx.require_lang_item(LangItem::AsyncDrop, span);
244            tcx.associated_item_def_ids(async_drop_trait)[0]
245        } else {
246            // Resolving async_drop_in_place<T> function for drop_ty
247            tcx.require_lang_item(LangItem::AsyncDropInPlace, span)
248        };
249
250        let fut_ty = tcx
251            .instantiate_bound_regions_with_erased(
252                Ty::new_fn_def(tcx, async_drop_fn_def_id, [drop_ty]).fn_sig(tcx),
253            )
254            .output();
255        let fut = self.new_temp(fut_ty);
256
257        // Create an intermediate block that does StorageDead(fut) then jumps to succ.
258        // This is necessary because we do not want to modify statements
259        // in existing blocks, in case those are used somewhere else in MIR.
260        let succ_with_dead = self.new_block_with_statements(
261            unwind,
262            vec![self.storage_dead(fut)],
263            TerminatorKind::Goto { target: succ },
264        );
265        let dropline_with_dead = dropline.map(|target| {
266            self.new_block_with_statements(
267                unwind,
268                vec![self.storage_dead(fut)],
269                TerminatorKind::Goto { target },
270            )
271        });
272        let unwind_with_dead = unwind.map(|target| {
273            self.new_block_with_statements(
274                Unwind::InCleanup,
275                vec![self.storage_dead(fut)],
276                TerminatorKind::Goto { target },
277            )
278        });
279
280        // The yielded value depends on the kind of coroutine, to match what AST lowering does.
281        let coroutine_kind = self.elaborator.body().coroutine_kind().unwrap();
282        let yield_value = match coroutine_kind {
283            // For async gen, we need `yield Poll<OptRet>::Pending`.
284            CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _) => {
285                let full_yield_ty = self.elaborator.body().yield_ty().unwrap();
286                let ty::Adt(_poll_adt, args) = *full_yield_ty.kind() else { bug!() };
287                let ty::Adt(_option_adt, args) = *args.type_at(0).kind() else { bug!() };
288                let yield_ty = args.type_at(0);
289                Operand::unevaluated_constant(
290                    tcx,
291                    tcx.require_lang_item(LangItem::AsyncGenPending, span),
292                    tcx.mk_args(&[yield_ty.into()]),
293                    span,
294                )
295            }
296            // For regular async fn, we need `yield ()`.
297            CoroutineKind::Desugared(CoroutineDesugaring::Async, _) => {
298                Operand::zero_sized_constant(tcx.types.unit, span)
299            }
300            // `is_async_drop` should have checked that.
301            _ => panic!("unexpected coroutine for async drop {coroutine_kind:?}"),
302        };
303
304        // The branching here is tricky and deserves some explanation.
305        //
306        // If we are in the drop code path, ie. we are currently dropping the coroutine.
307        // The state machine follows the `drop` branch in the `yield` terminator.
308        // To repeatedly poll the future, the `drop` branch must loop.
309        // Meanwhile, the `resume` branch corresponds to anomalous execution,
310        // trying to resume the coroutine while it is being dropped. So that branch panics
311        // (`panic_bb`).
312        let panic_bb = self.build_resumed_after_drop_abort_block(unwind_with_dead, coroutine_kind);
313        let (drop_pin_bb, drop_resume_bb, drop_drop_bb) = self.build_pin_poll_yield_loop(
314            CTX_ARG.into(),
315            fut.into(),
316            yield_value.clone(),
317            // If `dropline_with_dead` is set, it points to the continuation of the drop execution.
318            // Otherwise, we are already dropping the coroutine, and `succ_with_dead` does.
319            dropline_with_dead.unwrap_or(succ_with_dead),
320            unwind_with_dead,
321        );
322        self.elaborator
323            .patch()
324            .patch_terminator(drop_resume_bb, TerminatorKind::Goto { target: panic_bb });
325        self.elaborator
326            .patch()
327            .patch_terminator(drop_drop_bb, TerminatorKind::Goto { target: drop_pin_bb });
328
329        // If we are in the regular code path, `dropline_with_dead` is `Some`.
330        //
331        // In that case, the logic is reversed. Normal execution branches on `resume` from the
332        // `yield` terminator. To repeatedly poll the future, that `resume` branch must loop.
333        // When the future is dropped, the `yield` terminator branches to `drop`, which follows to
334        // the previous loop `drop_pin_bb`.
335        let succ_yield_loop = if dropline_with_dead.is_some() {
336            let (pin_bb, resume_bb, drop_bb) = self.build_pin_poll_yield_loop(
337                CTX_ARG.into(),
338                fut.into(),
339                yield_value,
340                // `dropline_with_dead` is `Some`, so the previous loop point to it.
341                succ_with_dead,
342                unwind_with_dead,
343            );
344            self.elaborator
345                .patch()
346                .patch_terminator(resume_bb, TerminatorKind::Goto { target: pin_bb });
347            self.elaborator
348                .patch()
349                .patch_terminator(drop_bb, TerminatorKind::Goto { target: drop_pin_bb });
350            pin_bb
351        } else {
352            // We were already in the drop line, so return the loop we created for it.
353            drop_pin_bb
354        };
355
356        // #2:call_drop_bb >>>
357        //    call AsyncDrop::drop(pin_obj)
358        // OR call async_drop_in_place(pin_obj.pointer)
359        let pin_adt_def = tcx.adt_def(tcx.require_lang_item(LangItem::Pin, span));
360        let pin_obj_ty = Ty::new_adt(tcx, pin_adt_def, tcx.mk_args(&[obj_ref_ty.into()]));
361        // Where we store the result of Pin<&drop_ty>::new_unchecked(&mut place).
362        let pin_obj_local = self.new_temp(pin_obj_ty);
363        let drop_arg = if call_destructor_only {
364            // `AsyncDrop::drop` takes `self: Pin<&mut Self>`.
365            Operand::Move(pin_obj_local.into())
366        } else {
367            // `async_drop_in_place` takes `obj: &mut T`.
368            Operand::Copy(tcx.mk_place_field(pin_obj_local.into(), FieldIdx::ZERO, obj_ref_ty))
369        };
370        let call_drop_bb = self.new_block_with_statements(
371            unwind_with_dead,
372            vec![self.storage_live(fut)],
373            TerminatorKind::Call {
374                func: Operand::function_handle(tcx, async_drop_fn_def_id, [drop_ty.into()], span),
375                args: [dummy_spanned(drop_arg)].into(),
376                destination: fut.into(),
377                target: Some(succ_yield_loop),
378                unwind: unwind_with_dead.into_action(),
379                call_source: CallSource::Misc,
380                fn_span: self.source_info.span,
381            },
382        );
383
384        // #1:pin_obj_bb >>> call Pin<ObjTy>::new_unchecked(&mut obj)
385        let obj_ref_place = Place::from(self.new_temp(obj_ref_ty));
386        let pin_obj_new_unchecked_fn = tcx.require_lang_item(LangItem::PinNewUnchecked, span);
387        let assign_obj_ref_place = self.assign(
388            obj_ref_place,
389            Rvalue::Ref(
390                tcx.lifetimes.re_erased,
391                BorrowKind::Mut { kind: MutBorrowKind::Default },
392                place,
393            ),
394        );
395        self.new_block_with_statements(
396            unwind,
397            vec![assign_obj_ref_place],
398            TerminatorKind::Call {
399                func: Operand::function_handle(
400                    tcx,
401                    pin_obj_new_unchecked_fn,
402                    [obj_ref_ty.into()],
403                    span,
404                ),
405                args: [dummy_spanned(Operand::Move(obj_ref_place))].into(),
406                destination: pin_obj_local.into(),
407                target: Some(call_drop_bb),
408                unwind: unwind.into_action(),
409                call_source: CallSource::Misc,
410                fn_span: span,
411            },
412        )
413    }
414
415    fn build_resumed_after_drop_abort_block(
416        &mut self,
417        unwind: Unwind,
418        coroutine_kind: CoroutineKind,
419    ) -> BasicBlock {
420        let tcx = self.tcx();
421        let panic_bb = self.new_block(unwind, TerminatorKind::Unreachable);
422        let msg = AssertMessage::ResumedAfterDrop(coroutine_kind);
423        let false_op = Operand::Constant(Box::new(ConstOperand {
424            span: self.source_info.span,
425            user_ty: None,
426            const_: Const::from_bool(tcx, false),
427        }));
428        self.elaborator.patch().patch_terminator(
429            panic_bb,
430            TerminatorKind::Assert {
431                cond: false_op,
432                expected: true,
433                msg: Box::new(msg),
434                target: panic_bb,
435                unwind: unwind.into_action(),
436            },
437        );
438        panic_bb
439    }
440
441    /// Build a small MIR loop that pins and polls a future, yielding when
442    /// the future returns `Poll::Pending` and continuing to `ready_target`
443    /// when it returns `Poll::Ready`.
444    ///
445    /// Pseudo-code:
446    /// ```mir
447    /// pin_bb:
448    ///   let pin_fut = Pin::new_unchecked(&mut fut_place);
449    ///   match Future::poll(pin_fut, CTX_ARG) {
450    ///     Poll::Ready => goto succ,
451    ///     Poll::Pending(..) => CTX_ARG = yield () [resume: resume_bb, drop: drop_bb],
452    ///   }
453    /// ```
454    ///
455    ///  Returns: the tuple `(pin_bb, resume_bb, drop_bb)`.
456    x;#[instrument(level = "trace", skip(self), ret)]
457    fn build_pin_poll_yield_loop(
458        &mut self,
459        resume_place: Place<'tcx>,
460        fut_place: Place<'tcx>,
461        yield_value: Operand<'tcx>,
462        succ: BasicBlock,
463        unwind: Unwind,
464    ) -> (BasicBlock, BasicBlock, BasicBlock) {
465        let tcx = self.tcx();
466        let source_info = self.source_info;
467
468        let resume_arg_ty = resume_place.ty(self.elaborator.body(), tcx).ty;
469        let context_ref_ty = Ty::new_task_context(tcx);
470
471        let poll_adt_def = tcx.adt_def(tcx.require_lang_item(LangItem::Poll, source_info.span));
472        let poll_enum = Ty::new_adt(tcx, poll_adt_def, tcx.mk_args(&[tcx.types.unit.into()]));
473
474        let fut_ty = self.elaborator.patch_ref().local_ty(fut_place.local);
475        let fut_ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, fut_ty);
476
477        let pin_adt_def = tcx.adt_def(tcx.require_lang_item(LangItem::Pin, source_info.span));
478        let fut_pin_ty = Ty::new_adt(tcx, pin_adt_def, tcx.mk_args(&[fut_ref_ty.into()]));
479
480        // Coroutine `transform_async_context` assumes that the local `resume_arg` to a yield
481        // is not used once, so create a special temp for it.
482        let yield_resume_local = self.new_temp(resume_arg_ty);
483        let resume_bb = self.new_block_with_statements(
484            unwind,
485            vec![
486                self.assign(
487                    resume_place,
488                    Rvalue::Use(Operand::Move(yield_resume_local.into()), WithRetag::Yes),
489                ),
490                self.storage_dead(yield_resume_local),
491            ],
492            // This will be transformed by the caller.
493            TerminatorKind::Unreachable,
494        );
495        let dropline_bb = self.new_block_with_statements(
496            unwind,
497            vec![
498                self.assign(
499                    resume_place,
500                    Rvalue::Use(Operand::Move(yield_resume_local.into()), WithRetag::Yes),
501                ),
502                self.storage_dead(yield_resume_local),
503            ],
504            // This will be transformed by the caller.
505            TerminatorKind::Unreachable,
506        );
507        let yield_bb = self.new_block_with_statements(
508            unwind,
509            vec![self.storage_live(yield_resume_local)],
510            TerminatorKind::Yield {
511                value: yield_value,
512                resume: resume_bb,
513                resume_arg: yield_resume_local.into(),
514                drop: Some(dropline_bb),
515            },
516        );
517
518        let poll_unit_local = self.new_temp(poll_enum);
519        let switch_bb = {
520            let poll_ready_variant =
521                tcx.require_lang_item(LangItem::PollReady, self.source_info.span);
522            let poll_ready_variant_idx = poll_adt_def.variant_index_with_id(poll_ready_variant);
523            let poll_pending_variant =
524                tcx.require_lang_item(LangItem::PollPending, self.source_info.span);
525            let poll_pending_variant_idx = poll_adt_def.variant_index_with_id(poll_pending_variant);
526
527            let Discr { val: poll_ready_discr, ty: poll_discr_ty } =
528                poll_enum.discriminant_for_variant(tcx, poll_ready_variant_idx).unwrap();
529            let Discr { val: poll_pending_discr, ty: _ } =
530                poll_enum.discriminant_for_variant(tcx, poll_pending_variant_idx).unwrap();
531
532            let poll_discr_local = self.new_temp(poll_discr_ty);
533            let otherwise_bb = self.elaborator.patch().unreachable_no_cleanup_block();
534            self.new_block_with_statements(
535                unwind,
536                vec![
537                    self.assign(
538                        poll_discr_local.into(),
539                        Rvalue::Discriminant(poll_unit_local.into()),
540                    ),
541                ],
542                TerminatorKind::SwitchInt {
543                    discr: Operand::Move(poll_discr_local.into()),
544                    targets: SwitchTargets::new(
545                        [
546                            // on `Ready`, exit the loop, jump to `succ`
547                            (poll_ready_discr, succ),
548                            // on `Pending`, yield and resume back into the loop
549                            (poll_pending_discr, yield_bb),
550                        ]
551                        .into_iter(),
552                        // otherwise: unreachable
553                        otherwise_bb,
554                    ),
555                },
556            )
557        };
558
559        let fut_pin_local = self.new_temp(fut_pin_ty);
560        let context_ref_local = self.new_temp(context_ref_ty);
561
562        let poll_fn = tcx.require_lang_item(LangItem::FuturePoll, source_info.span);
563        let poll_bb = self.new_block_with_statements(
564            unwind,
565            Vec::new(),
566            TerminatorKind::Call {
567                func: Operand::function_handle(tcx, poll_fn, [fut_ty.into()], source_info.span),
568                args: [
569                    dummy_spanned(Operand::Move(fut_pin_local.into())),
570                    dummy_spanned(Operand::Move(context_ref_local.into())),
571                ]
572                .into(),
573                destination: poll_unit_local.into(),
574                target: Some(switch_bb),
575                unwind: unwind.into_action(),
576                call_source: CallSource::Misc,
577                fn_span: source_info.span,
578            },
579        );
580
581        let get_context_fn = tcx.require_lang_item(LangItem::GetContext, source_info.span);
582        let get_context_bb = {
583            // Coroutine `transform_async_context` assumes that the local argument to `GetContext`
584            // is not used once, so create a special temp for it.
585            let entry_resume_local = self.new_temp(resume_arg_ty);
586            self.new_block_with_statements(
587                unwind,
588                vec![self.assign(
589                    entry_resume_local.into(),
590                    Rvalue::Use(Operand::Move(resume_place), WithRetag::Yes),
591                )],
592                TerminatorKind::Call {
593                    func: Operand::function_handle(
594                        tcx,
595                        get_context_fn,
596                        [tcx.lifetimes.re_erased.into(), tcx.lifetimes.re_erased.into()],
597                        source_info.span,
598                    ),
599                    args: [dummy_spanned(Operand::Move(entry_resume_local.into()))].into(),
600                    destination: context_ref_local.into(),
601                    target: Some(poll_bb),
602                    unwind: unwind.into_action(),
603                    call_source: CallSource::Misc,
604                    fn_span: source_info.span,
605                },
606            )
607        };
608
609        let fut_ref_local = self.new_temp(fut_ref_ty);
610        let fut_pin_new_unchecked_fn =
611            tcx.require_lang_item(LangItem::PinNewUnchecked, source_info.span);
612        let pin_bb = self.new_block_with_statements(
613            unwind,
614            vec![self.assign(
615                fut_ref_local.into(),
616                Rvalue::Ref(
617                    tcx.lifetimes.re_erased,
618                    BorrowKind::Mut { kind: MutBorrowKind::Default },
619                    fut_place,
620                ),
621            )],
622            TerminatorKind::Call {
623                func: Operand::function_handle(
624                    tcx,
625                    fut_pin_new_unchecked_fn,
626                    [fut_ref_ty.into()],
627                    source_info.span,
628                ),
629                args: [dummy_spanned(Operand::Move(fut_ref_local.into()))].into(),
630                destination: fut_pin_local.into(),
631                target: Some(get_context_bb),
632                unwind: unwind.into_action(),
633                call_source: CallSource::Misc,
634                fn_span: source_info.span,
635            },
636        );
637
638        (pin_bb, resume_bb, dropline_bb)
639    }
640
641    fn build_drop(&mut self, bb: BasicBlock) {
642        let drop_ty = self.place_ty(self.place);
643        if !self.elaborator.patch_ref().block(self.elaborator.body(), bb).is_cleanup
644            && self.check_if_can_async_drop(drop_ty, false)
645        {
646            let async_drop_bb = self.build_async_drop(
647                self.place,
648                drop_ty,
649                self.succ,
650                self.unwind,
651                self.dropline,
652                false,
653            );
654            self.elaborator
655                .patch()
656                .patch_terminator(bb, TerminatorKind::Goto { target: async_drop_bb });
657        } else {
658            self.elaborator.patch().patch_terminator(
659                bb,
660                TerminatorKind::Drop {
661                    place: self.place,
662                    target: self.succ,
663                    unwind: self.unwind.into_action(),
664                    replace: false,
665                    drop: None,
666                },
667            );
668        }
669    }
670
671    /// Function to check if we can generate an async drop here
672    fn check_if_can_async_drop(&mut self, drop_ty: Ty<'tcx>, call_destructor_only: bool) -> bool {
673        if !self.elaborator.allow_async_drops()
674            || !self
675                .elaborator
676                .body()
677                .coroutine
678                .as_ref()
679                .is_some_and(|ck| ck.coroutine_kind.is_async_desugaring())
680        {
681            return false;
682        }
683
684        if drop_ty == self.place_ty(Local::arg(0).into()) {
685            return false;
686        }
687
688        let is_async_drop_feature_enabled = if self.tcx().features().async_drop() {
689            true
690        } else {
691            // Check if the type needing async drop comes from a dependency crate.
692            if let ty::Adt(adt_def, _) = drop_ty.kind() {
693                !adt_def.did().is_local() && adt_def.async_destructor(self.tcx()).is_some()
694            } else {
695                false
696            }
697        };
698
699        // Short-circuit before calling needs_async_drop/is_async_drop, as those
700        // require the `async_drop` lang item to exist (which may not be present
701        // in minimal/custom core environments like cranelift's mini_core).
702        if !is_async_drop_feature_enabled {
703            return false;
704        }
705
706        let needs_async_drop = if call_destructor_only {
707            drop_ty.is_async_drop(self.tcx(), self.elaborator.typing_env())
708        } else {
709            drop_ty.needs_async_drop(self.tcx(), self.elaborator.typing_env())
710        };
711
712        // Async drop in libstd/libcore would become insta-stable — catch that mistake.
713        if needs_async_drop && self.tcx().features().staged_api() {
714            ::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!(
715                self.source_info.span,
716                "don't use async drop in libstd, it becomes insta-stable"
717            );
718        }
719
720        needs_async_drop
721    }
722
723    /// This elaborates a single drop instruction, located at `bb`, and
724    /// patches over it.
725    ///
726    /// The elaborated drop checks the drop flags to only drop what
727    /// is initialized.
728    ///
729    /// In addition, the relevant drop flags also need to be cleared
730    /// to avoid double-drops. However, in the middle of a complex
731    /// drop, one must avoid clearing some of the flags before they
732    /// are read, as that would cause a memory leak.
733    ///
734    /// In particular, when dropping an ADT, multiple fields may be
735    /// joined together under the `rest` subpath. They are all controlled
736    /// by the primary drop flag, but only the last rest-field dropped
737    /// should clear it (and it must also not clear anything else).
738    //
739    // FIXME: I think we should just control the flags externally,
740    // and then we do not need this machinery.
741    #[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(741u32),
                                    ::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")]
742    fn elaborate_drop(&mut self, bb: BasicBlock) {
743        match self.elaborator.drop_style(self.path, DropFlagMode::Deep) {
744            DropStyle::Dead => {
745                self.elaborator
746                    .patch()
747                    .patch_terminator(bb, TerminatorKind::Goto { target: self.succ });
748            }
749            DropStyle::Static => {
750                self.build_drop(bb);
751            }
752            DropStyle::Conditional => {
753                let drop_bb = self.complete_drop(self.succ, self.unwind);
754                self.elaborator
755                    .patch()
756                    .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
757            }
758            DropStyle::Open => {
759                let drop_bb = self.open_drop();
760                self.elaborator
761                    .patch()
762                    .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
763            }
764        }
765    }
766
767    /// Returns the place and move path for each field of `variant`,
768    /// (the move path is `None` if the field is a rest field).
769    fn move_paths_for_fields(
770        &self,
771        base_place: Place<'tcx>,
772        variant_path: D::Path,
773        variant: &'tcx ty::VariantDef,
774        args: GenericArgsRef<'tcx>,
775    ) -> Vec<(Place<'tcx>, Option<D::Path>)> {
776        variant
777            .fields
778            .iter_enumerated()
779            .map(|(field_idx, field)| {
780                let subpath = self.elaborator.field_subpath(variant_path, field_idx);
781                let tcx = self.tcx();
782
783                match self.elaborator.typing_env().typing_mode().assert_not_erased() {
784                    ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen => {}
785                    ty::TypingMode::Coherence
786                    | ty::TypingMode::Typeck { .. }
787                    | ty::TypingMode::PostTypeckUntilBorrowck { .. }
788                    | ty::TypingMode::PostBorrowck { .. } => {
789                        ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
790                    }
791                }
792
793                let field_ty = field.ty(tcx, args);
794                // We silently leave an unnormalized type here to support polymorphic drop
795                // elaboration for users of rustc internal APIs
796                let field_ty = tcx
797                    .try_normalize_erasing_regions(self.elaborator.typing_env(), field_ty)
798                    .unwrap_or(field_ty.skip_norm_wip());
799
800                (tcx.mk_place_field(base_place, field_idx, field_ty), subpath)
801            })
802            .collect()
803    }
804
805    x;#[instrument(level = "debug", skip(self), ret)]
806    fn drop_subpath(
807        &mut self,
808        place: Place<'tcx>,
809        path: Option<D::Path>,
810        succ: BasicBlock,
811        unwind: Unwind,
812        dropline: Option<BasicBlock>,
813    ) -> BasicBlock {
814        if let Some(path) = path {
815            DropCtxt {
816                elaborator: self.elaborator,
817                source_info: self.source_info,
818                path,
819                place,
820                succ,
821                unwind,
822                dropline,
823            }
824            .elaborated_drop_block()
825        } else {
826            DropCtxt {
827                elaborator: self.elaborator,
828                source_info: self.source_info,
829                place,
830                succ,
831                unwind,
832                dropline,
833                // Using `self.path` here to condition the drop on our own drop flag.
834                path: self.path,
835            }
836            .complete_drop(succ, unwind)
837        }
838    }
839
840    /// Creates one-half of the drop ladder for a list of fields, and return
841    /// the list of steps in it in reverse order, with the first step
842    /// dropping 0 fields and so on.
843    ///
844    /// `unwind_ladder` is such a list of steps in reverse order,
845    /// which is called if the matching step of the drop glue panics.
846    ///
847    /// `dropline_ladder` is a similar list of steps in reverse order,
848    /// which is called if the matching step of the drop glue will contain async drop
849    /// (expanded later to Yield) and the containing coroutine will be dropped at this point.
850    x;#[instrument(level = "debug", skip(self), ret)]
851    fn drop_halfladder(
852        &mut self,
853        unwind_ladder: &[Unwind],
854        dropline_ladder: &[Option<BasicBlock>],
855        mut succ: BasicBlock,
856        fields: &[(Place<'tcx>, Option<D::Path>)],
857    ) -> Vec<BasicBlock> {
858        iter::once(succ)
859            .chain(itertools::izip!(fields.iter().rev(), unwind_ladder, dropline_ladder).map(
860                |(&(place, path), &unwind_succ, &dropline_to)| {
861                    succ = self.drop_subpath(place, path, succ, unwind_succ, dropline_to);
862                    succ
863                },
864            ))
865            .collect()
866    }
867
868    fn drop_ladder_bottom(&mut self) -> (BasicBlock, Unwind, Option<BasicBlock>) {
869        // Clear the "master" drop flag at the end. This is needed
870        // because the "master" drop protects the ADT's discriminant,
871        // which is invalidated after the ADT is dropped.
872        (
873            self.drop_flag_reset_block(DropFlagMode::Shallow, self.succ, self.unwind),
874            self.unwind,
875            self.dropline,
876        )
877    }
878
879    /// Creates a full drop ladder, consisting of 2 connected half-drop-ladders
880    ///
881    /// For example, with 3 fields, the drop ladder is
882    ///
883    /// ```text
884    /// .d0:
885    ///     ELAB(drop location.0 [target=.d1, unwind=.c1])
886    /// .d1:
887    ///     ELAB(drop location.1 [target=.d2, unwind=.c2])
888    /// .d2:
889    ///     ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`])
890    /// .c1:
891    ///     ELAB(drop location.1 [target=.c2])
892    /// .c2:
893    ///     ELAB(drop location.2 [target=`self.unwind`])
894    /// ```
895    ///
896    /// For possible-async drops in coroutines we also need dropline ladder
897    /// ```text
898    /// .d0 (mainline):
899    ///     ELAB(drop location.0 [target=.d1, unwind=.c1, drop=.e1])
900    /// .d1 (mainline):
901    ///     ELAB(drop location.1 [target=.d2, unwind=.c2, drop=.e2])
902    /// .d2 (mainline):
903    ///     ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`, drop=`self.drop`])
904    /// .c1 (unwind):
905    ///     ELAB(drop location.1 [target=.c2])
906    /// .c2 (unwind):
907    ///     ELAB(drop location.2 [target=`self.unwind`])
908    /// .e1 (dropline):
909    ///     ELAB(drop location.1 [target=.e2, unwind=.c2])
910    /// .e2 (dropline):
911    ///     ELAB(drop location.2 [target=`self.drop`, unwind=`self.unwind`])
912    /// ```
913    ///
914    /// NOTE: this does not clear the master drop flag, so you need
915    /// to point succ/unwind on a `drop_ladder_bottom`.
916    x;#[instrument(level = "debug", skip(self), ret)]
917    fn drop_ladder(
918        &mut self,
919        fields: Vec<(Place<'tcx>, Option<D::Path>)>,
920        succ: BasicBlock,
921        unwind: Unwind,
922        dropline: Option<BasicBlock>,
923    ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
924        assert!(
925            if unwind.is_cleanup() { dropline.is_none() } else { true },
926            "Dropline is set for cleanup drop ladder"
927        );
928
929        let mut fields = fields;
930        fields.retain(|&(place, _)| {
931            self.place_ty(place).needs_drop(self.tcx(), self.elaborator.typing_env())
932        });
933
934        debug!("drop_ladder - fields needing drop: {:?}", fields);
935
936        let dropline_ladder: Vec<Option<BasicBlock>> = vec![None; fields.len() + 1];
937        let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1];
938        let unwind_ladder: Vec<_> = if let Unwind::To(succ) = unwind {
939            let halfladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
940            halfladder.into_iter().map(Unwind::To).collect()
941        } else {
942            unwind_ladder
943        };
944        let dropline_ladder: Vec<_> = if let Some(succ) = dropline {
945            let halfladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
946            halfladder.into_iter().map(Some).collect()
947        } else {
948            dropline_ladder
949        };
950
951        let normal_ladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
952
953        (
954            *normal_ladder.last().unwrap(),
955            *unwind_ladder.last().unwrap(),
956            *dropline_ladder.last().unwrap(),
957        )
958    }
959
960    x;#[instrument(level = "debug", skip(self), ret)]
961    fn open_drop_for_tuple(&mut self, tys: &[Ty<'tcx>]) -> BasicBlock {
962        let fields = tys
963            .iter()
964            .enumerate()
965            .map(|(i, &ty)| {
966                (
967                    self.tcx().mk_place_field(self.place, FieldIdx::new(i), ty),
968                    self.elaborator.field_subpath(self.path, FieldIdx::new(i)),
969                )
970            })
971            .collect();
972
973        let (succ, unwind, dropline) = self.drop_ladder_bottom();
974        self.drop_ladder(fields, succ, unwind, dropline).0
975    }
976
977    /// Drops the T contained in a `Box<T>` if it has not been moved out of
978    x;#[instrument(level = "debug", ret)]
979    fn open_drop_for_box_contents(
980        &mut self,
981        adt: ty::AdtDef<'tcx>,
982        args: GenericArgsRef<'tcx>,
983        succ: BasicBlock,
984        unwind: Unwind,
985        dropline: Option<BasicBlock>,
986    ) -> BasicBlock {
987        // drop glue is sent straight to codegen
988        // box cannot be directly dereferenced
989        let unique_ty =
990            adt.non_enum_variant().fields[FieldIdx::ZERO].ty(self.tcx(), args).skip_norm_wip();
991        let unique_variant = unique_ty.ty_adt_def().unwrap().non_enum_variant();
992        let nonnull_ty = unique_variant.fields[FieldIdx::ZERO].ty(self.tcx(), args).skip_norm_wip();
993        let ptr_ty = Ty::new_imm_ptr(self.tcx(), args[0].expect_ty());
994
995        let unique_place = self.tcx().mk_place_field(self.place, FieldIdx::ZERO, unique_ty);
996        let nonnull_place = self.tcx().mk_place_field(unique_place, FieldIdx::ZERO, nonnull_ty);
997
998        let ptr_local = self.new_temp(ptr_ty);
999
1000        let interior = self.tcx().mk_place_deref(Place::from(ptr_local));
1001        let interior_path = self.elaborator.deref_subpath(self.path);
1002
1003        let do_drop_bb = self.drop_subpath(interior, interior_path, succ, unwind, dropline);
1004
1005        self.new_block_with_statements(
1006            unwind,
1007            vec![self.assign(
1008                Place::from(ptr_local),
1009                Rvalue::Cast(CastKind::Transmute, Operand::Copy(nonnull_place), ptr_ty),
1010            )],
1011            TerminatorKind::Goto { target: do_drop_bb },
1012        )
1013    }
1014
1015    x;#[instrument(level = "debug", ret)]
1016    fn open_drop_for_adt(
1017        &mut self,
1018        adt: ty::AdtDef<'tcx>,
1019        args: GenericArgsRef<'tcx>,
1020    ) -> BasicBlock {
1021        if adt.variants().is_empty() {
1022            return self.new_block(self.unwind, TerminatorKind::Unreachable);
1023        }
1024
1025        let skip_contents = adt.is_union() || adt.is_manually_drop();
1026        let (contents_succ, contents_unwind, contents_dropline) = if skip_contents {
1027            if adt.has_dtor(self.tcx()) && self.elaborator.get_drop_flag(self.path).is_some() {
1028                // the top-level drop flag is usually cleared by open_drop_for_adt_contents
1029                // types with destructors would still need an empty drop ladder to clear it
1030
1031                // however, these types are only open dropped in `DropShimElaborator`
1032                // which does not have drop flags
1033                // a future box-like "DerefMove" trait would allow for this case to happen
1034                span_bug!(self.source_info.span, "open dropping partially moved union");
1035            }
1036
1037            (self.succ, self.unwind, self.dropline)
1038        } else {
1039            self.open_drop_for_adt_contents(adt, args)
1040        };
1041
1042        if adt.has_dtor(self.tcx()) {
1043            let destructor_block = if adt.is_box() {
1044                // we need to drop the inside of the box before running the destructor
1045                let succ = self.destructor_call_block_sync(contents_succ, contents_unwind);
1046                let unwind = contents_unwind
1047                    .map(|unwind| self.destructor_call_block_sync(unwind, Unwind::InCleanup));
1048                let dropline = contents_dropline
1049                    .map(|dropline| self.destructor_call_block_sync(dropline, contents_unwind));
1050                self.open_drop_for_box_contents(adt, args, succ, unwind, dropline)
1051            } else {
1052                self.destructor_call_block(contents_succ, contents_unwind, contents_dropline)
1053            };
1054
1055            self.drop_flag_test_block(destructor_block, contents_succ, contents_unwind)
1056        } else {
1057            contents_succ
1058        }
1059    }
1060
1061    fn open_drop_for_adt_contents(
1062        &mut self,
1063        adt: ty::AdtDef<'tcx>,
1064        args: GenericArgsRef<'tcx>,
1065    ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
1066        let (succ, unwind, dropline) = self.drop_ladder_bottom();
1067        if !adt.is_enum() {
1068            let fields =
1069                self.move_paths_for_fields(self.place, self.path, adt.variant(FIRST_VARIANT), args);
1070            self.drop_ladder(fields, succ, unwind, dropline)
1071        } else {
1072            self.open_drop_for_multivariant(adt, args, succ, unwind, dropline)
1073        }
1074    }
1075
1076    fn open_drop_for_multivariant(
1077        &mut self,
1078        adt: ty::AdtDef<'tcx>,
1079        args: GenericArgsRef<'tcx>,
1080        succ: BasicBlock,
1081        unwind: Unwind,
1082        dropline: Option<BasicBlock>,
1083    ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
1084        let mut values = Vec::with_capacity(adt.variants().len());
1085        let mut normal_blocks = Vec::with_capacity(adt.variants().len());
1086        let mut unwind_blocks =
1087            Vec::with_capacity(if unwind.is_cleanup() { 0 } else { adt.variants().len() });
1088        let mut dropline_blocks =
1089            Vec::with_capacity(if dropline.is_none() { 0 } else { adt.variants().len() });
1090
1091        let mut have_otherwise_with_drop_glue = false;
1092        let mut have_otherwise = false;
1093        let tcx = self.tcx();
1094
1095        for (variant_index, discr) in adt.discriminants(tcx) {
1096            let variant = &adt.variant(variant_index);
1097            let subpath = self.elaborator.downcast_subpath(self.path, variant_index);
1098
1099            if let Some(variant_path) = subpath {
1100                let base_place = tcx.mk_place_elem(
1101                    self.place,
1102                    ProjectionElem::Downcast(Some(variant.name), variant_index),
1103                );
1104                let fields = self.move_paths_for_fields(base_place, variant_path, variant, args);
1105                values.push(discr.val);
1106                if let Unwind::To(unwind) = unwind {
1107                    // We can't use the half-ladder from the original
1108                    // drop ladder, because this breaks the
1109                    // "funclet can't have 2 successor funclets"
1110                    // requirement from MSVC:
1111                    //
1112                    //           switch       unwind-switch
1113                    //          /      \         /        \
1114                    //         v1.0    v2.0  v2.0-unwind  v1.0-unwind
1115                    //         |        |      /             |
1116                    //    v1.1-unwind  v2.1-unwind           |
1117                    //      ^                                |
1118                    //       \-------------------------------/
1119                    //
1120                    // Create a duplicate half-ladder to avoid that. We
1121                    // could technically only do this on MSVC, but I
1122                    // I want to minimize the divergence between MSVC
1123                    // and non-MSVC.
1124
1125                    let unwind_ladder = ::alloc::vec::from_elem(Unwind::InCleanup, fields.len() + 1)vec![Unwind::InCleanup; fields.len() + 1];
1126                    let dropline_ladder: Vec<Option<BasicBlock>> = ::alloc::vec::from_elem(None, fields.len() + 1)vec![None; fields.len() + 1];
1127                    let halfladder =
1128                        self.drop_halfladder(&unwind_ladder, &dropline_ladder, unwind, &fields);
1129                    unwind_blocks.push(halfladder.last().cloned().unwrap());
1130                }
1131                let (normal, _, drop_bb) = self.drop_ladder(fields, succ, unwind, dropline);
1132                normal_blocks.push(normal);
1133                if dropline.is_some() {
1134                    dropline_blocks.push(drop_bb.unwrap());
1135                }
1136            } else {
1137                have_otherwise = true;
1138
1139                let typing_env = self.elaborator.typing_env();
1140                let have_field_with_drop_glue = variant
1141                    .fields
1142                    .iter()
1143                    .any(|field| field.ty(tcx, args).skip_norm_wip().needs_drop(tcx, typing_env));
1144                if have_field_with_drop_glue {
1145                    have_otherwise_with_drop_glue = true;
1146                }
1147            }
1148        }
1149
1150        if !have_otherwise {
1151            values.pop();
1152        } else if !have_otherwise_with_drop_glue {
1153            normal_blocks.push(self.goto_block(succ, unwind));
1154            if let Unwind::To(unwind) = unwind {
1155                unwind_blocks.push(self.goto_block(unwind, Unwind::InCleanup));
1156            }
1157        } else {
1158            normal_blocks.push(self.drop_block(succ, unwind));
1159            if let Unwind::To(unwind) = unwind {
1160                unwind_blocks.push(self.drop_block(unwind, Unwind::InCleanup));
1161            }
1162        }
1163
1164        (
1165            self.adt_switch_block(adt, normal_blocks, &values, succ, unwind),
1166            unwind.map(|unwind| {
1167                self.adt_switch_block(adt, unwind_blocks, &values, unwind, Unwind::InCleanup)
1168            }),
1169            dropline.map(|dropline| {
1170                self.adt_switch_block(adt, dropline_blocks, &values, dropline, unwind)
1171            }),
1172        )
1173    }
1174
1175    fn adt_switch_block(
1176        &mut self,
1177        adt: ty::AdtDef<'tcx>,
1178        blocks: Vec<BasicBlock>,
1179        values: &[u128],
1180        succ: BasicBlock,
1181        unwind: Unwind,
1182    ) -> BasicBlock {
1183        // If there are multiple variants, then if something
1184        // is present within the enum the discriminant, tracked
1185        // by the rest path, must be initialized.
1186        //
1187        // Additionally, we do not want to switch on the
1188        // discriminant after it is free-ed, because that
1189        // way lies only trouble.
1190        let discr_ty = adt.repr().discr_type().to_ty(self.tcx());
1191        let discr = Place::from(self.new_temp(discr_ty));
1192        let discr_rv = Rvalue::Discriminant(self.place);
1193        let switch_block = self.new_block_with_statements(
1194            unwind,
1195            ::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)],
1196            TerminatorKind::SwitchInt {
1197                discr: Operand::Move(discr),
1198                targets: SwitchTargets::new(
1199                    values.iter().copied().zip(blocks.iter().copied()),
1200                    *blocks.last().unwrap(),
1201                ),
1202            },
1203        );
1204        self.drop_flag_test_block(switch_block, succ, unwind)
1205    }
1206
1207    x;#[instrument(level = "debug", skip(self), ret)]
1208    fn destructor_call_block_sync(&mut self, succ: BasicBlock, unwind: Unwind) -> BasicBlock {
1209        let tcx = self.tcx();
1210        let drop_trait = tcx.require_lang_item(LangItem::Drop, DUMMY_SP);
1211        let drop_fn = tcx.associated_item_def_ids(drop_trait)[0];
1212        let ty = self.place_ty(self.place);
1213
1214        let ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
1215        let ref_place = self.new_temp(ref_ty);
1216        let unit_temp = Place::from(self.new_temp(tcx.types.unit));
1217
1218        self.new_block_with_statements(
1219            unwind,
1220            vec![self.assign(
1221                Place::from(ref_place),
1222                Rvalue::Ref(
1223                    tcx.lifetimes.re_erased,
1224                    BorrowKind::Mut { kind: MutBorrowKind::Default },
1225                    self.place,
1226                ),
1227            )],
1228            TerminatorKind::Call {
1229                func: Operand::function_handle(tcx, drop_fn, [ty.into()], self.source_info.span),
1230                args: [dummy_spanned(Operand::Move(Place::from(ref_place)))].into(),
1231                destination: unit_temp,
1232                target: Some(succ),
1233                unwind: unwind.into_action(),
1234                call_source: CallSource::Misc,
1235                fn_span: self.source_info.span,
1236            },
1237        )
1238    }
1239
1240    x;#[instrument(level = "debug", skip(self), ret)]
1241    fn destructor_call_block(
1242        &mut self,
1243        succ: BasicBlock,
1244        unwind: Unwind,
1245        dropline: Option<BasicBlock>,
1246    ) -> BasicBlock {
1247        let ty = self.place_ty(self.place);
1248        if !unwind.is_cleanup() && self.check_if_can_async_drop(ty, true) {
1249            self.build_async_drop(self.place, ty, succ, unwind, dropline, true)
1250        } else {
1251            self.destructor_call_block_sync(succ, unwind)
1252        }
1253    }
1254
1255    /// Create a loop that drops an array:
1256    ///
1257    /// ```text
1258    /// loop-block:
1259    ///    can_go = cur == len
1260    ///    if can_go then succ else drop-block
1261    /// drop-block:
1262    ///    ptr = &raw mut P[cur]
1263    ///    cur = cur + 1
1264    ///    drop(ptr)
1265    /// ```
1266    fn drop_loop(
1267        &mut self,
1268        succ: BasicBlock,
1269        cur: Local,
1270        len: Local,
1271        ety: Ty<'tcx>,
1272        unwind: Unwind,
1273        dropline: Option<BasicBlock>,
1274    ) -> BasicBlock {
1275        let copy = |place: Place<'tcx>| Operand::Copy(place);
1276        let move_ = |place: Place<'tcx>| Operand::Move(place);
1277        let tcx = self.tcx();
1278
1279        let ptr_ty = Ty::new_mut_ptr(tcx, ety);
1280        let ptr = Place::from(self.new_temp(ptr_ty));
1281        let can_go = Place::from(self.new_temp(tcx.types.bool));
1282        let one = self.constant_usize(1);
1283
1284        let drop_block = self.new_block_with_statements(
1285            unwind,
1286            ::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![
1287                self.assign(
1288                    ptr,
1289                    Rvalue::RawPtr(RawPtrKind::Mut, tcx.mk_place_index(self.place, cur)),
1290                ),
1291                self.assign(
1292                    cur.into(),
1293                    Rvalue::BinaryOp(BinOp::Add, Box::new((move_(cur.into()), one))),
1294                ),
1295            ],
1296            // this gets overwritten by drop elaboration.
1297            TerminatorKind::Unreachable,
1298        );
1299
1300        let loop_block = self.new_block_with_statements(
1301            unwind,
1302            ::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(
1303                can_go,
1304                Rvalue::BinaryOp(BinOp::Eq, Box::new((copy(Place::from(cur)), copy(len.into())))),
1305            )],
1306            TerminatorKind::if_(move_(can_go), succ, drop_block),
1307        );
1308
1309        let place = tcx.mk_place_deref(ptr);
1310        if !unwind.is_cleanup() && self.check_if_can_async_drop(ety, false) {
1311            let async_drop_bb =
1312                self.build_async_drop(place, ety, loop_block, unwind, dropline, false);
1313            self.elaborator
1314                .patch()
1315                .patch_terminator(drop_block, TerminatorKind::Goto { target: async_drop_bb });
1316        } else {
1317            self.elaborator.patch().patch_terminator(
1318                drop_block,
1319                TerminatorKind::Drop {
1320                    place,
1321                    target: loop_block,
1322                    unwind: unwind.into_action(),
1323                    replace: false,
1324                    drop: None,
1325                },
1326            );
1327        }
1328        loop_block
1329    }
1330
1331    x;#[instrument(level = "debug", skip(self), ret)]
1332    fn open_drop_for_array(
1333        &mut self,
1334        array_ty: Ty<'tcx>,
1335        ety: Ty<'tcx>,
1336        opt_size: Option<u64>,
1337    ) -> BasicBlock {
1338        let tcx = self.tcx();
1339
1340        if let Some(size) = opt_size {
1341            enum ProjectionKind<Path> {
1342                Drop(std::ops::Range<u64>),
1343                Keep(u64, Path),
1344            }
1345            // Previously, we'd make a projection for every element in the array and create a drop
1346            // ladder if any `array_subpath` was `Some`, i.e. moving out with an array pattern.
1347            // This caused huge memory usage when generating the drops for large arrays, so we instead
1348            // record the *subslices* which are dropped and the *indexes* which are kept
1349            let mut drop_ranges = vec![];
1350            let mut dropping = true;
1351            let mut start = 0;
1352            for i in 0..size {
1353                let path = self.elaborator.array_subpath(self.path, i, size);
1354                if dropping && path.is_some() {
1355                    drop_ranges.push(ProjectionKind::Drop(start..i));
1356                    dropping = false;
1357                } else if !dropping && path.is_none() {
1358                    dropping = true;
1359                    start = i;
1360                }
1361                if let Some(path) = path {
1362                    drop_ranges.push(ProjectionKind::Keep(i, path));
1363                }
1364            }
1365            if !drop_ranges.is_empty() {
1366                if dropping {
1367                    drop_ranges.push(ProjectionKind::Drop(start..size));
1368                }
1369                let fields = drop_ranges
1370                    .iter()
1371                    .rev()
1372                    .map(|p| {
1373                        let (project, path) = match p {
1374                            ProjectionKind::Drop(r) => (
1375                                ProjectionElem::Subslice {
1376                                    from: r.start,
1377                                    to: r.end,
1378                                    from_end: false,
1379                                },
1380                                None,
1381                            ),
1382                            &ProjectionKind::Keep(offset, path) => (
1383                                ProjectionElem::ConstantIndex {
1384                                    offset,
1385                                    min_length: size,
1386                                    from_end: false,
1387                                },
1388                                Some(path),
1389                            ),
1390                        };
1391                        (tcx.mk_place_elem(self.place, project), path)
1392                    })
1393                    .collect::<Vec<_>>();
1394                let (succ, unwind, dropline) = self.drop_ladder_bottom();
1395                return self.drop_ladder(fields, succ, unwind, dropline).0;
1396            }
1397        }
1398
1399        let array_ptr_ty = Ty::new_mut_ptr(tcx, array_ty);
1400        let array_ptr = self.new_temp(array_ptr_ty);
1401
1402        let slice_ty = Ty::new_slice(tcx, ety);
1403        let slice_ptr_ty = Ty::new_mut_ptr(tcx, slice_ty);
1404        let slice_ptr = self.new_temp(slice_ptr_ty);
1405
1406        let array_place = mem::replace(
1407            &mut self.place,
1408            Place::from(slice_ptr).project_deeper(&[PlaceElem::Deref], tcx),
1409        );
1410        let slice_block = self.drop_loop_trio_for_slice(ety);
1411        self.place = array_place;
1412
1413        self.new_block_with_statements(
1414            self.unwind,
1415            vec![
1416                self.assign(Place::from(array_ptr), Rvalue::RawPtr(RawPtrKind::Mut, self.place)),
1417                self.assign(
1418                    Place::from(slice_ptr),
1419                    Rvalue::Cast(
1420                        CastKind::PointerCoercion(
1421                            PointerCoercion::Unsize,
1422                            CoercionSource::Implicit,
1423                        ),
1424                        Operand::Move(Place::from(array_ptr)),
1425                        slice_ptr_ty,
1426                    ),
1427                ),
1428            ],
1429            TerminatorKind::Goto { target: slice_block },
1430        )
1431    }
1432
1433    /// Creates a trio of drop-loops of `place`, which drops its contents, even
1434    /// in the case of 1 panic or in the case of coroutine drop
1435    x;#[instrument(level = "debug", skip(self), ret)]
1436    fn drop_loop_trio_for_slice(&mut self, ety: Ty<'tcx>) -> BasicBlock {
1437        let tcx = self.tcx();
1438        let len = self.new_temp(tcx.types.usize);
1439        let cur = self.new_temp(tcx.types.usize);
1440
1441        let unwind = self
1442            .unwind
1443            .map(|unwind| self.drop_loop(unwind, cur, len, ety, Unwind::InCleanup, None));
1444
1445        let dropline =
1446            self.dropline.map(|dropline| self.drop_loop(dropline, cur, len, ety, unwind, None));
1447
1448        let loop_block = self.drop_loop(self.succ, cur, len, ety, unwind, dropline);
1449
1450        let [PlaceElem::Deref] = self.place.projection.as_slice() else {
1451            span_bug!(
1452                self.source_info.span,
1453                "Expected place for slice drop shim to be *_n, but it's {:?}",
1454                self.place,
1455            );
1456        };
1457
1458        let zero = self.constant_usize(0);
1459        let drop_block = self.new_block_with_statements(
1460            unwind,
1461            vec![
1462                self.assign(
1463                    len.into(),
1464                    Rvalue::UnaryOp(
1465                        UnOp::PtrMetadata,
1466                        Operand::Copy(Place::from(self.place.local)),
1467                    ),
1468                ),
1469                self.assign(cur.into(), Rvalue::Use(zero, WithRetag::Yes)),
1470            ],
1471            TerminatorKind::Goto { target: loop_block },
1472        );
1473
1474        // FIXME(#34708): handle partially-dropped array/slice elements.
1475        let reset_block = self.drop_flag_reset_block(DropFlagMode::Deep, drop_block, unwind);
1476        self.drop_flag_test_block(reset_block, self.succ, unwind)
1477    }
1478
1479    /// The slow-path - create an "open", elaborated drop for a type
1480    /// which is moved-out-of only partially, and patch `bb` to a jump
1481    /// to it. This must not be called on ADTs with a destructor,
1482    /// as these can't be moved-out-of, except for `Box<T>`, which is
1483    /// special-cased.
1484    ///
1485    /// This creates a "drop ladder" that drops the needed fields of the
1486    /// ADT, both in the success case or if one of the destructors fail.
1487    fn open_drop(&mut self) -> BasicBlock {
1488        let ty = self.place_ty(self.place);
1489        match ty.kind() {
1490            ty::Closure(_, args) => self.open_drop_for_tuple(args.as_closure().upvar_tys()),
1491            ty::CoroutineClosure(_, args) => {
1492                self.open_drop_for_tuple(args.as_coroutine_closure().upvar_tys())
1493            }
1494            // Note that `elaborate_drops` only drops the upvars of a coroutine,
1495            // and this is ok because `open_drop` here can only be reached
1496            // within that own coroutine's resume function.
1497            // This should only happen for the self argument on the resume function.
1498            // It effectively only contains upvars until the coroutine transformation runs.
1499            // See librustc_body/transform/coroutine.rs for more details.
1500            ty::Coroutine(_, args) => self.open_drop_for_tuple(args.as_coroutine().upvar_tys()),
1501            ty::Tuple(fields) => self.open_drop_for_tuple(fields),
1502            ty::Adt(def, args) => self.open_drop_for_adt(*def, args),
1503            ty::Dynamic(..) => self.complete_drop(self.succ, self.unwind),
1504            ty::Array(ety, size) => {
1505                let size = size.try_to_target_usize(self.tcx());
1506                self.open_drop_for_array(ty, *ety, size)
1507            }
1508            ty::Slice(ety) => self.drop_loop_trio_for_slice(*ety),
1509
1510            ty::UnsafeBinder(_) => {
1511                // Unsafe binders may elaborate drops if their inner type isn't copy.
1512                // This is enforced in typeck, so this should never happen.
1513                self.tcx().dcx().span_delayed_bug(
1514                    self.source_info.span,
1515                    "open drop for unsafe binder shouldn't be encountered",
1516                );
1517                self.new_block(self.unwind, TerminatorKind::Unreachable)
1518            }
1519
1520            _ => ::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),
1521        }
1522    }
1523
1524    x;#[instrument(level = "debug", skip(self), ret)]
1525    fn complete_drop(&mut self, succ: BasicBlock, unwind: Unwind) -> BasicBlock {
1526        let drop_block = self.drop_block(succ, unwind);
1527        self.drop_flag_test_block(drop_block, succ, unwind)
1528    }
1529
1530    /// Creates a block that resets the drop flag. If `mode` is deep, all children drop flags will
1531    /// also be cleared.
1532    x;#[instrument(level = "debug", skip(self), ret)]
1533    fn drop_flag_reset_block(
1534        &mut self,
1535        mode: DropFlagMode,
1536        succ: BasicBlock,
1537        unwind: Unwind,
1538    ) -> BasicBlock {
1539        if unwind.is_cleanup() {
1540            // The drop flag isn't read again on the unwind path, so don't
1541            // bother setting it.
1542            return succ;
1543        }
1544        let block = self.new_block(unwind, TerminatorKind::Goto { target: succ });
1545        let block_start = Location { block, statement_index: 0 };
1546        self.elaborator.clear_drop_flag(block_start, self.path, mode);
1547        block
1548    }
1549
1550    x;#[instrument(level = "debug", skip(self), ret)]
1551    fn elaborated_drop_block(&mut self) -> BasicBlock {
1552        let blk = self.new_block(
1553            self.unwind,
1554            TerminatorKind::Drop {
1555                place: self.place,
1556                target: self.succ,
1557                unwind: self.unwind.into_action(),
1558                replace: false,
1559                drop: self.dropline,
1560            },
1561        );
1562        self.elaborate_drop(blk);
1563        blk
1564    }
1565
1566    fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
1567        let drop_ty = self.place_ty(self.place);
1568        if !unwind.is_cleanup() && self.check_if_can_async_drop(drop_ty, false) {
1569            self.build_async_drop(self.place, drop_ty, self.succ, unwind, self.dropline, false)
1570        } else {
1571            self.new_block(
1572                unwind,
1573                TerminatorKind::Drop {
1574                    place: self.place,
1575                    target,
1576                    unwind: unwind.into_action(),
1577                    replace: false,
1578                    drop: None,
1579                },
1580            )
1581        }
1582    }
1583
1584    fn goto_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
1585        let block = TerminatorKind::Goto { target };
1586        self.new_block(unwind, block)
1587    }
1588
1589    /// Returns the block to jump to in order to test the drop flag and execute the drop.
1590    ///
1591    /// Depending on the required `DropStyle`, this might be a generated block with an `if`
1592    /// terminator (for dynamic/open drops), or it might be `on_set` or `on_unset` itself, in case
1593    /// the drop can be statically determined.
1594    x;#[instrument(level = "debug", skip(self), ret)]
1595    fn drop_flag_test_block(
1596        &mut self,
1597        on_set: BasicBlock,
1598        on_unset: BasicBlock,
1599        unwind: Unwind,
1600    ) -> BasicBlock {
1601        let style = self.elaborator.drop_style(self.path, DropFlagMode::Shallow);
1602        match style {
1603            DropStyle::Dead => on_unset,
1604            DropStyle::Static => on_set,
1605            DropStyle::Conditional | DropStyle::Open => {
1606                let flag = self.elaborator.get_drop_flag(self.path).unwrap();
1607                let term = TerminatorKind::if_(flag, on_set, on_unset);
1608                self.new_block(unwind, term)
1609            }
1610        }
1611    }
1612
1613    x;#[instrument(level = "trace", skip(self), ret)]
1614    fn new_block(&mut self, unwind: Unwind, k: TerminatorKind<'tcx>) -> BasicBlock {
1615        self.elaborator.patch().new_block(BasicBlockData::new(
1616            Some(Terminator { source_info: self.source_info, kind: k, attributes: ThinVec::new() }),
1617            unwind.is_cleanup(),
1618        ))
1619    }
1620
1621    x;#[instrument(level = "trace", skip(self, statements), ret)]
1622    fn new_block_with_statements(
1623        &mut self,
1624        unwind: Unwind,
1625        statements: Vec<Statement<'tcx>>,
1626        k: TerminatorKind<'tcx>,
1627    ) -> BasicBlock {
1628        self.elaborator.patch().new_block(BasicBlockData::new_stmts(
1629            statements,
1630            Some(Terminator { source_info: self.source_info, kind: k, attributes: ThinVec::new() }),
1631            unwind.is_cleanup(),
1632        ))
1633    }
1634
1635    fn new_temp(&mut self, ty: Ty<'tcx>) -> Local {
1636        self.elaborator.patch().new_temp(ty, self.source_info.span)
1637    }
1638
1639    fn constant_usize(&self, val: u16) -> Operand<'tcx> {
1640        Operand::Constant(Box::new(ConstOperand {
1641            span: self.source_info.span,
1642            user_ty: None,
1643            const_: Const::from_usize(self.tcx(), val.into()),
1644        }))
1645    }
1646
1647    fn assign(&self, lhs: Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> {
1648        Statement::new(self.source_info, StatementKind::Assign(Box::new((lhs, rhs))))
1649    }
1650
1651    fn storage_live(&self, local: Local) -> Statement<'tcx> {
1652        Statement::new(self.source_info, StatementKind::StorageLive(local))
1653    }
1654
1655    fn storage_dead(&self, local: Local) -> Statement<'tcx> {
1656        Statement::new(self.source_info, StatementKind::StorageDead(local))
1657    }
1658}