Skip to main content

rustc_mir_transform/
elaborate_drop.rs

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