1use std::{fmt, iter, mem};
2
3use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
4use rustc_data_structures::thin_vec::ThinVec;
5use rustc_hir::lang_items::LangItem;
6use rustc_hir::{CoroutineDesugaring, CoroutineKind};
7use rustc_index::Idx;
8use rustc_middle::mir::*;
9use rustc_middle::ty::adjustment::PointerCoercion;
10use rustc_middle::ty::util::{Discr, IntTypeExt};
11use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt};
12use rustc_middle::{bug, span_bug};
13use rustc_span::{DUMMY_SP, dummy_spanned};
14use tracing::{debug, instrument};
15
16use crate::coroutine::CTX_ARG;
17use crate::patch::MirPatch;
18
19#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DropStyle {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
DropStyle::Dead => "Dead",
DropStyle::Static => "Static",
DropStyle::Conditional => "Conditional",
DropStyle::Open => "Open",
})
}
}Debug)]
21pub(crate) enum DropStyle {
22 Dead,
24
25 Static,
28
29 Conditional,
31
32 Open,
38}
39
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)]
42pub(crate) enum DropFlagMode {
43 Shallow,
45 Deep,
47}
48
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)]
51pub(crate) enum Unwind {
52 To(BasicBlock),
54 InCleanup,
56}
57
58impl Unwind {
59 fn is_cleanup(self) -> bool {
60 match self {
61 Unwind::To(..) => false,
62 Unwind::InCleanup => true,
63 }
64 }
65
66 fn into_action(self) -> UnwindAction {
67 match self {
68 Unwind::To(bb) => UnwindAction::Cleanup(bb),
69 Unwind::InCleanup => UnwindAction::Terminate(UnwindTerminateReason::InCleanup),
70 }
71 }
72
73 fn map<F>(self, f: F) -> Self
74 where
75 F: FnOnce(BasicBlock) -> BasicBlock,
76 {
77 match self {
78 Unwind::To(bb) => Unwind::To(f(bb)),
79 Unwind::InCleanup => Unwind::InCleanup,
80 }
81 }
82}
83
84pub(crate) trait DropElaborator<'a, 'tcx>: fmt::Debug {
85 type Path: Copy + fmt::Debug;
91
92 fn patch_ref(&self) -> &MirPatch<'tcx>;
95 fn patch(&mut self) -> &mut MirPatch<'tcx>;
96 fn body(&self) -> &'a Body<'tcx>;
97 fn tcx(&self) -> TyCtxt<'tcx>;
98 fn typing_env(&self) -> ty::TypingEnv<'tcx>;
99 fn allow_async_drops(&self) -> bool;
100
101 fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle;
105
106 fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>>;
108
109 fn clear_drop_flag(&mut self, location: Location, path: Self::Path, mode: DropFlagMode);
114
115 fn field_subpath(&self, path: Self::Path, field: FieldIdx) -> Option<Self::Path>;
121
122 fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path>;
128
129 fn downcast_subpath(&self, path: Self::Path, variant: VariantIdx) -> Option<Self::Path>;
133
134 fn array_subpath(&self, path: Self::Path, index: u64, size: u64) -> Option<Self::Path>;
140}
141
142#[derive(#[automatically_derived]
impl<'a, 'b, 'tcx, D: ::core::fmt::Debug> ::core::fmt::Debug for
DropCtxt<'a, 'b, 'tcx, D> where D: DropElaborator<'b, 'tcx>,
D::Path: ::core::fmt::Debug {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["elaborator", "source_info", "place", "path", "succ", "unwind",
"dropline"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.elaborator, &self.source_info, &self.place, &self.path,
&self.succ, &self.unwind, &&self.dropline];
::core::fmt::Formatter::debug_struct_fields_finish(f, "DropCtxt",
names, values)
}
}Debug)]
143struct DropCtxt<'a, 'b, 'tcx, D>
144where
145 D: DropElaborator<'b, 'tcx>,
146{
147 elaborator: &'a mut D,
148
149 source_info: SourceInfo,
150
151 place: Place<'tcx>,
152 path: D::Path,
153 succ: BasicBlock,
154 unwind: Unwind,
155 dropline: Option<BasicBlock>,
156}
157
158pub(crate) fn elaborate_drop<'b, 'tcx, D>(
167 elaborator: &mut D,
168 source_info: SourceInfo,
169 place: Place<'tcx>,
170 path: D::Path,
171 succ: BasicBlock,
172 unwind: Unwind,
173 bb: BasicBlock,
174 dropline: Option<BasicBlock>,
175) where
176 D: DropElaborator<'b, 'tcx>,
177 'tcx: 'b,
178{
179 DropCtxt { elaborator, source_info, place, path, succ, unwind, dropline }.elaborate_drop(bb)
180}
181
182impl<'a, 'b, 'tcx, D> DropCtxt<'a, 'b, 'tcx, D>
183where
184 D: DropElaborator<'b, 'tcx>,
185 'tcx: 'b,
186{
187 x;#[instrument(level = "trace", skip(self), ret)]
188 fn place_ty(&self, place: Place<'tcx>) -> Ty<'tcx> {
189 if place.local < self.elaborator.body().local_decls.next_index() {
190 place.ty(self.elaborator.body(), self.tcx()).ty
191 } else {
192 PlaceTy::from_ty(self.elaborator.patch_ref().local_ty(place.local))
194 .multi_projection_ty(self.elaborator.tcx(), place.projection)
195 .ty
196 }
197 }
198
199 fn tcx(&self) -> TyCtxt<'tcx> {
200 self.elaborator.tcx()
201 }
202
203 x;#[instrument(level = "debug", skip(self), ret)]
228 fn build_async_drop(
229 &mut self,
230 place: Place<'tcx>,
231 drop_ty: Ty<'tcx>,
232 succ: BasicBlock,
233 unwind: Unwind,
234 dropline: Option<BasicBlock>,
235 call_destructor_only: bool,
236 ) -> BasicBlock {
237 let tcx = self.tcx();
238 let span = self.source_info.span;
239 let obj_ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, drop_ty);
240
241 let async_drop_fn_def_id = if call_destructor_only {
242 let async_drop_trait = tcx.require_lang_item(LangItem::AsyncDrop, span);
244 tcx.associated_item_def_ids(async_drop_trait)[0]
245 } else {
246 tcx.require_lang_item(LangItem::AsyncDropInPlace, span)
248 };
249
250 let fut_ty = tcx
251 .instantiate_bound_regions_with_erased(
252 Ty::new_fn_def(tcx, async_drop_fn_def_id, [drop_ty]).fn_sig(tcx),
253 )
254 .output();
255 let fut = self.new_temp(fut_ty);
256
257 let succ_with_dead = self.new_block_with_statements(
261 unwind,
262 vec![self.storage_dead(fut)],
263 TerminatorKind::Goto { target: succ },
264 );
265 let dropline_with_dead = dropline.map(|target| {
266 self.new_block_with_statements(
267 unwind,
268 vec![self.storage_dead(fut)],
269 TerminatorKind::Goto { target },
270 )
271 });
272 let unwind_with_dead = unwind.map(|target| {
273 self.new_block_with_statements(
274 Unwind::InCleanup,
275 vec![self.storage_dead(fut)],
276 TerminatorKind::Goto { target },
277 )
278 });
279
280 let coroutine_kind = self.elaborator.body().coroutine_kind().unwrap();
282 let yield_value = match coroutine_kind {
283 CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _) => {
285 let full_yield_ty = self.elaborator.body().yield_ty().unwrap();
286 let ty::Adt(_poll_adt, args) = *full_yield_ty.kind() else { bug!() };
287 let ty::Adt(_option_adt, args) = *args.type_at(0).kind() else { bug!() };
288 let yield_ty = args.type_at(0);
289 Operand::unevaluated_constant(
290 tcx,
291 tcx.require_lang_item(LangItem::AsyncGenPending, span),
292 tcx.mk_args(&[yield_ty.into()]),
293 span,
294 )
295 }
296 CoroutineKind::Desugared(CoroutineDesugaring::Async, _) => {
298 Operand::zero_sized_constant(tcx.types.unit, span)
299 }
300 _ => panic!("unexpected coroutine for async drop {coroutine_kind:?}"),
302 };
303
304 let panic_bb = self.build_resumed_after_drop_abort_block(unwind_with_dead, coroutine_kind);
313 let (drop_pin_bb, drop_resume_bb, drop_drop_bb) = self.build_pin_poll_yield_loop(
314 CTX_ARG.into(),
315 fut.into(),
316 yield_value.clone(),
317 dropline_with_dead.unwrap_or(succ_with_dead),
320 unwind_with_dead,
321 );
322 self.elaborator
323 .patch()
324 .patch_terminator(drop_resume_bb, TerminatorKind::Goto { target: panic_bb });
325 self.elaborator
326 .patch()
327 .patch_terminator(drop_drop_bb, TerminatorKind::Goto { target: drop_pin_bb });
328
329 let succ_yield_loop = if dropline_with_dead.is_some() {
336 let (pin_bb, resume_bb, drop_bb) = self.build_pin_poll_yield_loop(
337 CTX_ARG.into(),
338 fut.into(),
339 yield_value,
340 succ_with_dead,
342 unwind_with_dead,
343 );
344 self.elaborator
345 .patch()
346 .patch_terminator(resume_bb, TerminatorKind::Goto { target: pin_bb });
347 self.elaborator
348 .patch()
349 .patch_terminator(drop_bb, TerminatorKind::Goto { target: drop_pin_bb });
350 pin_bb
351 } else {
352 drop_pin_bb
354 };
355
356 let pin_adt_def = tcx.adt_def(tcx.require_lang_item(LangItem::Pin, span));
360 let pin_obj_ty = Ty::new_adt(tcx, pin_adt_def, tcx.mk_args(&[obj_ref_ty.into()]));
361 let pin_obj_local = self.new_temp(pin_obj_ty);
363 let drop_arg = if call_destructor_only {
364 Operand::Move(pin_obj_local.into())
366 } else {
367 Operand::Copy(tcx.mk_place_field(pin_obj_local.into(), FieldIdx::ZERO, obj_ref_ty))
369 };
370 let call_drop_bb = self.new_block_with_statements(
371 unwind_with_dead,
372 vec![self.storage_live(fut)],
373 TerminatorKind::Call {
374 func: Operand::function_handle(tcx, async_drop_fn_def_id, [drop_ty.into()], span),
375 args: [dummy_spanned(drop_arg)].into(),
376 destination: fut.into(),
377 target: Some(succ_yield_loop),
378 unwind: unwind_with_dead.into_action(),
379 call_source: CallSource::Misc,
380 fn_span: self.source_info.span,
381 },
382 );
383
384 let obj_ref_place = Place::from(self.new_temp(obj_ref_ty));
386 let pin_obj_new_unchecked_fn = tcx.require_lang_item(LangItem::PinNewUnchecked, span);
387 let assign_obj_ref_place = self.assign(
388 obj_ref_place,
389 Rvalue::Ref(
390 tcx.lifetimes.re_erased,
391 BorrowKind::Mut { kind: MutBorrowKind::Default },
392 place,
393 ),
394 );
395 self.new_block_with_statements(
396 unwind,
397 vec![assign_obj_ref_place],
398 TerminatorKind::Call {
399 func: Operand::function_handle(
400 tcx,
401 pin_obj_new_unchecked_fn,
402 [obj_ref_ty.into()],
403 span,
404 ),
405 args: [dummy_spanned(Operand::Move(obj_ref_place))].into(),
406 destination: pin_obj_local.into(),
407 target: Some(call_drop_bb),
408 unwind: unwind.into_action(),
409 call_source: CallSource::Misc,
410 fn_span: span,
411 },
412 )
413 }
414
415 fn build_resumed_after_drop_abort_block(
416 &mut self,
417 unwind: Unwind,
418 coroutine_kind: CoroutineKind,
419 ) -> BasicBlock {
420 let tcx = self.tcx();
421 let panic_bb = self.new_block(unwind, TerminatorKind::Unreachable);
422 let msg = AssertMessage::ResumedAfterDrop(coroutine_kind);
423 let false_op = Operand::Constant(Box::new(ConstOperand {
424 span: self.source_info.span,
425 user_ty: None,
426 const_: Const::from_bool(tcx, false),
427 }));
428 self.elaborator.patch().patch_terminator(
429 panic_bb,
430 TerminatorKind::Assert {
431 cond: false_op,
432 expected: true,
433 msg: Box::new(msg),
434 target: panic_bb,
435 unwind: unwind.into_action(),
436 },
437 );
438 panic_bb
439 }
440
441 x;#[instrument(level = "trace", skip(self), ret)]
457 fn build_pin_poll_yield_loop(
458 &mut self,
459 resume_place: Place<'tcx>,
460 fut_place: Place<'tcx>,
461 yield_value: Operand<'tcx>,
462 succ: BasicBlock,
463 unwind: Unwind,
464 ) -> (BasicBlock, BasicBlock, BasicBlock) {
465 let tcx = self.tcx();
466 let source_info = self.source_info;
467
468 let resume_arg_ty = resume_place.ty(self.elaborator.body(), tcx).ty;
469 let context_ref_ty = Ty::new_task_context(tcx);
470
471 let poll_adt_def = tcx.adt_def(tcx.require_lang_item(LangItem::Poll, source_info.span));
472 let poll_enum = Ty::new_adt(tcx, poll_adt_def, tcx.mk_args(&[tcx.types.unit.into()]));
473
474 let fut_ty = self.elaborator.patch_ref().local_ty(fut_place.local);
475 let fut_ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, fut_ty);
476
477 let pin_adt_def = tcx.adt_def(tcx.require_lang_item(LangItem::Pin, source_info.span));
478 let fut_pin_ty = Ty::new_adt(tcx, pin_adt_def, tcx.mk_args(&[fut_ref_ty.into()]));
479
480 let yield_resume_local = self.new_temp(resume_arg_ty);
483 let resume_bb = self.new_block_with_statements(
484 unwind,
485 vec![
486 self.assign(
487 resume_place,
488 Rvalue::Use(Operand::Move(yield_resume_local.into()), WithRetag::Yes),
489 ),
490 self.storage_dead(yield_resume_local),
491 ],
492 TerminatorKind::Unreachable,
494 );
495 let dropline_bb = self.new_block_with_statements(
496 unwind,
497 vec![
498 self.assign(
499 resume_place,
500 Rvalue::Use(Operand::Move(yield_resume_local.into()), WithRetag::Yes),
501 ),
502 self.storage_dead(yield_resume_local),
503 ],
504 TerminatorKind::Unreachable,
506 );
507 let yield_bb = self.new_block_with_statements(
508 unwind,
509 vec![self.storage_live(yield_resume_local)],
510 TerminatorKind::Yield {
511 value: yield_value,
512 resume: resume_bb,
513 resume_arg: yield_resume_local.into(),
514 drop: Some(dropline_bb),
515 },
516 );
517
518 let poll_unit_local = self.new_temp(poll_enum);
519 let switch_bb = {
520 let poll_ready_variant =
521 tcx.require_lang_item(LangItem::PollReady, self.source_info.span);
522 let poll_ready_variant_idx = poll_adt_def.variant_index_with_id(poll_ready_variant);
523 let poll_pending_variant =
524 tcx.require_lang_item(LangItem::PollPending, self.source_info.span);
525 let poll_pending_variant_idx = poll_adt_def.variant_index_with_id(poll_pending_variant);
526
527 let Discr { val: poll_ready_discr, ty: poll_discr_ty } =
528 poll_enum.discriminant_for_variant(tcx, poll_ready_variant_idx).unwrap();
529 let Discr { val: poll_pending_discr, ty: _ } =
530 poll_enum.discriminant_for_variant(tcx, poll_pending_variant_idx).unwrap();
531
532 let poll_discr_local = self.new_temp(poll_discr_ty);
533 let otherwise_bb = self.elaborator.patch().unreachable_no_cleanup_block();
534 self.new_block_with_statements(
535 unwind,
536 vec![
537 self.assign(
538 poll_discr_local.into(),
539 Rvalue::Discriminant(poll_unit_local.into()),
540 ),
541 ],
542 TerminatorKind::SwitchInt {
543 discr: Operand::Move(poll_discr_local.into()),
544 targets: SwitchTargets::new(
545 [
546 (poll_ready_discr, succ),
548 (poll_pending_discr, yield_bb),
550 ]
551 .into_iter(),
552 otherwise_bb,
554 ),
555 },
556 )
557 };
558
559 let fut_pin_local = self.new_temp(fut_pin_ty);
560 let context_ref_local = self.new_temp(context_ref_ty);
561
562 let poll_fn = tcx.require_lang_item(LangItem::FuturePoll, source_info.span);
563 let poll_bb = self.new_block_with_statements(
564 unwind,
565 Vec::new(),
566 TerminatorKind::Call {
567 func: Operand::function_handle(tcx, poll_fn, [fut_ty.into()], source_info.span),
568 args: [
569 dummy_spanned(Operand::Move(fut_pin_local.into())),
570 dummy_spanned(Operand::Move(context_ref_local.into())),
571 ]
572 .into(),
573 destination: poll_unit_local.into(),
574 target: Some(switch_bb),
575 unwind: unwind.into_action(),
576 call_source: CallSource::Misc,
577 fn_span: source_info.span,
578 },
579 );
580
581 let get_context_fn = tcx.require_lang_item(LangItem::GetContext, source_info.span);
582 let get_context_bb = {
583 let entry_resume_local = self.new_temp(resume_arg_ty);
586 self.new_block_with_statements(
587 unwind,
588 vec![self.assign(
589 entry_resume_local.into(),
590 Rvalue::Use(Operand::Move(resume_place), WithRetag::Yes),
591 )],
592 TerminatorKind::Call {
593 func: Operand::function_handle(
594 tcx,
595 get_context_fn,
596 [tcx.lifetimes.re_erased.into(), tcx.lifetimes.re_erased.into()],
597 source_info.span,
598 ),
599 args: [dummy_spanned(Operand::Move(entry_resume_local.into()))].into(),
600 destination: context_ref_local.into(),
601 target: Some(poll_bb),
602 unwind: unwind.into_action(),
603 call_source: CallSource::Misc,
604 fn_span: source_info.span,
605 },
606 )
607 };
608
609 let fut_ref_local = self.new_temp(fut_ref_ty);
610 let fut_pin_new_unchecked_fn =
611 tcx.require_lang_item(LangItem::PinNewUnchecked, source_info.span);
612 let pin_bb = self.new_block_with_statements(
613 unwind,
614 vec![self.assign(
615 fut_ref_local.into(),
616 Rvalue::Ref(
617 tcx.lifetimes.re_erased,
618 BorrowKind::Mut { kind: MutBorrowKind::Default },
619 fut_place,
620 ),
621 )],
622 TerminatorKind::Call {
623 func: Operand::function_handle(
624 tcx,
625 fut_pin_new_unchecked_fn,
626 [fut_ref_ty.into()],
627 source_info.span,
628 ),
629 args: [dummy_spanned(Operand::Move(fut_ref_local.into()))].into(),
630 destination: fut_pin_local.into(),
631 target: Some(get_context_bb),
632 unwind: unwind.into_action(),
633 call_source: CallSource::Misc,
634 fn_span: source_info.span,
635 },
636 );
637
638 (pin_bb, resume_bb, dropline_bb)
639 }
640
641 fn build_drop(&mut self, bb: BasicBlock) {
642 let drop_ty = self.place_ty(self.place);
643 if !self.elaborator.patch_ref().block(self.elaborator.body(), bb).is_cleanup
644 && self.check_if_can_async_drop(drop_ty, false)
645 {
646 let async_drop_bb = self.build_async_drop(
647 self.place,
648 drop_ty,
649 self.succ,
650 self.unwind,
651 self.dropline,
652 false,
653 );
654 self.elaborator
655 .patch()
656 .patch_terminator(bb, TerminatorKind::Goto { target: async_drop_bb });
657 } else {
658 self.elaborator.patch().patch_terminator(
659 bb,
660 TerminatorKind::Drop {
661 place: self.place,
662 target: self.succ,
663 unwind: self.unwind.into_action(),
664 replace: false,
665 drop: None,
666 },
667 );
668 }
669 }
670
671 fn check_if_can_async_drop(&mut self, drop_ty: Ty<'tcx>, call_destructor_only: bool) -> bool {
673 if !self.elaborator.allow_async_drops()
674 || !self
675 .elaborator
676 .body()
677 .coroutine
678 .as_ref()
679 .is_some_and(|ck| ck.coroutine_kind.is_async_desugaring())
680 {
681 return false;
682 }
683
684 if drop_ty == self.place_ty(Local::arg(0).into()) {
685 return false;
686 }
687
688 let is_async_drop_feature_enabled = if self.tcx().features().async_drop() {
689 true
690 } else {
691 if let ty::Adt(adt_def, _) = drop_ty.kind() {
693 !adt_def.did().is_local() && adt_def.async_destructor(self.tcx()).is_some()
694 } else {
695 false
696 }
697 };
698
699 if !is_async_drop_feature_enabled {
703 return false;
704 }
705
706 let needs_async_drop = if call_destructor_only {
707 drop_ty.is_async_drop(self.tcx(), self.elaborator.typing_env())
708 } else {
709 drop_ty.needs_async_drop(self.tcx(), self.elaborator.typing_env())
710 };
711
712 if needs_async_drop && self.tcx().features().staged_api() {
714 ::rustc_middle::util::bug::span_bug_fmt(self.source_info.span,
format_args!("don\'t use async drop in libstd, it becomes insta-stable"));span_bug!(
715 self.source_info.span,
716 "don't use async drop in libstd, it becomes insta-stable"
717 );
718 }
719
720 needs_async_drop
721 }
722
723 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("elaborate_drop",
"rustc_mir_transform::elaborate_drop",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/elaborate_drop.rs"),
::tracing_core::__macro_support::Option::Some(741u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_transform::elaborate_drop"),
::tracing_core::field::FieldSet::new(&["self", "bb"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&bb)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
match self.elaborator.drop_style(self.path, DropFlagMode::Deep) {
DropStyle::Dead => {
self.elaborator.patch().patch_terminator(bb,
TerminatorKind::Goto { target: self.succ });
}
DropStyle::Static => { self.build_drop(bb); }
DropStyle::Conditional => {
let drop_bb = self.complete_drop(self.succ, self.unwind);
self.elaborator.patch().patch_terminator(bb,
TerminatorKind::Goto { target: drop_bb });
}
DropStyle::Open => {
let drop_bb = self.open_drop();
self.elaborator.patch().patch_terminator(bb,
TerminatorKind::Goto { target: drop_bb });
}
}
}
}
}#[instrument(level = "debug")]
742 fn elaborate_drop(&mut self, bb: BasicBlock) {
743 match self.elaborator.drop_style(self.path, DropFlagMode::Deep) {
744 DropStyle::Dead => {
745 self.elaborator
746 .patch()
747 .patch_terminator(bb, TerminatorKind::Goto { target: self.succ });
748 }
749 DropStyle::Static => {
750 self.build_drop(bb);
751 }
752 DropStyle::Conditional => {
753 let drop_bb = self.complete_drop(self.succ, self.unwind);
754 self.elaborator
755 .patch()
756 .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
757 }
758 DropStyle::Open => {
759 let drop_bb = self.open_drop();
760 self.elaborator
761 .patch()
762 .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
763 }
764 }
765 }
766
767 fn move_paths_for_fields(
770 &self,
771 base_place: Place<'tcx>,
772 variant_path: D::Path,
773 variant: &'tcx ty::VariantDef,
774 args: GenericArgsRef<'tcx>,
775 ) -> Vec<(Place<'tcx>, Option<D::Path>)> {
776 variant
777 .fields
778 .iter_enumerated()
779 .map(|(field_idx, field)| {
780 let subpath = self.elaborator.field_subpath(variant_path, field_idx);
781 let tcx = self.tcx();
782
783 match self.elaborator.typing_env().typing_mode().assert_not_erased() {
784 ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen => {}
785 ty::TypingMode::Coherence
786 | ty::TypingMode::Typeck { .. }
787 | ty::TypingMode::PostTypeckUntilBorrowck { .. }
788 | ty::TypingMode::PostBorrowck { .. } => {
789 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
790 }
791 }
792
793 let field_ty = field.ty(tcx, args);
794 let field_ty = tcx
797 .try_normalize_erasing_regions(self.elaborator.typing_env(), field_ty)
798 .unwrap_or(field_ty.skip_norm_wip());
799
800 (tcx.mk_place_field(base_place, field_idx, field_ty), subpath)
801 })
802 .collect()
803 }
804
805 x;#[instrument(level = "debug", skip(self), ret)]
806 fn drop_subpath(
807 &mut self,
808 place: Place<'tcx>,
809 path: Option<D::Path>,
810 succ: BasicBlock,
811 unwind: Unwind,
812 dropline: Option<BasicBlock>,
813 ) -> BasicBlock {
814 if let Some(path) = path {
815 DropCtxt {
816 elaborator: self.elaborator,
817 source_info: self.source_info,
818 path,
819 place,
820 succ,
821 unwind,
822 dropline,
823 }
824 .elaborated_drop_block()
825 } else {
826 DropCtxt {
827 elaborator: self.elaborator,
828 source_info: self.source_info,
829 place,
830 succ,
831 unwind,
832 dropline,
833 path: self.path,
835 }
836 .complete_drop(succ, unwind)
837 }
838 }
839
840 x;#[instrument(level = "debug", skip(self), ret)]
851 fn drop_halfladder(
852 &mut self,
853 unwind_ladder: &[Unwind],
854 dropline_ladder: &[Option<BasicBlock>],
855 mut succ: BasicBlock,
856 fields: &[(Place<'tcx>, Option<D::Path>)],
857 ) -> Vec<BasicBlock> {
858 iter::once(succ)
859 .chain(itertools::izip!(fields.iter().rev(), unwind_ladder, dropline_ladder).map(
860 |(&(place, path), &unwind_succ, &dropline_to)| {
861 succ = self.drop_subpath(place, path, succ, unwind_succ, dropline_to);
862 succ
863 },
864 ))
865 .collect()
866 }
867
868 fn drop_ladder_bottom(&mut self) -> (BasicBlock, Unwind, Option<BasicBlock>) {
869 (
873 self.drop_flag_reset_block(DropFlagMode::Shallow, self.succ, self.unwind),
874 self.unwind,
875 self.dropline,
876 )
877 }
878
879 x;#[instrument(level = "debug", skip(self), ret)]
917 fn drop_ladder(
918 &mut self,
919 fields: Vec<(Place<'tcx>, Option<D::Path>)>,
920 succ: BasicBlock,
921 unwind: Unwind,
922 dropline: Option<BasicBlock>,
923 ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
924 assert!(
925 if unwind.is_cleanup() { dropline.is_none() } else { true },
926 "Dropline is set for cleanup drop ladder"
927 );
928
929 let mut fields = fields;
930 fields.retain(|&(place, _)| {
931 self.place_ty(place).needs_drop(self.tcx(), self.elaborator.typing_env())
932 });
933
934 debug!("drop_ladder - fields needing drop: {:?}", fields);
935
936 let dropline_ladder: Vec<Option<BasicBlock>> = vec![None; fields.len() + 1];
937 let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1];
938 let unwind_ladder: Vec<_> = if let Unwind::To(succ) = unwind {
939 let halfladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
940 halfladder.into_iter().map(Unwind::To).collect()
941 } else {
942 unwind_ladder
943 };
944 let dropline_ladder: Vec<_> = if let Some(succ) = dropline {
945 let halfladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
946 halfladder.into_iter().map(Some).collect()
947 } else {
948 dropline_ladder
949 };
950
951 let normal_ladder = self.drop_halfladder(&unwind_ladder, &dropline_ladder, succ, &fields);
952
953 (
954 *normal_ladder.last().unwrap(),
955 *unwind_ladder.last().unwrap(),
956 *dropline_ladder.last().unwrap(),
957 )
958 }
959
960 x;#[instrument(level = "debug", skip(self), ret)]
961 fn open_drop_for_tuple(&mut self, tys: &[Ty<'tcx>]) -> BasicBlock {
962 let fields = tys
963 .iter()
964 .enumerate()
965 .map(|(i, &ty)| {
966 (
967 self.tcx().mk_place_field(self.place, FieldIdx::new(i), ty),
968 self.elaborator.field_subpath(self.path, FieldIdx::new(i)),
969 )
970 })
971 .collect();
972
973 let (succ, unwind, dropline) = self.drop_ladder_bottom();
974 self.drop_ladder(fields, succ, unwind, dropline).0
975 }
976
977 x;#[instrument(level = "debug", ret)]
979 fn open_drop_for_box_contents(
980 &mut self,
981 adt: ty::AdtDef<'tcx>,
982 args: GenericArgsRef<'tcx>,
983 succ: BasicBlock,
984 unwind: Unwind,
985 dropline: Option<BasicBlock>,
986 ) -> BasicBlock {
987 let unique_ty =
990 adt.non_enum_variant().fields[FieldIdx::ZERO].ty(self.tcx(), args).skip_norm_wip();
991 let unique_variant = unique_ty.ty_adt_def().unwrap().non_enum_variant();
992 let nonnull_ty = unique_variant.fields[FieldIdx::ZERO].ty(self.tcx(), args).skip_norm_wip();
993 let ptr_ty = Ty::new_imm_ptr(self.tcx(), args[0].expect_ty());
994
995 let unique_place = self.tcx().mk_place_field(self.place, FieldIdx::ZERO, unique_ty);
996 let nonnull_place = self.tcx().mk_place_field(unique_place, FieldIdx::ZERO, nonnull_ty);
997
998 let ptr_local = self.new_temp(ptr_ty);
999
1000 let interior = self.tcx().mk_place_deref(Place::from(ptr_local));
1001 let interior_path = self.elaborator.deref_subpath(self.path);
1002
1003 let do_drop_bb = self.drop_subpath(interior, interior_path, succ, unwind, dropline);
1004
1005 self.new_block_with_statements(
1006 unwind,
1007 vec![self.assign(
1008 Place::from(ptr_local),
1009 Rvalue::Cast(CastKind::Transmute, Operand::Copy(nonnull_place), ptr_ty),
1010 )],
1011 TerminatorKind::Goto { target: do_drop_bb },
1012 )
1013 }
1014
1015 x;#[instrument(level = "debug", ret)]
1016 fn open_drop_for_adt(
1017 &mut self,
1018 adt: ty::AdtDef<'tcx>,
1019 args: GenericArgsRef<'tcx>,
1020 ) -> BasicBlock {
1021 if adt.variants().is_empty() {
1022 return self.new_block(self.unwind, TerminatorKind::Unreachable);
1023 }
1024
1025 let skip_contents = adt.is_union() || adt.is_manually_drop();
1026 let (contents_succ, contents_unwind, contents_dropline) = if skip_contents {
1027 if adt.has_dtor(self.tcx()) && self.elaborator.get_drop_flag(self.path).is_some() {
1028 span_bug!(self.source_info.span, "open dropping partially moved union");
1035 }
1036
1037 (self.succ, self.unwind, self.dropline)
1038 } else {
1039 self.open_drop_for_adt_contents(adt, args)
1040 };
1041
1042 if adt.has_dtor(self.tcx()) {
1043 let destructor_block = if adt.is_box() {
1044 let succ = self.destructor_call_block_sync(contents_succ, contents_unwind);
1046 let unwind = contents_unwind
1047 .map(|unwind| self.destructor_call_block_sync(unwind, Unwind::InCleanup));
1048 let dropline = contents_dropline
1049 .map(|dropline| self.destructor_call_block_sync(dropline, contents_unwind));
1050 self.open_drop_for_box_contents(adt, args, succ, unwind, dropline)
1051 } else {
1052 self.destructor_call_block(contents_succ, contents_unwind, contents_dropline)
1053 };
1054
1055 self.drop_flag_test_block(destructor_block, contents_succ, contents_unwind)
1056 } else {
1057 contents_succ
1058 }
1059 }
1060
1061 fn open_drop_for_adt_contents(
1062 &mut self,
1063 adt: ty::AdtDef<'tcx>,
1064 args: GenericArgsRef<'tcx>,
1065 ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
1066 let (succ, unwind, dropline) = self.drop_ladder_bottom();
1067 if !adt.is_enum() {
1068 let fields =
1069 self.move_paths_for_fields(self.place, self.path, adt.variant(FIRST_VARIANT), args);
1070 self.drop_ladder(fields, succ, unwind, dropline)
1071 } else {
1072 self.open_drop_for_multivariant(adt, args, succ, unwind, dropline)
1073 }
1074 }
1075
1076 fn open_drop_for_multivariant(
1077 &mut self,
1078 adt: ty::AdtDef<'tcx>,
1079 args: GenericArgsRef<'tcx>,
1080 succ: BasicBlock,
1081 unwind: Unwind,
1082 dropline: Option<BasicBlock>,
1083 ) -> (BasicBlock, Unwind, Option<BasicBlock>) {
1084 let mut values = Vec::with_capacity(adt.variants().len());
1085 let mut normal_blocks = Vec::with_capacity(adt.variants().len());
1086 let mut unwind_blocks =
1087 Vec::with_capacity(if unwind.is_cleanup() { 0 } else { adt.variants().len() });
1088 let mut dropline_blocks =
1089 Vec::with_capacity(if dropline.is_none() { 0 } else { adt.variants().len() });
1090
1091 let mut have_otherwise_with_drop_glue = false;
1092 let mut have_otherwise = false;
1093 let tcx = self.tcx();
1094
1095 for (variant_index, discr) in adt.discriminants(tcx) {
1096 let variant = &adt.variant(variant_index);
1097 let subpath = self.elaborator.downcast_subpath(self.path, variant_index);
1098
1099 if let Some(variant_path) = subpath {
1100 let base_place = tcx.mk_place_elem(
1101 self.place,
1102 ProjectionElem::Downcast(Some(variant.name), variant_index),
1103 );
1104 let fields = self.move_paths_for_fields(base_place, variant_path, variant, args);
1105 values.push(discr.val);
1106 if let Unwind::To(unwind) = unwind {
1107 let unwind_ladder = ::alloc::vec::from_elem(Unwind::InCleanup, fields.len() + 1)vec![Unwind::InCleanup; fields.len() + 1];
1126 let dropline_ladder: Vec<Option<BasicBlock>> = ::alloc::vec::from_elem(None, fields.len() + 1)vec![None; fields.len() + 1];
1127 let halfladder =
1128 self.drop_halfladder(&unwind_ladder, &dropline_ladder, unwind, &fields);
1129 unwind_blocks.push(halfladder.last().cloned().unwrap());
1130 }
1131 let (normal, _, drop_bb) = self.drop_ladder(fields, succ, unwind, dropline);
1132 normal_blocks.push(normal);
1133 if dropline.is_some() {
1134 dropline_blocks.push(drop_bb.unwrap());
1135 }
1136 } else {
1137 have_otherwise = true;
1138
1139 let typing_env = self.elaborator.typing_env();
1140 let have_field_with_drop_glue = variant
1141 .fields
1142 .iter()
1143 .any(|field| field.ty(tcx, args).skip_norm_wip().needs_drop(tcx, typing_env));
1144 if have_field_with_drop_glue {
1145 have_otherwise_with_drop_glue = true;
1146 }
1147 }
1148 }
1149
1150 if !have_otherwise {
1151 values.pop();
1152 } else if !have_otherwise_with_drop_glue {
1153 normal_blocks.push(self.goto_block(succ, unwind));
1154 if let Unwind::To(unwind) = unwind {
1155 unwind_blocks.push(self.goto_block(unwind, Unwind::InCleanup));
1156 }
1157 } else {
1158 normal_blocks.push(self.drop_block(succ, unwind));
1159 if let Unwind::To(unwind) = unwind {
1160 unwind_blocks.push(self.drop_block(unwind, Unwind::InCleanup));
1161 }
1162 }
1163
1164 (
1165 self.adt_switch_block(adt, normal_blocks, &values, succ, unwind),
1166 unwind.map(|unwind| {
1167 self.adt_switch_block(adt, unwind_blocks, &values, unwind, Unwind::InCleanup)
1168 }),
1169 dropline.map(|dropline| {
1170 self.adt_switch_block(adt, dropline_blocks, &values, dropline, unwind)
1171 }),
1172 )
1173 }
1174
1175 fn adt_switch_block(
1176 &mut self,
1177 adt: ty::AdtDef<'tcx>,
1178 blocks: Vec<BasicBlock>,
1179 values: &[u128],
1180 succ: BasicBlock,
1181 unwind: Unwind,
1182 ) -> BasicBlock {
1183 let discr_ty = adt.repr().discr_type().to_ty(self.tcx());
1191 let discr = Place::from(self.new_temp(discr_ty));
1192 let discr_rv = Rvalue::Discriminant(self.place);
1193 let switch_block = self.new_block_with_statements(
1194 unwind,
1195 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[self.assign(discr, discr_rv)]))vec![self.assign(discr, discr_rv)],
1196 TerminatorKind::SwitchInt {
1197 discr: Operand::Move(discr),
1198 targets: SwitchTargets::new(
1199 values.iter().copied().zip(blocks.iter().copied()),
1200 *blocks.last().unwrap(),
1201 ),
1202 },
1203 );
1204 self.drop_flag_test_block(switch_block, succ, unwind)
1205 }
1206
1207 x;#[instrument(level = "debug", skip(self), ret)]
1208 fn destructor_call_block_sync(&mut self, succ: BasicBlock, unwind: Unwind) -> BasicBlock {
1209 let tcx = self.tcx();
1210 let drop_trait = tcx.require_lang_item(LangItem::Drop, DUMMY_SP);
1211 let drop_fn = tcx.associated_item_def_ids(drop_trait)[0];
1212 let ty = self.place_ty(self.place);
1213
1214 let ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
1215 let ref_place = self.new_temp(ref_ty);
1216 let unit_temp = Place::from(self.new_temp(tcx.types.unit));
1217
1218 self.new_block_with_statements(
1219 unwind,
1220 vec![self.assign(
1221 Place::from(ref_place),
1222 Rvalue::Ref(
1223 tcx.lifetimes.re_erased,
1224 BorrowKind::Mut { kind: MutBorrowKind::Default },
1225 self.place,
1226 ),
1227 )],
1228 TerminatorKind::Call {
1229 func: Operand::function_handle(tcx, drop_fn, [ty.into()], self.source_info.span),
1230 args: [dummy_spanned(Operand::Move(Place::from(ref_place)))].into(),
1231 destination: unit_temp,
1232 target: Some(succ),
1233 unwind: unwind.into_action(),
1234 call_source: CallSource::Misc,
1235 fn_span: self.source_info.span,
1236 },
1237 )
1238 }
1239
1240 x;#[instrument(level = "debug", skip(self), ret)]
1241 fn destructor_call_block(
1242 &mut self,
1243 succ: BasicBlock,
1244 unwind: Unwind,
1245 dropline: Option<BasicBlock>,
1246 ) -> BasicBlock {
1247 let ty = self.place_ty(self.place);
1248 if !unwind.is_cleanup() && self.check_if_can_async_drop(ty, true) {
1249 self.build_async_drop(self.place, ty, succ, unwind, dropline, true)
1250 } else {
1251 self.destructor_call_block_sync(succ, unwind)
1252 }
1253 }
1254
1255 fn drop_loop(
1267 &mut self,
1268 succ: BasicBlock,
1269 cur: Local,
1270 len: Local,
1271 ety: Ty<'tcx>,
1272 unwind: Unwind,
1273 dropline: Option<BasicBlock>,
1274 ) -> BasicBlock {
1275 let copy = |place: Place<'tcx>| Operand::Copy(place);
1276 let move_ = |place: Place<'tcx>| Operand::Move(place);
1277 let tcx = self.tcx();
1278
1279 let ptr_ty = Ty::new_mut_ptr(tcx, ety);
1280 let ptr = Place::from(self.new_temp(ptr_ty));
1281 let can_go = Place::from(self.new_temp(tcx.types.bool));
1282 let one = self.constant_usize(1);
1283
1284 let drop_block = self.new_block_with_statements(
1285 unwind,
1286 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[self.assign(ptr,
Rvalue::RawPtr(RawPtrKind::Mut,
tcx.mk_place_index(self.place, cur))),
self.assign(cur.into(),
Rvalue::BinaryOp(BinOp::Add,
Box::new((move_(cur.into()), one))))]))vec![
1287 self.assign(
1288 ptr,
1289 Rvalue::RawPtr(RawPtrKind::Mut, tcx.mk_place_index(self.place, cur)),
1290 ),
1291 self.assign(
1292 cur.into(),
1293 Rvalue::BinaryOp(BinOp::Add, Box::new((move_(cur.into()), one))),
1294 ),
1295 ],
1296 TerminatorKind::Unreachable,
1298 );
1299
1300 let loop_block = self.new_block_with_statements(
1301 unwind,
1302 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[self.assign(can_go,
Rvalue::BinaryOp(BinOp::Eq,
Box::new((copy(Place::from(cur)), copy(len.into())))))]))vec![self.assign(
1303 can_go,
1304 Rvalue::BinaryOp(BinOp::Eq, Box::new((copy(Place::from(cur)), copy(len.into())))),
1305 )],
1306 TerminatorKind::if_(move_(can_go), succ, drop_block),
1307 );
1308
1309 let place = tcx.mk_place_deref(ptr);
1310 if !unwind.is_cleanup() && self.check_if_can_async_drop(ety, false) {
1311 let async_drop_bb =
1312 self.build_async_drop(place, ety, loop_block, unwind, dropline, false);
1313 self.elaborator
1314 .patch()
1315 .patch_terminator(drop_block, TerminatorKind::Goto { target: async_drop_bb });
1316 } else {
1317 self.elaborator.patch().patch_terminator(
1318 drop_block,
1319 TerminatorKind::Drop {
1320 place,
1321 target: loop_block,
1322 unwind: unwind.into_action(),
1323 replace: false,
1324 drop: None,
1325 },
1326 );
1327 }
1328 loop_block
1329 }
1330
1331 x;#[instrument(level = "debug", skip(self), ret)]
1332 fn open_drop_for_array(
1333 &mut self,
1334 array_ty: Ty<'tcx>,
1335 ety: Ty<'tcx>,
1336 opt_size: Option<u64>,
1337 ) -> BasicBlock {
1338 let tcx = self.tcx();
1339
1340 if let Some(size) = opt_size {
1341 enum ProjectionKind<Path> {
1342 Drop(std::ops::Range<u64>),
1343 Keep(u64, Path),
1344 }
1345 let mut drop_ranges = vec![];
1350 let mut dropping = true;
1351 let mut start = 0;
1352 for i in 0..size {
1353 let path = self.elaborator.array_subpath(self.path, i, size);
1354 if dropping && path.is_some() {
1355 drop_ranges.push(ProjectionKind::Drop(start..i));
1356 dropping = false;
1357 } else if !dropping && path.is_none() {
1358 dropping = true;
1359 start = i;
1360 }
1361 if let Some(path) = path {
1362 drop_ranges.push(ProjectionKind::Keep(i, path));
1363 }
1364 }
1365 if !drop_ranges.is_empty() {
1366 if dropping {
1367 drop_ranges.push(ProjectionKind::Drop(start..size));
1368 }
1369 let fields = drop_ranges
1370 .iter()
1371 .rev()
1372 .map(|p| {
1373 let (project, path) = match p {
1374 ProjectionKind::Drop(r) => (
1375 ProjectionElem::Subslice {
1376 from: r.start,
1377 to: r.end,
1378 from_end: false,
1379 },
1380 None,
1381 ),
1382 &ProjectionKind::Keep(offset, path) => (
1383 ProjectionElem::ConstantIndex {
1384 offset,
1385 min_length: size,
1386 from_end: false,
1387 },
1388 Some(path),
1389 ),
1390 };
1391 (tcx.mk_place_elem(self.place, project), path)
1392 })
1393 .collect::<Vec<_>>();
1394 let (succ, unwind, dropline) = self.drop_ladder_bottom();
1395 return self.drop_ladder(fields, succ, unwind, dropline).0;
1396 }
1397 }
1398
1399 let array_ptr_ty = Ty::new_mut_ptr(tcx, array_ty);
1400 let array_ptr = self.new_temp(array_ptr_ty);
1401
1402 let slice_ty = Ty::new_slice(tcx, ety);
1403 let slice_ptr_ty = Ty::new_mut_ptr(tcx, slice_ty);
1404 let slice_ptr = self.new_temp(slice_ptr_ty);
1405
1406 let array_place = mem::replace(
1407 &mut self.place,
1408 Place::from(slice_ptr).project_deeper(&[PlaceElem::Deref], tcx),
1409 );
1410 let slice_block = self.drop_loop_trio_for_slice(ety);
1411 self.place = array_place;
1412
1413 self.new_block_with_statements(
1414 self.unwind,
1415 vec![
1416 self.assign(Place::from(array_ptr), Rvalue::RawPtr(RawPtrKind::Mut, self.place)),
1417 self.assign(
1418 Place::from(slice_ptr),
1419 Rvalue::Cast(
1420 CastKind::PointerCoercion(
1421 PointerCoercion::Unsize,
1422 CoercionSource::Implicit,
1423 ),
1424 Operand::Move(Place::from(array_ptr)),
1425 slice_ptr_ty,
1426 ),
1427 ),
1428 ],
1429 TerminatorKind::Goto { target: slice_block },
1430 )
1431 }
1432
1433 x;#[instrument(level = "debug", skip(self), ret)]
1436 fn drop_loop_trio_for_slice(&mut self, ety: Ty<'tcx>) -> BasicBlock {
1437 let tcx = self.tcx();
1438 let len = self.new_temp(tcx.types.usize);
1439 let cur = self.new_temp(tcx.types.usize);
1440
1441 let unwind = self
1442 .unwind
1443 .map(|unwind| self.drop_loop(unwind, cur, len, ety, Unwind::InCleanup, None));
1444
1445 let dropline =
1446 self.dropline.map(|dropline| self.drop_loop(dropline, cur, len, ety, unwind, None));
1447
1448 let loop_block = self.drop_loop(self.succ, cur, len, ety, unwind, dropline);
1449
1450 let [PlaceElem::Deref] = self.place.projection.as_slice() else {
1451 span_bug!(
1452 self.source_info.span,
1453 "Expected place for slice drop shim to be *_n, but it's {:?}",
1454 self.place,
1455 );
1456 };
1457
1458 let zero = self.constant_usize(0);
1459 let drop_block = self.new_block_with_statements(
1460 unwind,
1461 vec![
1462 self.assign(
1463 len.into(),
1464 Rvalue::UnaryOp(
1465 UnOp::PtrMetadata,
1466 Operand::Copy(Place::from(self.place.local)),
1467 ),
1468 ),
1469 self.assign(cur.into(), Rvalue::Use(zero, WithRetag::Yes)),
1470 ],
1471 TerminatorKind::Goto { target: loop_block },
1472 );
1473
1474 let reset_block = self.drop_flag_reset_block(DropFlagMode::Deep, drop_block, unwind);
1476 self.drop_flag_test_block(reset_block, self.succ, unwind)
1477 }
1478
1479 fn open_drop(&mut self) -> BasicBlock {
1488 let ty = self.place_ty(self.place);
1489 match ty.kind() {
1490 ty::Closure(_, args) => self.open_drop_for_tuple(args.as_closure().upvar_tys()),
1491 ty::CoroutineClosure(_, args) => {
1492 self.open_drop_for_tuple(args.as_coroutine_closure().upvar_tys())
1493 }
1494 ty::Coroutine(_, args) => self.open_drop_for_tuple(args.as_coroutine().upvar_tys()),
1501 ty::Tuple(fields) => self.open_drop_for_tuple(fields),
1502 ty::Adt(def, args) => self.open_drop_for_adt(*def, args),
1503 ty::Dynamic(..) => self.complete_drop(self.succ, self.unwind),
1504 ty::Array(ety, size) => {
1505 let size = size.try_to_target_usize(self.tcx());
1506 self.open_drop_for_array(ty, *ety, size)
1507 }
1508 ty::Slice(ety) => self.drop_loop_trio_for_slice(*ety),
1509
1510 ty::UnsafeBinder(_) => {
1511 self.tcx().dcx().span_delayed_bug(
1514 self.source_info.span,
1515 "open drop for unsafe binder shouldn't be encountered",
1516 );
1517 self.new_block(self.unwind, TerminatorKind::Unreachable)
1518 }
1519
1520 _ => ::rustc_middle::util::bug::span_bug_fmt(self.source_info.span,
format_args!("open drop from non-ADT `{0:?}`", ty))span_bug!(self.source_info.span, "open drop from non-ADT `{:?}`", ty),
1521 }
1522 }
1523
1524 x;#[instrument(level = "debug", skip(self), ret)]
1525 fn complete_drop(&mut self, succ: BasicBlock, unwind: Unwind) -> BasicBlock {
1526 let drop_block = self.drop_block(succ, unwind);
1527 self.drop_flag_test_block(drop_block, succ, unwind)
1528 }
1529
1530 x;#[instrument(level = "debug", skip(self), ret)]
1533 fn drop_flag_reset_block(
1534 &mut self,
1535 mode: DropFlagMode,
1536 succ: BasicBlock,
1537 unwind: Unwind,
1538 ) -> BasicBlock {
1539 if unwind.is_cleanup() {
1540 return succ;
1543 }
1544 let block = self.new_block(unwind, TerminatorKind::Goto { target: succ });
1545 let block_start = Location { block, statement_index: 0 };
1546 self.elaborator.clear_drop_flag(block_start, self.path, mode);
1547 block
1548 }
1549
1550 x;#[instrument(level = "debug", skip(self), ret)]
1551 fn elaborated_drop_block(&mut self) -> BasicBlock {
1552 let blk = self.new_block(
1553 self.unwind,
1554 TerminatorKind::Drop {
1555 place: self.place,
1556 target: self.succ,
1557 unwind: self.unwind.into_action(),
1558 replace: false,
1559 drop: self.dropline,
1560 },
1561 );
1562 self.elaborate_drop(blk);
1563 blk
1564 }
1565
1566 fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
1567 let drop_ty = self.place_ty(self.place);
1568 if !unwind.is_cleanup() && self.check_if_can_async_drop(drop_ty, false) {
1569 self.build_async_drop(self.place, drop_ty, self.succ, unwind, self.dropline, false)
1570 } else {
1571 self.new_block(
1572 unwind,
1573 TerminatorKind::Drop {
1574 place: self.place,
1575 target,
1576 unwind: unwind.into_action(),
1577 replace: false,
1578 drop: None,
1579 },
1580 )
1581 }
1582 }
1583
1584 fn goto_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
1585 let block = TerminatorKind::Goto { target };
1586 self.new_block(unwind, block)
1587 }
1588
1589 x;#[instrument(level = "debug", skip(self), ret)]
1595 fn drop_flag_test_block(
1596 &mut self,
1597 on_set: BasicBlock,
1598 on_unset: BasicBlock,
1599 unwind: Unwind,
1600 ) -> BasicBlock {
1601 let style = self.elaborator.drop_style(self.path, DropFlagMode::Shallow);
1602 match style {
1603 DropStyle::Dead => on_unset,
1604 DropStyle::Static => on_set,
1605 DropStyle::Conditional | DropStyle::Open => {
1606 let flag = self.elaborator.get_drop_flag(self.path).unwrap();
1607 let term = TerminatorKind::if_(flag, on_set, on_unset);
1608 self.new_block(unwind, term)
1609 }
1610 }
1611 }
1612
1613 x;#[instrument(level = "trace", skip(self), ret)]
1614 fn new_block(&mut self, unwind: Unwind, k: TerminatorKind<'tcx>) -> BasicBlock {
1615 self.elaborator.patch().new_block(BasicBlockData::new(
1616 Some(Terminator { source_info: self.source_info, kind: k, attributes: ThinVec::new() }),
1617 unwind.is_cleanup(),
1618 ))
1619 }
1620
1621 x;#[instrument(level = "trace", skip(self, statements), ret)]
1622 fn new_block_with_statements(
1623 &mut self,
1624 unwind: Unwind,
1625 statements: Vec<Statement<'tcx>>,
1626 k: TerminatorKind<'tcx>,
1627 ) -> BasicBlock {
1628 self.elaborator.patch().new_block(BasicBlockData::new_stmts(
1629 statements,
1630 Some(Terminator { source_info: self.source_info, kind: k, attributes: ThinVec::new() }),
1631 unwind.is_cleanup(),
1632 ))
1633 }
1634
1635 fn new_temp(&mut self, ty: Ty<'tcx>) -> Local {
1636 self.elaborator.patch().new_temp(ty, self.source_info.span)
1637 }
1638
1639 fn constant_usize(&self, val: u16) -> Operand<'tcx> {
1640 Operand::Constant(Box::new(ConstOperand {
1641 span: self.source_info.span,
1642 user_ty: None,
1643 const_: Const::from_usize(self.tcx(), val.into()),
1644 }))
1645 }
1646
1647 fn assign(&self, lhs: Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> {
1648 Statement::new(self.source_info, StatementKind::Assign(Box::new((lhs, rhs))))
1649 }
1650
1651 fn storage_live(&self, local: Local) -> Statement<'tcx> {
1652 Statement::new(self.source_info, StatementKind::StorageLive(local))
1653 }
1654
1655 fn storage_dead(&self, local: Local) -> Statement<'tcx> {
1656 Statement::new(self.source_info, StatementKind::StorageDead(local))
1657 }
1658}