4 use rustc::middle::lang_items;
5 use rustc::traits::Reveal;
6 use rustc::ty::{self, Ty, TyCtxt};
7 use rustc::ty::layout::VariantIdx;
8 use rustc::ty::subst::SubstsRef;
9 use rustc::ty::util::IntTypeExt;
10 use rustc_data_structures::indexed_vec::Idx;
11 use crate::util::patch::MirPatch;
13 use std::convert::TryInto;
15 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
16 pub enum DropFlagState {
17 Present, // i.e., initialized
18 Absent, // i.e., deinitialized or "moved"
22 pub fn value(self) -> bool {
24 DropFlagState::Present => true,
25 DropFlagState::Absent => false
39 pub enum DropFlagMode {
44 #[derive(Copy, Clone, Debug)]
51 fn is_cleanup(self) -> bool {
53 Unwind::To(..) => false,
54 Unwind::InCleanup => true
58 fn into_option(self) -> Option<BasicBlock> {
60 Unwind::To(bb) => Some(bb),
61 Unwind::InCleanup => None,
65 fn map<F>(self, f: F) -> Self where F: FnOnce(BasicBlock) -> BasicBlock {
67 Unwind::To(bb) => Unwind::To(f(bb)),
68 Unwind::InCleanup => Unwind::InCleanup
73 pub trait DropElaborator<'a, 'tcx: 'a>: fmt::Debug {
74 type Path : Copy + fmt::Debug;
76 fn patch(&mut self) -> &mut MirPatch<'tcx>;
77 fn body(&self) -> &'a Body<'tcx>;
78 fn tcx(&self) -> TyCtxt<'tcx>;
79 fn param_env(&self) -> ty::ParamEnv<'tcx>;
81 fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle;
82 fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>>;
83 fn clear_drop_flag(&mut self, location: Location, path: Self::Path, mode: DropFlagMode);
86 fn field_subpath(&self, path: Self::Path, field: Field) -> Option<Self::Path>;
87 fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path>;
88 fn downcast_subpath(&self, path: Self::Path, variant: VariantIdx) -> Option<Self::Path>;
89 fn array_subpath(&self, path: Self::Path, index: u32, size: u32) -> Option<Self::Path>;
93 struct DropCtxt<'l, 'b: 'l, 'tcx: 'b, D>
94 where D : DropElaborator<'b, 'tcx> + 'l
96 elaborator: &'l mut D,
98 source_info: SourceInfo,
100 place: &'l Place<'tcx>,
106 pub fn elaborate_drop<'b, 'tcx, D>(
108 source_info: SourceInfo,
114 where D: DropElaborator<'b, 'tcx>
117 elaborator, source_info, place, path, succ, unwind
121 impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
123 D: DropElaborator<'b, 'tcx>,
125 fn place_ty(&self, place: &Place<'tcx>) -> Ty<'tcx> {
126 place.ty(self.elaborator.body(), self.tcx()).ty
129 fn tcx(&self) -> TyCtxt<'tcx> {
130 self.elaborator.tcx()
133 /// This elaborates a single drop instruction, located at `bb`, and
136 /// The elaborated drop checks the drop flags to only drop what
139 /// In addition, the relevant drop flags also need to be cleared
140 /// to avoid double-drops. However, in the middle of a complex
141 /// drop, one must avoid clearing some of the flags before they
142 /// are read, as that would cause a memory leak.
144 /// In particular, when dropping an ADT, multiple fields may be
145 /// joined together under the `rest` subpath. They are all controlled
146 /// by the primary drop flag, but only the last rest-field dropped
147 /// should clear it (and it must also not clear anything else).
149 // FIXME: I think we should just control the flags externally,
150 // and then we do not need this machinery.
151 pub fn elaborate_drop(&mut self, bb: BasicBlock) {
152 debug!("elaborate_drop({:?})", self);
153 let style = self.elaborator.drop_style(self.path, DropFlagMode::Deep);
154 debug!("elaborate_drop({:?}): live - {:?}", self, style);
157 self.elaborator.patch().patch_terminator(bb, TerminatorKind::Goto {
161 DropStyle::Static => {
162 let loc = self.terminator_loc(bb);
163 self.elaborator.clear_drop_flag(loc, self.path, DropFlagMode::Deep);
164 self.elaborator.patch().patch_terminator(bb, TerminatorKind::Drop {
165 location: self.place.clone(),
167 unwind: self.unwind.into_option(),
170 DropStyle::Conditional => {
171 let unwind = self.unwind; // FIXME(#43234)
172 let succ = self.succ;
173 let drop_bb = self.complete_drop(Some(DropFlagMode::Deep), succ, unwind);
174 self.elaborator.patch().patch_terminator(bb, TerminatorKind::Goto {
179 let drop_bb = self.open_drop();
180 self.elaborator.patch().patch_terminator(bb, TerminatorKind::Goto {
187 /// Returns the place and move path for each field of `variant`,
188 /// (the move path is `None` if the field is a rest field).
189 fn move_paths_for_fields(&self,
190 base_place: &Place<'tcx>,
191 variant_path: D::Path,
192 variant: &'tcx ty::VariantDef,
193 substs: SubstsRef<'tcx>)
194 -> Vec<(Place<'tcx>, Option<D::Path>)>
196 variant.fields.iter().enumerate().map(|(i, f)| {
197 let field = Field::new(i);
198 let subpath = self.elaborator.field_subpath(variant_path, field);
200 assert_eq!(self.elaborator.param_env().reveal, Reveal::All);
201 let field_ty = self.tcx().normalize_erasing_regions(
202 self.elaborator.param_env(),
203 f.ty(self.tcx(), substs),
205 (base_place.clone().field(field, field_ty), subpath)
209 fn drop_subpath(&mut self,
211 path: Option<D::Path>,
216 if let Some(path) = path {
217 debug!("drop_subpath: for std field {:?}", place);
220 elaborator: self.elaborator,
221 source_info: self.source_info,
222 path, place, succ, unwind,
223 }.elaborated_drop_block()
225 debug!("drop_subpath: for rest field {:?}", place);
228 elaborator: self.elaborator,
229 source_info: self.source_info,
231 // Using `self.path` here to condition the drop on
232 // our own drop flag.
234 }.complete_drop(None, succ, unwind)
238 /// Creates one-half of the drop ladder for a list of fields, and return
239 /// the list of steps in it in reverse order, with the first step
240 /// dropping 0 fields and so on.
242 /// `unwind_ladder` is such a list of steps in reverse order,
243 /// which is called if the matching step of the drop glue panics.
244 fn drop_halfladder(&mut self,
245 unwind_ladder: &[Unwind],
246 mut succ: BasicBlock,
247 fields: &[(Place<'tcx>, Option<D::Path>)])
250 Some(succ).into_iter().chain(
251 fields.iter().rev().zip(unwind_ladder)
252 .map(|(&(ref place, path), &unwind_succ)| {
253 succ = self.drop_subpath(place, path, succ, unwind_succ);
259 fn drop_ladder_bottom(&mut self) -> (BasicBlock, Unwind) {
260 // Clear the "master" drop flag at the end. This is needed
261 // because the "master" drop protects the ADT's discriminant,
262 // which is invalidated after the ADT is dropped.
263 let (succ, unwind) = (self.succ, self.unwind); // FIXME(#43234)
265 self.drop_flag_reset_block(DropFlagMode::Shallow, succ, unwind),
266 unwind.map(|unwind| {
267 self.drop_flag_reset_block(DropFlagMode::Shallow, unwind, Unwind::InCleanup)
272 /// Creates a full drop ladder, consisting of 2 connected half-drop-ladders
274 /// For example, with 3 fields, the drop ladder is
277 /// ELAB(drop location.0 [target=.d1, unwind=.c1])
279 /// ELAB(drop location.1 [target=.d2, unwind=.c2])
281 /// ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`])
283 /// ELAB(drop location.1 [target=.c2])
285 /// ELAB(drop location.2 [target=`self.unwind`])
287 /// NOTE: this does not clear the master drop flag, so you need
288 /// to point succ/unwind on a `drop_ladder_bottom`.
291 fields: Vec<(Place<'tcx>, Option<D::Path>)>,
294 ) -> (BasicBlock, Unwind) {
295 debug!("drop_ladder({:?}, {:?})", self, fields);
297 let mut fields = fields;
298 fields.retain(|&(ref place, _)| {
299 self.place_ty(place).needs_drop(self.tcx(), self.elaborator.param_env())
302 debug!("drop_ladder - fields needing drop: {:?}", fields);
304 let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1];
305 let unwind_ladder: Vec<_> = if let Unwind::To(target) = unwind {
306 let halfladder = self.drop_halfladder(&unwind_ladder, target, &fields);
307 halfladder.into_iter().map(Unwind::To).collect()
313 self.drop_halfladder(&unwind_ladder, succ, &fields);
315 (*normal_ladder.last().unwrap(), *unwind_ladder.last().unwrap())
318 fn open_drop_for_tuple(&mut self, tys: &[Ty<'tcx>]) -> BasicBlock {
319 debug!("open_drop_for_tuple({:?}, {:?})", self, tys);
321 let fields = tys.iter().enumerate().map(|(i, &ty)| {
322 (self.place.clone().field(Field::new(i), ty),
323 self.elaborator.field_subpath(self.path, Field::new(i)))
326 let (succ, unwind) = self.drop_ladder_bottom();
327 self.drop_ladder(fields, succ, unwind).0
330 fn open_drop_for_box(&mut self, adt: &'tcx ty::AdtDef, substs: SubstsRef<'tcx>) -> BasicBlock {
331 debug!("open_drop_for_box({:?}, {:?}, {:?})", self, adt, substs);
333 let interior = self.place.clone().deref();
334 let interior_path = self.elaborator.deref_subpath(self.path);
336 let succ = self.succ; // FIXME(#43234)
337 let unwind = self.unwind;
338 let succ = self.box_free_block(adt, substs, succ, unwind);
339 let unwind_succ = self.unwind.map(|unwind| {
340 self.box_free_block(adt, substs, unwind, Unwind::InCleanup)
343 self.drop_subpath(&interior, interior_path, succ, unwind_succ)
346 fn open_drop_for_adt(&mut self, adt: &'tcx ty::AdtDef, substs: SubstsRef<'tcx>) -> BasicBlock {
347 debug!("open_drop_for_adt({:?}, {:?}, {:?})", self, adt, substs);
348 if adt.variants.len() == 0 {
349 return self.elaborator.patch().new_block(BasicBlockData {
351 terminator: Some(Terminator {
352 source_info: self.source_info,
353 kind: TerminatorKind::Unreachable
355 is_cleanup: self.unwind.is_cleanup()
360 adt.is_union() || Some(adt.did) == self.tcx().lang_items().manually_drop();
361 let contents_drop = if skip_contents {
362 (self.succ, self.unwind)
364 self.open_drop_for_adt_contents(adt, substs)
367 if adt.has_dtor(self.tcx()) {
368 self.destructor_call_block(contents_drop)
374 fn open_drop_for_adt_contents(&mut self, adt: &'tcx ty::AdtDef,
375 substs: SubstsRef<'tcx>)
376 -> (BasicBlock, Unwind) {
377 let (succ, unwind) = self.drop_ladder_bottom();
379 let fields = self.move_paths_for_fields(
382 &adt.variants[VariantIdx::new(0)],
385 self.drop_ladder(fields, succ, unwind)
387 self.open_drop_for_multivariant(adt, substs, succ, unwind)
391 fn open_drop_for_multivariant(&mut self, adt: &'tcx ty::AdtDef,
392 substs: SubstsRef<'tcx>,
395 -> (BasicBlock, Unwind) {
396 let mut values = Vec::with_capacity(adt.variants.len());
397 let mut normal_blocks = Vec::with_capacity(adt.variants.len());
398 let mut unwind_blocks = if unwind.is_cleanup() {
401 Some(Vec::with_capacity(adt.variants.len()))
404 let mut have_otherwise = false;
406 for (variant_index, discr) in adt.discriminants(self.tcx()) {
407 let subpath = self.elaborator.downcast_subpath(
408 self.path, variant_index);
409 if let Some(variant_path) = subpath {
410 let base_place = self.place.clone().elem(
411 ProjectionElem::Downcast(Some(adt.variants[variant_index].ident.name),
413 let fields = self.move_paths_for_fields(
416 &adt.variants[variant_index],
418 values.push(discr.val);
419 if let Unwind::To(unwind) = unwind {
420 // We can't use the half-ladder from the original
421 // drop ladder, because this breaks the
422 // "funclet can't have 2 successor funclets"
423 // requirement from MSVC:
425 // switch unwind-switch
427 // v1.0 v2.0 v2.0-unwind v1.0-unwind
429 // v1.1-unwind v2.1-unwind |
431 // \-------------------------------/
433 // Create a duplicate half-ladder to avoid that. We
434 // could technically only do this on MSVC, but I
435 // I want to minimize the divergence between MSVC
438 let unwind_blocks = unwind_blocks.as_mut().unwrap();
439 let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1];
441 self.drop_halfladder(&unwind_ladder, unwind, &fields);
442 unwind_blocks.push(halfladder.last().cloned().unwrap());
444 let (normal, _) = self.drop_ladder(fields, succ, unwind);
445 normal_blocks.push(normal);
447 have_otherwise = true;
452 normal_blocks.push(self.drop_block(succ, unwind));
453 if let Unwind::To(unwind) = unwind {
454 unwind_blocks.as_mut().unwrap().push(
455 self.drop_block(unwind, Unwind::InCleanup)
462 (self.adt_switch_block(adt, normal_blocks, &values, succ, unwind),
463 unwind.map(|unwind| {
464 self.adt_switch_block(
465 adt, unwind_blocks.unwrap(), &values, unwind, Unwind::InCleanup
470 fn adt_switch_block(&mut self,
471 adt: &'tcx ty::AdtDef,
472 blocks: Vec<BasicBlock>,
477 // If there are multiple variants, then if something
478 // is present within the enum the discriminant, tracked
479 // by the rest path, must be initialized.
481 // Additionally, we do not want to switch on the
482 // discriminant after it is free-ed, because that
483 // way lies only trouble.
484 let discr_ty = adt.repr.discr_type().to_ty(self.tcx());
485 let discr = Place::Base(PlaceBase::Local(self.new_temp(discr_ty)));
486 let discr_rv = Rvalue::Discriminant(self.place.clone());
487 let switch_block = BasicBlockData {
488 statements: vec![self.assign(&discr, discr_rv)],
489 terminator: Some(Terminator {
490 source_info: self.source_info,
491 kind: TerminatorKind::SwitchInt {
492 discr: Operand::Move(discr),
494 values: From::from(values.to_owned()),
498 is_cleanup: unwind.is_cleanup(),
500 let switch_block = self.elaborator.patch().new_block(switch_block);
501 self.drop_flag_test_block(switch_block, succ, unwind)
504 fn destructor_call_block(&mut self, (succ, unwind): (BasicBlock, Unwind)) -> BasicBlock {
505 debug!("destructor_call_block({:?}, {:?})", self, succ);
506 let tcx = self.tcx();
507 let drop_trait = tcx.lang_items().drop_trait().unwrap();
508 let drop_fn = tcx.associated_items(drop_trait).next().unwrap();
509 let ty = self.place_ty(self.place);
510 let substs = tcx.mk_substs_trait(ty, &[]);
512 let ref_ty = tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut {
514 mutbl: hir::Mutability::MutMutable
516 let ref_place = self.new_temp(ref_ty);
517 let unit_temp = Place::Base(PlaceBase::Local(self.new_temp(tcx.mk_unit())));
519 let result = BasicBlockData {
520 statements: vec![self.assign(
521 &Place::Base(PlaceBase::Local(ref_place)),
522 Rvalue::Ref(tcx.lifetimes.re_erased,
523 BorrowKind::Mut { allow_two_phase_borrow: false },
526 terminator: Some(Terminator {
527 kind: TerminatorKind::Call {
528 func: Operand::function_handle(tcx, drop_fn.def_id, substs,
529 self.source_info.span),
530 args: vec![Operand::Move(Place::Base(PlaceBase::Local(ref_place)))],
531 destination: Some((unit_temp, succ)),
532 cleanup: unwind.into_option(),
535 source_info: self.source_info,
537 is_cleanup: unwind.is_cleanup(),
539 self.elaborator.patch().new_block(result)
542 /// Create a loop that drops an array:
546 /// can_go = cur == length_or_end
547 /// if can_go then succ else drop-block
551 /// cur = cur.offset(1)
553 /// ptr = &mut P[cur]
562 length_or_end: &Place<'tcx>,
567 let copy = |place: &Place<'tcx>| Operand::Copy(place.clone());
568 let move_ = |place: &Place<'tcx>| Operand::Move(place.clone());
569 let tcx = self.tcx();
571 let ref_ty = tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut {
573 mutbl: hir::Mutability::MutMutable
575 let ptr = &Place::Base(PlaceBase::Local(self.new_temp(ref_ty)));
576 let can_go = &Place::Base(PlaceBase::Local(self.new_temp(tcx.types.bool)));
578 let one = self.constant_usize(1);
579 let (ptr_next, cur_next) = if ptr_based {
581 tcx.lifetimes.re_erased,
582 BorrowKind::Mut { allow_two_phase_borrow: false },
583 Place::Projection(Box::new(Projection {
584 base: Place::Base(PlaceBase::Local(cur)),
585 elem: ProjectionElem::Deref,
588 Rvalue::BinaryOp(BinOp::Offset, move_(&Place::Base(PlaceBase::Local(cur))), one))
591 tcx.lifetimes.re_erased,
592 BorrowKind::Mut { allow_two_phase_borrow: false },
593 self.place.clone().index(cur)),
594 Rvalue::BinaryOp(BinOp::Add, move_(&Place::Base(PlaceBase::Local(cur))), one))
597 let drop_block = BasicBlockData {
599 self.assign(ptr, ptr_next),
600 self.assign(&Place::Base(PlaceBase::Local(cur)), cur_next)
602 is_cleanup: unwind.is_cleanup(),
603 terminator: Some(Terminator {
604 source_info: self.source_info,
605 // this gets overwritten by drop elaboration.
606 kind: TerminatorKind::Unreachable,
609 let drop_block = self.elaborator.patch().new_block(drop_block);
611 let loop_block = BasicBlockData {
613 self.assign(can_go, Rvalue::BinaryOp(BinOp::Eq,
614 copy(&Place::Base(PlaceBase::Local(cur))),
615 copy(length_or_end)))
617 is_cleanup: unwind.is_cleanup(),
618 terminator: Some(Terminator {
619 source_info: self.source_info,
620 kind: TerminatorKind::if_(tcx, move_(can_go), succ, drop_block)
623 let loop_block = self.elaborator.patch().new_block(loop_block);
625 self.elaborator.patch().patch_terminator(drop_block, TerminatorKind::Drop {
626 location: ptr.clone().deref(),
628 unwind: unwind.into_option()
634 fn open_drop_for_array(&mut self, ety: Ty<'tcx>, opt_size: Option<u64>) -> BasicBlock {
635 debug!("open_drop_for_array({:?}, {:?})", ety, opt_size);
637 // if size_of::<ety>() == 0 {
643 if let Some(size) = opt_size {
644 let size: u32 = size.try_into().unwrap_or_else(|_| {
645 bug!("move out check isn't implemented for array sizes bigger than u32::MAX");
647 let fields: Vec<(Place<'tcx>, Option<D::Path>)> = (0..size).map(|i| {
648 (self.place.clone().elem(ProjectionElem::ConstantIndex{
653 self.elaborator.array_subpath(self.path, i, size))
656 if fields.iter().any(|(_,path)| path.is_some()) {
657 let (succ, unwind) = self.drop_ladder_bottom();
658 return self.drop_ladder(fields, succ, unwind).0
662 let move_ = |place: &Place<'tcx>| Operand::Move(place.clone());
663 let tcx = self.tcx();
664 let elem_size = &Place::Base(PlaceBase::Local(self.new_temp(tcx.types.usize)));
665 let len = &Place::Base(PlaceBase::Local(self.new_temp(tcx.types.usize)));
667 static USIZE_SWITCH_ZERO: &[u128] = &[0];
669 let base_block = BasicBlockData {
671 self.assign(elem_size, Rvalue::NullaryOp(NullOp::SizeOf, ety)),
672 self.assign(len, Rvalue::Len(self.place.clone())),
674 is_cleanup: self.unwind.is_cleanup(),
675 terminator: Some(Terminator {
676 source_info: self.source_info,
677 kind: TerminatorKind::SwitchInt {
678 discr: move_(elem_size),
679 switch_ty: tcx.types.usize,
680 values: From::from(USIZE_SWITCH_ZERO),
682 self.drop_loop_pair(ety, false, len.clone()),
683 self.drop_loop_pair(ety, true, len.clone()),
688 self.elaborator.patch().new_block(base_block)
691 /// Ceates a pair of drop-loops of `place`, which drops its contents, even
692 /// in the case of 1 panic. If `ptr_based`, creates a pointer loop,
693 /// otherwise create an index loop.
700 debug!("drop_loop_pair({:?}, {:?})", ety, ptr_based);
701 let tcx = self.tcx();
702 let iter_ty = if ptr_based {
708 let cur = self.new_temp(iter_ty);
709 let length_or_end = if ptr_based {
710 // FIXME check if we want to make it return a `Place` directly
711 // if all use sites want a `Place::Base` anyway.
712 let temp = self.new_temp(iter_ty);
713 Place::Base(PlaceBase::Local(temp))
718 let unwind = self.unwind.map(|unwind| {
719 self.drop_loop(unwind,
727 let loop_block = self.drop_loop(
735 let cur = Place::Base(PlaceBase::Local(cur));
736 let drop_block_stmts = if ptr_based {
737 let tmp_ty = tcx.mk_mut_ptr(self.place_ty(self.place));
738 let tmp = Place::Base(PlaceBase::Local(self.new_temp(tmp_ty)));
740 // cur = tmp as *mut T;
741 // end = Offset(cur, len);
743 self.assign(&tmp, Rvalue::Ref(
744 tcx.lifetimes.re_erased,
745 BorrowKind::Mut { allow_two_phase_borrow: false },
750 Rvalue::Cast(CastKind::Misc, Operand::Move(tmp), iter_ty),
754 Rvalue::BinaryOp(BinOp::Offset, Operand::Copy(cur), Operand::Move(length)
758 // cur = 0 (length already pushed)
759 let zero = self.constant_usize(0);
760 vec![self.assign(&cur, Rvalue::Use(zero))]
762 let drop_block = self.elaborator.patch().new_block(BasicBlockData {
763 statements: drop_block_stmts,
764 is_cleanup: unwind.is_cleanup(),
765 terminator: Some(Terminator {
766 source_info: self.source_info,
767 kind: TerminatorKind::Goto { target: loop_block }
771 // FIXME(#34708): handle partially-dropped array/slice elements.
772 let reset_block = self.drop_flag_reset_block(DropFlagMode::Deep, drop_block, unwind);
773 self.drop_flag_test_block(reset_block, self.succ, unwind)
776 /// The slow-path - create an "open", elaborated drop for a type
777 /// which is moved-out-of only partially, and patch `bb` to a jump
778 /// to it. This must not be called on ADTs with a destructor,
779 /// as these can't be moved-out-of, except for `Box<T>`, which is
782 /// This creates a "drop ladder" that drops the needed fields of the
783 /// ADT, both in the success case or if one of the destructors fail.
784 fn open_drop(&mut self) -> BasicBlock {
785 let ty = self.place_ty(self.place);
787 ty::Closure(def_id, substs) => {
788 let tys : Vec<_> = substs.upvar_tys(def_id, self.tcx()).collect();
789 self.open_drop_for_tuple(&tys)
791 // Note that `elaborate_drops` only drops the upvars of a generator,
792 // and this is ok because `open_drop` here can only be reached
793 // within that own generator's resume function.
794 // This should only happen for the self argument on the resume function.
795 // It effetively only contains upvars until the generator transformation runs.
796 // See librustc_body/transform/generator.rs for more details.
797 ty::Generator(def_id, substs, _) => {
798 let tys : Vec<_> = substs.upvar_tys(def_id, self.tcx()).collect();
799 self.open_drop_for_tuple(&tys)
802 let tys: Vec<_> = tys.iter().map(|k| k.expect_ty()).collect();
803 self.open_drop_for_tuple(&tys)
805 ty::Adt(def, substs) => {
807 self.open_drop_for_box(def, substs)
809 self.open_drop_for_adt(def, substs)
813 let unwind = self.unwind; // FIXME(#43234)
814 let succ = self.succ;
815 self.complete_drop(Some(DropFlagMode::Deep), succ, unwind)
817 ty::Array(ety, size) => {
818 let size = size.assert_usize(self.tcx());
819 self.open_drop_for_array(ety, size)
821 ty::Slice(ety) => self.open_drop_for_array(ety, None),
823 _ => bug!("open drop from non-ADT `{:?}`", ty)
827 /// Returns a basic block that drop a place using the context
828 /// and path in `c`. If `mode` is something, also clear `c`
831 /// if FLAG(self.path)
832 /// if let Some(mode) = mode: FLAG(self.path)[mode] = false
836 drop_mode: Option<DropFlagMode>,
840 debug!("complete_drop({:?},{:?})", self, drop_mode);
842 let drop_block = self.drop_block(succ, unwind);
843 let drop_block = if let Some(mode) = drop_mode {
844 self.drop_flag_reset_block(mode, drop_block, unwind)
849 self.drop_flag_test_block(drop_block, succ, unwind)
852 fn drop_flag_reset_block(&mut self,
855 unwind: Unwind) -> BasicBlock
857 debug!("drop_flag_reset_block({:?},{:?})", self, mode);
859 let block = self.new_block(unwind, TerminatorKind::Goto { target: succ });
860 let block_start = Location { block: block, statement_index: 0 };
861 self.elaborator.clear_drop_flag(block_start, self.path, mode);
865 fn elaborated_drop_block(&mut self) -> BasicBlock {
866 debug!("elaborated_drop_block({:?})", self);
867 let unwind = self.unwind; // FIXME(#43234)
868 let succ = self.succ;
869 let blk = self.drop_block(succ, unwind);
870 self.elaborate_drop(blk);
876 adt: &'tcx ty::AdtDef,
877 substs: SubstsRef<'tcx>,
881 let block = self.unelaborated_free_block(adt, substs, target, unwind);
882 self.drop_flag_test_block(block, target, unwind)
885 fn unelaborated_free_block(
887 adt: &'tcx ty::AdtDef,
888 substs: SubstsRef<'tcx>,
892 let tcx = self.tcx();
893 let unit_temp = Place::Base(PlaceBase::Local(self.new_temp(tcx.mk_unit())));
894 let free_func = tcx.require_lang_item(lang_items::BoxFreeFnLangItem);
895 let args = adt.variants[VariantIdx::new(0)].fields.iter().enumerate().map(|(i, f)| {
896 let field = Field::new(i);
897 let field_ty = f.ty(self.tcx(), substs);
898 Operand::Move(self.place.clone().field(field, field_ty))
901 let call = TerminatorKind::Call {
902 func: Operand::function_handle(tcx, free_func, substs, self.source_info.span),
904 destination: Some((unit_temp, target)),
906 from_hir_call: false,
908 let free_block = self.new_block(unwind, call);
910 let block_start = Location { block: free_block, statement_index: 0 };
911 self.elaborator.clear_drop_flag(block_start, self.path, DropFlagMode::Shallow);
915 fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
916 let block = TerminatorKind::Drop {
917 location: self.place.clone(),
919 unwind: unwind.into_option()
921 self.new_block(unwind, block)
924 fn drop_flag_test_block(&mut self,
926 on_unset: BasicBlock,
930 let style = self.elaborator.drop_style(self.path, DropFlagMode::Shallow);
931 debug!("drop_flag_test_block({:?},{:?},{:?},{:?}) - {:?}",
932 self, on_set, on_unset, unwind, style);
935 DropStyle::Dead => on_unset,
936 DropStyle::Static => on_set,
937 DropStyle::Conditional | DropStyle::Open => {
938 let flag = self.elaborator.get_drop_flag(self.path).unwrap();
939 let term = TerminatorKind::if_(self.tcx(), flag, on_set, on_unset);
940 self.new_block(unwind, term)
945 fn new_block(&mut self, unwind: Unwind, k: TerminatorKind<'tcx>) -> BasicBlock {
946 self.elaborator.patch().new_block(BasicBlockData {
948 terminator: Some(Terminator {
949 source_info: self.source_info, kind: k
951 is_cleanup: unwind.is_cleanup()
955 fn new_temp(&mut self, ty: Ty<'tcx>) -> Local {
956 self.elaborator.patch().new_temp(ty, self.source_info.span)
959 fn terminator_loc(&mut self, bb: BasicBlock) -> Location {
960 let body = self.elaborator.body();
961 self.elaborator.patch().terminator_loc(body, bb)
964 fn constant_usize(&self, val: u16) -> Operand<'tcx> {
965 Operand::Constant(box Constant {
966 span: self.source_info.span,
967 ty: self.tcx().types.usize,
969 literal: ty::Const::from_usize(self.tcx(), val.into()),
973 fn assign(&self, lhs: &Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> {
975 source_info: self.source_info,
976 kind: StatementKind::Assign(lhs.clone(), box rhs)