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#[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 Dead,
23
24 Static,
27
28 Conditional,
30
31 Open,
37}
38
39#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DropFlagMode {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
DropFlagMode::Shallow => "Shallow",
DropFlagMode::Deep => "Deep",
})
}
}Debug)]
41pub(crate) enum DropFlagMode {
42 Shallow,
44 Deep,
46}
47
48#[derive(#[automatically_derived]
impl ::core::marker::Copy for Unwind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Unwind {
#[inline]
fn clone(&self) -> Unwind {
let _: ::core::clone::AssertParamIsClone<BasicBlock>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Unwind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Unwind::To(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "To",
&__self_0),
Unwind::InCleanup =>
::core::fmt::Formatter::write_str(f, "InCleanup"),
}
}
}Debug)]
50pub(crate) enum Unwind {
51 To(BasicBlock),
53 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 type Path: Copy + fmt::Debug;
90
91 fn patch_ref(&self) -> &MirPatch<'tcx>;
94 fn patch(&mut self) -> &mut MirPatch<'tcx>;
95 fn body(&self) -> &'a Body<'tcx>;
96 fn tcx(&self) -> TyCtxt<'tcx>;
97 fn typing_env(&self) -> ty::TypingEnv<'tcx>;
98 fn allow_async_drops(&self) -> bool;
99
100 fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle;
104
105 fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>>;
107
108 fn clear_drop_flag(&mut self, location: Location, path: Self::Path, mode: DropFlagMode);
113
114 fn field_subpath(&self, path: Self::Path, field: FieldIdx) -> Option<Self::Path>;
120
121 fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path>;
127
128 fn downcast_subpath(&self, path: Self::Path, variant: VariantIdx) -> Option<Self::Path>;
132
133 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
157pub(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 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 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 let async_drop_trait = tcx.require_lang_item(LangItem::AsyncDrop, span);
243 tcx.associated_item_def_ids(async_drop_trait)[0]
244 } else {
245 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 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 let coroutine_kind = self.elaborator.body().coroutine_kind().unwrap();
281 let yield_value = match coroutine_kind {
282 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 CoroutineKind::Desugared(CoroutineDesugaring::Async, _) => {
297 Operand::zero_sized_constant(tcx.types.unit, span)
298 }
299 _ => panic!("unexpected coroutine for async drop {coroutine_kind:?}"),
301 };
302
303 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 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 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 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 drop_pin_bb
353 };
354
355 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 let pin_obj_local = self.new_temp(pin_obj_ty);
362 let drop_arg = if call_destructor_only {
363 Operand::Move(pin_obj_local.into())
365 } else {
366 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 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 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 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 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 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 (poll_ready_discr, succ),
547 (poll_pending_discr, yield_bb),
549 ]
550 .into_iter(),
551 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 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 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 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 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 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 #[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 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 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 path: self.path,
834 }
835 .complete_drop(succ, unwind)
836 }
837 }
838
839 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 (
872 self.drop_flag_reset_block(DropFlagMode::Shallow, self.succ, self.unwind),
873 self.unwind,
874 self.dropline,
875 )
876 }
877
878 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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}