]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/util/elaborate_drops.rs
Rollup merge of #69792 - LenaWil:try_reserve_error/impl-error, r=sfackler
[rust.git] / src / librustc_mir / util / elaborate_drops.rs
1 use crate::util::patch::MirPatch;
2 use rustc::middle::lang_items;
3 use rustc::mir::*;
4 use rustc::traits::Reveal;
5 use rustc::ty::layout::VariantIdx;
6 use rustc::ty::subst::SubstsRef;
7 use rustc::ty::util::IntTypeExt;
8 use rustc::ty::{self, Ty, TyCtxt};
9 use rustc_hir as hir;
10 use rustc_index::vec::Idx;
11 use std::fmt;
12
13 use std::convert::TryInto;
14
15 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
16 pub enum DropFlagState {
17     Present, // i.e., initialized
18     Absent,  // i.e., deinitialized or "moved"
19 }
20
21 impl DropFlagState {
22     pub fn value(self) -> bool {
23         match self {
24             DropFlagState::Present => true,
25             DropFlagState::Absent => false,
26         }
27     }
28 }
29
30 #[derive(Debug)]
31 pub enum DropStyle {
32     Dead,
33     Static,
34     Conditional,
35     Open,
36 }
37
38 #[derive(Debug)]
39 pub enum DropFlagMode {
40     Shallow,
41     Deep,
42 }
43
44 #[derive(Copy, Clone, Debug)]
45 pub enum Unwind {
46     To(BasicBlock),
47     InCleanup,
48 }
49
50 impl Unwind {
51     fn is_cleanup(self) -> bool {
52         match self {
53             Unwind::To(..) => false,
54             Unwind::InCleanup => true,
55         }
56     }
57
58     fn into_option(self) -> Option<BasicBlock> {
59         match self {
60             Unwind::To(bb) => Some(bb),
61             Unwind::InCleanup => None,
62         }
63     }
64
65     fn map<F>(self, f: F) -> Self
66     where
67         F: FnOnce(BasicBlock) -> BasicBlock,
68     {
69         match self {
70             Unwind::To(bb) => Unwind::To(f(bb)),
71             Unwind::InCleanup => Unwind::InCleanup,
72         }
73     }
74 }
75
76 pub trait DropElaborator<'a, 'tcx>: fmt::Debug {
77     type Path: Copy + fmt::Debug;
78
79     fn patch(&mut self) -> &mut MirPatch<'tcx>;
80     fn body(&self) -> &'a Body<'tcx>;
81     fn tcx(&self) -> TyCtxt<'tcx>;
82     fn param_env(&self) -> ty::ParamEnv<'tcx>;
83
84     fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle;
85     fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>>;
86     fn clear_drop_flag(&mut self, location: Location, path: Self::Path, mode: DropFlagMode);
87
88     fn field_subpath(&self, path: Self::Path, field: Field) -> Option<Self::Path>;
89     fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path>;
90     fn downcast_subpath(&self, path: Self::Path, variant: VariantIdx) -> Option<Self::Path>;
91     fn array_subpath(&self, path: Self::Path, index: u32, size: u32) -> Option<Self::Path>;
92 }
93
94 #[derive(Debug)]
95 struct DropCtxt<'l, 'b, 'tcx, D>
96 where
97     D: DropElaborator<'b, 'tcx>,
98 {
99     elaborator: &'l mut D,
100
101     source_info: SourceInfo,
102
103     place: &'l Place<'tcx>,
104     path: D::Path,
105     succ: BasicBlock,
106     unwind: Unwind,
107 }
108
109 pub fn elaborate_drop<'b, 'tcx, D>(
110     elaborator: &mut D,
111     source_info: SourceInfo,
112     place: &Place<'tcx>,
113     path: D::Path,
114     succ: BasicBlock,
115     unwind: Unwind,
116     bb: BasicBlock,
117 ) where
118     D: DropElaborator<'b, 'tcx>,
119     'tcx: 'b,
120 {
121     DropCtxt { elaborator, source_info, place, path, succ, unwind }.elaborate_drop(bb)
122 }
123
124 impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
125 where
126     D: DropElaborator<'b, 'tcx>,
127     'tcx: 'b,
128 {
129     fn place_ty(&self, place: &Place<'tcx>) -> Ty<'tcx> {
130         place.ty(self.elaborator.body(), self.tcx()).ty
131     }
132
133     fn tcx(&self) -> TyCtxt<'tcx> {
134         self.elaborator.tcx()
135     }
136
137     /// This elaborates a single drop instruction, located at `bb`, and
138     /// patches over it.
139     ///
140     /// The elaborated drop checks the drop flags to only drop what
141     /// is initialized.
142     ///
143     /// In addition, the relevant drop flags also need to be cleared
144     /// to avoid double-drops. However, in the middle of a complex
145     /// drop, one must avoid clearing some of the flags before they
146     /// are read, as that would cause a memory leak.
147     ///
148     /// In particular, when dropping an ADT, multiple fields may be
149     /// joined together under the `rest` subpath. They are all controlled
150     /// by the primary drop flag, but only the last rest-field dropped
151     /// should clear it (and it must also not clear anything else).
152     //
153     // FIXME: I think we should just control the flags externally,
154     // and then we do not need this machinery.
155     pub fn elaborate_drop(&mut self, bb: BasicBlock) {
156         debug!("elaborate_drop({:?}, {:?})", bb, self);
157         let style = self.elaborator.drop_style(self.path, DropFlagMode::Deep);
158         debug!("elaborate_drop({:?}, {:?}): live - {:?}", bb, self, style);
159         match style {
160             DropStyle::Dead => {
161                 self.elaborator
162                     .patch()
163                     .patch_terminator(bb, TerminatorKind::Goto { target: self.succ });
164             }
165             DropStyle::Static => {
166                 let loc = self.terminator_loc(bb);
167                 self.elaborator.clear_drop_flag(loc, self.path, DropFlagMode::Deep);
168                 self.elaborator.patch().patch_terminator(
169                     bb,
170                     TerminatorKind::Drop {
171                         location: *self.place,
172                         target: self.succ,
173                         unwind: self.unwind.into_option(),
174                     },
175                 );
176             }
177             DropStyle::Conditional => {
178                 let unwind = self.unwind; // FIXME(#43234)
179                 let succ = self.succ;
180                 let drop_bb = self.complete_drop(Some(DropFlagMode::Deep), succ, unwind);
181                 self.elaborator
182                     .patch()
183                     .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
184             }
185             DropStyle::Open => {
186                 let drop_bb = self.open_drop();
187                 self.elaborator
188                     .patch()
189                     .patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
190             }
191         }
192     }
193
194     /// Returns the place and move path for each field of `variant`,
195     /// (the move path is `None` if the field is a rest field).
196     fn move_paths_for_fields(
197         &self,
198         base_place: &Place<'tcx>,
199         variant_path: D::Path,
200         variant: &'tcx ty::VariantDef,
201         substs: SubstsRef<'tcx>,
202     ) -> Vec<(Place<'tcx>, Option<D::Path>)> {
203         variant
204             .fields
205             .iter()
206             .enumerate()
207             .map(|(i, f)| {
208                 let field = Field::new(i);
209                 let subpath = self.elaborator.field_subpath(variant_path, field);
210                 let tcx = self.tcx();
211
212                 assert_eq!(self.elaborator.param_env().reveal, Reveal::All);
213                 let field_ty =
214                     tcx.normalize_erasing_regions(self.elaborator.param_env(), f.ty(tcx, substs));
215                 (tcx.mk_place_field(base_place.clone(), field, field_ty), subpath)
216             })
217             .collect()
218     }
219
220     fn drop_subpath(
221         &mut self,
222         place: &Place<'tcx>,
223         path: Option<D::Path>,
224         succ: BasicBlock,
225         unwind: Unwind,
226     ) -> BasicBlock {
227         if let Some(path) = path {
228             debug!("drop_subpath: for std field {:?}", place);
229
230             DropCtxt {
231                 elaborator: self.elaborator,
232                 source_info: self.source_info,
233                 path,
234                 place,
235                 succ,
236                 unwind,
237             }
238             .elaborated_drop_block()
239         } else {
240             debug!("drop_subpath: for rest field {:?}", place);
241
242             DropCtxt {
243                 elaborator: self.elaborator,
244                 source_info: self.source_info,
245                 place,
246                 succ,
247                 unwind,
248                 // Using `self.path` here to condition the drop on
249                 // our own drop flag.
250                 path: self.path,
251             }
252             .complete_drop(None, succ, unwind)
253         }
254     }
255
256     /// Creates one-half of the drop ladder for a list of fields, and return
257     /// the list of steps in it in reverse order, with the first step
258     /// dropping 0 fields and so on.
259     ///
260     /// `unwind_ladder` is such a list of steps in reverse order,
261     /// which is called if the matching step of the drop glue panics.
262     fn drop_halfladder(
263         &mut self,
264         unwind_ladder: &[Unwind],
265         mut succ: BasicBlock,
266         fields: &[(Place<'tcx>, Option<D::Path>)],
267     ) -> Vec<BasicBlock> {
268         Some(succ)
269             .into_iter()
270             .chain(fields.iter().rev().zip(unwind_ladder).map(
271                 |(&(ref place, path), &unwind_succ)| {
272                     succ = self.drop_subpath(place, path, succ, unwind_succ);
273                     succ
274                 },
275             ))
276             .collect()
277     }
278
279     fn drop_ladder_bottom(&mut self) -> (BasicBlock, Unwind) {
280         // Clear the "master" drop flag at the end. This is needed
281         // because the "master" drop protects the ADT's discriminant,
282         // which is invalidated after the ADT is dropped.
283         let (succ, unwind) = (self.succ, self.unwind); // FIXME(#43234)
284         (
285             self.drop_flag_reset_block(DropFlagMode::Shallow, succ, unwind),
286             unwind.map(|unwind| {
287                 self.drop_flag_reset_block(DropFlagMode::Shallow, unwind, Unwind::InCleanup)
288             }),
289         )
290     }
291
292     /// Creates a full drop ladder, consisting of 2 connected half-drop-ladders
293     ///
294     /// For example, with 3 fields, the drop ladder is
295     ///
296     /// .d0:
297     ///     ELAB(drop location.0 [target=.d1, unwind=.c1])
298     /// .d1:
299     ///     ELAB(drop location.1 [target=.d2, unwind=.c2])
300     /// .d2:
301     ///     ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`])
302     /// .c1:
303     ///     ELAB(drop location.1 [target=.c2])
304     /// .c2:
305     ///     ELAB(drop location.2 [target=`self.unwind`])
306     ///
307     /// NOTE: this does not clear the master drop flag, so you need
308     /// to point succ/unwind on a `drop_ladder_bottom`.
309     fn drop_ladder(
310         &mut self,
311         fields: Vec<(Place<'tcx>, Option<D::Path>)>,
312         succ: BasicBlock,
313         unwind: Unwind,
314     ) -> (BasicBlock, Unwind) {
315         debug!("drop_ladder({:?}, {:?})", self, fields);
316
317         let mut fields = fields;
318         fields.retain(|&(ref place, _)| {
319             self.place_ty(place).needs_drop(self.tcx(), self.elaborator.param_env())
320         });
321
322         debug!("drop_ladder - fields needing drop: {:?}", fields);
323
324         let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1];
325         let unwind_ladder: Vec<_> = if let Unwind::To(target) = unwind {
326             let halfladder = self.drop_halfladder(&unwind_ladder, target, &fields);
327             halfladder.into_iter().map(Unwind::To).collect()
328         } else {
329             unwind_ladder
330         };
331
332         let normal_ladder = self.drop_halfladder(&unwind_ladder, succ, &fields);
333
334         (*normal_ladder.last().unwrap(), *unwind_ladder.last().unwrap())
335     }
336
337     fn open_drop_for_tuple(&mut self, tys: &[Ty<'tcx>]) -> BasicBlock {
338         debug!("open_drop_for_tuple({:?}, {:?})", self, tys);
339
340         let fields = tys
341             .iter()
342             .enumerate()
343             .map(|(i, &ty)| {
344                 (
345                     self.tcx().mk_place_field(self.place.clone(), Field::new(i), ty),
346                     self.elaborator.field_subpath(self.path, Field::new(i)),
347                 )
348             })
349             .collect();
350
351         let (succ, unwind) = self.drop_ladder_bottom();
352         self.drop_ladder(fields, succ, unwind).0
353     }
354
355     fn open_drop_for_box(&mut self, adt: &'tcx ty::AdtDef, substs: SubstsRef<'tcx>) -> BasicBlock {
356         debug!("open_drop_for_box({:?}, {:?}, {:?})", self, adt, substs);
357
358         let interior = self.tcx().mk_place_deref(self.place.clone());
359         let interior_path = self.elaborator.deref_subpath(self.path);
360
361         let succ = self.succ; // FIXME(#43234)
362         let unwind = self.unwind;
363         let succ = self.box_free_block(adt, substs, succ, unwind);
364         let unwind_succ =
365             self.unwind.map(|unwind| self.box_free_block(adt, substs, unwind, Unwind::InCleanup));
366
367         self.drop_subpath(&interior, interior_path, succ, unwind_succ)
368     }
369
370     fn open_drop_for_adt(&mut self, adt: &'tcx ty::AdtDef, substs: SubstsRef<'tcx>) -> BasicBlock {
371         debug!("open_drop_for_adt({:?}, {:?}, {:?})", self, adt, substs);
372         if adt.variants.is_empty() {
373             return self.elaborator.patch().new_block(BasicBlockData {
374                 statements: vec![],
375                 terminator: Some(Terminator {
376                     source_info: self.source_info,
377                     kind: TerminatorKind::Unreachable,
378                 }),
379                 is_cleanup: self.unwind.is_cleanup(),
380             });
381         }
382
383         let skip_contents =
384             adt.is_union() || Some(adt.did) == self.tcx().lang_items().manually_drop();
385         let contents_drop = if skip_contents {
386             (self.succ, self.unwind)
387         } else {
388             self.open_drop_for_adt_contents(adt, substs)
389         };
390
391         if adt.has_dtor(self.tcx()) {
392             self.destructor_call_block(contents_drop)
393         } else {
394             contents_drop.0
395         }
396     }
397
398     fn open_drop_for_adt_contents(
399         &mut self,
400         adt: &'tcx ty::AdtDef,
401         substs: SubstsRef<'tcx>,
402     ) -> (BasicBlock, Unwind) {
403         let (succ, unwind) = self.drop_ladder_bottom();
404         if !adt.is_enum() {
405             let fields = self.move_paths_for_fields(
406                 self.place,
407                 self.path,
408                 &adt.variants[VariantIdx::new(0)],
409                 substs,
410             );
411             self.drop_ladder(fields, succ, unwind)
412         } else {
413             self.open_drop_for_multivariant(adt, substs, succ, unwind)
414         }
415     }
416
417     fn open_drop_for_multivariant(
418         &mut self,
419         adt: &'tcx ty::AdtDef,
420         substs: SubstsRef<'tcx>,
421         succ: BasicBlock,
422         unwind: Unwind,
423     ) -> (BasicBlock, Unwind) {
424         let mut values = Vec::with_capacity(adt.variants.len());
425         let mut normal_blocks = Vec::with_capacity(adt.variants.len());
426         let mut unwind_blocks =
427             if unwind.is_cleanup() { None } else { Some(Vec::with_capacity(adt.variants.len())) };
428
429         let mut have_otherwise_with_drop_glue = false;
430         let mut have_otherwise = false;
431         let tcx = self.tcx();
432
433         for (variant_index, discr) in adt.discriminants(tcx) {
434             let variant = &adt.variants[variant_index];
435             let subpath = self.elaborator.downcast_subpath(self.path, variant_index);
436
437             if let Some(variant_path) = subpath {
438                 let base_place = tcx.mk_place_elem(
439                     self.place.clone(),
440                     ProjectionElem::Downcast(Some(variant.ident.name), variant_index),
441                 );
442                 let fields =
443                     self.move_paths_for_fields(&base_place, variant_path, &variant, substs);
444                 values.push(discr.val);
445                 if let Unwind::To(unwind) = unwind {
446                     // We can't use the half-ladder from the original
447                     // drop ladder, because this breaks the
448                     // "funclet can't have 2 successor funclets"
449                     // requirement from MSVC:
450                     //
451                     //           switch       unwind-switch
452                     //          /      \         /        \
453                     //         v1.0    v2.0  v2.0-unwind  v1.0-unwind
454                     //         |        |      /             |
455                     //    v1.1-unwind  v2.1-unwind           |
456                     //      ^                                |
457                     //       \-------------------------------/
458                     //
459                     // Create a duplicate half-ladder to avoid that. We
460                     // could technically only do this on MSVC, but I
461                     // I want to minimize the divergence between MSVC
462                     // and non-MSVC.
463
464                     let unwind_blocks = unwind_blocks.as_mut().unwrap();
465                     let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1];
466                     let halfladder = self.drop_halfladder(&unwind_ladder, unwind, &fields);
467                     unwind_blocks.push(halfladder.last().cloned().unwrap());
468                 }
469                 let (normal, _) = self.drop_ladder(fields, succ, unwind);
470                 normal_blocks.push(normal);
471             } else {
472                 have_otherwise = true;
473
474                 let param_env = self.elaborator.param_env();
475                 let have_field_with_drop_glue = variant
476                     .fields
477                     .iter()
478                     .any(|field| field.ty(tcx, substs).needs_drop(tcx, param_env));
479                 if have_field_with_drop_glue {
480                     have_otherwise_with_drop_glue = true;
481                 }
482             }
483         }
484
485         if !have_otherwise {
486             values.pop();
487         } else if !have_otherwise_with_drop_glue {
488             normal_blocks.push(self.goto_block(succ, unwind));
489             if let Unwind::To(unwind) = unwind {
490                 unwind_blocks.as_mut().unwrap().push(self.goto_block(unwind, Unwind::InCleanup));
491             }
492         } else {
493             normal_blocks.push(self.drop_block(succ, unwind));
494             if let Unwind::To(unwind) = unwind {
495                 unwind_blocks.as_mut().unwrap().push(self.drop_block(unwind, Unwind::InCleanup));
496             }
497         }
498
499         (
500             self.adt_switch_block(adt, normal_blocks, &values, succ, unwind),
501             unwind.map(|unwind| {
502                 self.adt_switch_block(
503                     adt,
504                     unwind_blocks.unwrap(),
505                     &values,
506                     unwind,
507                     Unwind::InCleanup,
508                 )
509             }),
510         )
511     }
512
513     fn adt_switch_block(
514         &mut self,
515         adt: &'tcx ty::AdtDef,
516         blocks: Vec<BasicBlock>,
517         values: &[u128],
518         succ: BasicBlock,
519         unwind: Unwind,
520     ) -> BasicBlock {
521         // If there are multiple variants, then if something
522         // is present within the enum the discriminant, tracked
523         // by the rest path, must be initialized.
524         //
525         // Additionally, we do not want to switch on the
526         // discriminant after it is free-ed, because that
527         // way lies only trouble.
528         let discr_ty = adt.repr.discr_type().to_ty(self.tcx());
529         let discr = Place::from(self.new_temp(discr_ty));
530         let discr_rv = Rvalue::Discriminant(*self.place);
531         let switch_block = BasicBlockData {
532             statements: vec![self.assign(&discr, discr_rv)],
533             terminator: Some(Terminator {
534                 source_info: self.source_info,
535                 kind: TerminatorKind::SwitchInt {
536                     discr: Operand::Move(discr),
537                     switch_ty: discr_ty,
538                     values: From::from(values.to_owned()),
539                     targets: blocks,
540                 },
541             }),
542             is_cleanup: unwind.is_cleanup(),
543         };
544         let switch_block = self.elaborator.patch().new_block(switch_block);
545         self.drop_flag_test_block(switch_block, succ, unwind)
546     }
547
548     fn destructor_call_block(&mut self, (succ, unwind): (BasicBlock, Unwind)) -> BasicBlock {
549         debug!("destructor_call_block({:?}, {:?})", self, succ);
550         let tcx = self.tcx();
551         let drop_trait = tcx.lang_items().drop_trait().unwrap();
552         let drop_fn = tcx.associated_items(drop_trait).in_definition_order().next().unwrap();
553         let ty = self.place_ty(self.place);
554         let substs = tcx.mk_substs_trait(ty, &[]);
555
556         let ref_ty =
557             tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut { ty, mutbl: hir::Mutability::Mut });
558         let ref_place = self.new_temp(ref_ty);
559         let unit_temp = Place::from(self.new_temp(tcx.mk_unit()));
560
561         let result = BasicBlockData {
562             statements: vec![self.assign(
563                 &Place::from(ref_place),
564                 Rvalue::Ref(
565                     tcx.lifetimes.re_erased,
566                     BorrowKind::Mut { allow_two_phase_borrow: false },
567                     *self.place,
568                 ),
569             )],
570             terminator: Some(Terminator {
571                 kind: TerminatorKind::Call {
572                     func: Operand::function_handle(
573                         tcx,
574                         drop_fn.def_id,
575                         substs,
576                         self.source_info.span,
577                     ),
578                     args: vec![Operand::Move(Place::from(ref_place))],
579                     destination: Some((unit_temp, succ)),
580                     cleanup: unwind.into_option(),
581                     from_hir_call: true,
582                 },
583                 source_info: self.source_info,
584             }),
585             is_cleanup: unwind.is_cleanup(),
586         };
587         self.elaborator.patch().new_block(result)
588     }
589
590     /// Create a loop that drops an array:
591     ///
592     /// ```text
593     /// loop-block:
594     ///    can_go = cur == length_or_end
595     ///    if can_go then succ else drop-block
596     /// drop-block:
597     ///    if ptr_based {
598     ///        ptr = cur
599     ///        cur = cur.offset(1)
600     ///    } else {
601     ///        ptr = &raw mut P[cur]
602     ///        cur = cur + 1
603     ///    }
604     ///    drop(ptr)
605     /// ```
606     fn drop_loop(
607         &mut self,
608         succ: BasicBlock,
609         cur: Local,
610         length_or_end: &Place<'tcx>,
611         ety: Ty<'tcx>,
612         unwind: Unwind,
613         ptr_based: bool,
614     ) -> BasicBlock {
615         let copy = |place: Place<'tcx>| Operand::Copy(place);
616         let move_ = |place: Place<'tcx>| Operand::Move(place);
617         let tcx = self.tcx();
618
619         let ptr_ty = tcx.mk_ptr(ty::TypeAndMut { ty: ety, mutbl: hir::Mutability::Mut });
620         let ptr = &Place::from(self.new_temp(ptr_ty));
621         let can_go = Place::from(self.new_temp(tcx.types.bool));
622
623         let one = self.constant_usize(1);
624         let (ptr_next, cur_next) = if ptr_based {
625             (Rvalue::Use(copy(cur.into())), Rvalue::BinaryOp(BinOp::Offset, move_(cur.into()), one))
626         } else {
627             (
628                 Rvalue::AddressOf(Mutability::Mut, tcx.mk_place_index(self.place.clone(), cur)),
629                 Rvalue::BinaryOp(BinOp::Add, move_(cur.into()), one),
630             )
631         };
632
633         let drop_block = BasicBlockData {
634             statements: vec![self.assign(ptr, ptr_next), self.assign(&Place::from(cur), cur_next)],
635             is_cleanup: unwind.is_cleanup(),
636             terminator: Some(Terminator {
637                 source_info: self.source_info,
638                 // this gets overwritten by drop elaboration.
639                 kind: TerminatorKind::Unreachable,
640             }),
641         };
642         let drop_block = self.elaborator.patch().new_block(drop_block);
643
644         let loop_block = BasicBlockData {
645             statements: vec![self.assign(
646                 &can_go,
647                 Rvalue::BinaryOp(BinOp::Eq, copy(Place::from(cur)), copy(*length_or_end)),
648             )],
649             is_cleanup: unwind.is_cleanup(),
650             terminator: Some(Terminator {
651                 source_info: self.source_info,
652                 kind: TerminatorKind::if_(tcx, move_(can_go), succ, drop_block),
653             }),
654         };
655         let loop_block = self.elaborator.patch().new_block(loop_block);
656
657         self.elaborator.patch().patch_terminator(
658             drop_block,
659             TerminatorKind::Drop {
660                 location: tcx.mk_place_deref(ptr.clone()),
661                 target: loop_block,
662                 unwind: unwind.into_option(),
663             },
664         );
665
666         loop_block
667     }
668
669     fn open_drop_for_array(&mut self, ety: Ty<'tcx>, opt_size: Option<u64>) -> BasicBlock {
670         debug!("open_drop_for_array({:?}, {:?})", ety, opt_size);
671
672         // if size_of::<ety>() == 0 {
673         //     index_based_loop
674         // } else {
675         //     ptr_based_loop
676         // }
677
678         let tcx = self.tcx();
679
680         if let Some(size) = opt_size {
681             let size: u32 = size.try_into().unwrap_or_else(|_| {
682                 bug!("move out check isn't implemented for array sizes bigger than u32::MAX");
683             });
684             let fields: Vec<(Place<'tcx>, Option<D::Path>)> = (0..size)
685                 .map(|i| {
686                     (
687                         tcx.mk_place_elem(
688                             self.place.clone(),
689                             ProjectionElem::ConstantIndex {
690                                 offset: i,
691                                 min_length: size,
692                                 from_end: false,
693                             },
694                         ),
695                         self.elaborator.array_subpath(self.path, i, size),
696                     )
697                 })
698                 .collect();
699
700             if fields.iter().any(|(_, path)| path.is_some()) {
701                 let (succ, unwind) = self.drop_ladder_bottom();
702                 return self.drop_ladder(fields, succ, unwind).0;
703             }
704         }
705
706         let move_ = |place: &Place<'tcx>| Operand::Move(*place);
707         let elem_size = &Place::from(self.new_temp(tcx.types.usize));
708         let len = &Place::from(self.new_temp(tcx.types.usize));
709
710         static USIZE_SWITCH_ZERO: &[u128] = &[0];
711
712         let base_block = BasicBlockData {
713             statements: vec![
714                 self.assign(elem_size, Rvalue::NullaryOp(NullOp::SizeOf, ety)),
715                 self.assign(len, Rvalue::Len(*self.place)),
716             ],
717             is_cleanup: self.unwind.is_cleanup(),
718             terminator: Some(Terminator {
719                 source_info: self.source_info,
720                 kind: TerminatorKind::SwitchInt {
721                     discr: move_(elem_size),
722                     switch_ty: tcx.types.usize,
723                     values: From::from(USIZE_SWITCH_ZERO),
724                     targets: vec![
725                         self.drop_loop_pair(ety, false, len.clone()),
726                         self.drop_loop_pair(ety, true, len.clone()),
727                     ],
728                 },
729             }),
730         };
731         self.elaborator.patch().new_block(base_block)
732     }
733
734     /// Creates a pair of drop-loops of `place`, which drops its contents, even
735     /// in the case of 1 panic. If `ptr_based`, creates a pointer loop,
736     /// otherwise create an index loop.
737     fn drop_loop_pair(
738         &mut self,
739         ety: Ty<'tcx>,
740         ptr_based: bool,
741         length: Place<'tcx>,
742     ) -> BasicBlock {
743         debug!("drop_loop_pair({:?}, {:?})", ety, ptr_based);
744         let tcx = self.tcx();
745         let iter_ty = if ptr_based { tcx.mk_mut_ptr(ety) } else { tcx.types.usize };
746
747         let cur = self.new_temp(iter_ty);
748         let length_or_end = if ptr_based { Place::from(self.new_temp(iter_ty)) } else { length };
749
750         let unwind = self.unwind.map(|unwind| {
751             self.drop_loop(unwind, cur, &length_or_end, ety, Unwind::InCleanup, ptr_based)
752         });
753
754         let loop_block = self.drop_loop(self.succ, cur, &length_or_end, ety, unwind, ptr_based);
755
756         let cur = Place::from(cur);
757         let drop_block_stmts = if ptr_based {
758             let tmp_ty = tcx.mk_mut_ptr(self.place_ty(self.place));
759             let tmp = Place::from(self.new_temp(tmp_ty));
760             // tmp = &raw mut P;
761             // cur = tmp as *mut T;
762             // end = Offset(cur, len);
763             vec![
764                 self.assign(&tmp, Rvalue::AddressOf(Mutability::Mut, *self.place)),
765                 self.assign(&cur, Rvalue::Cast(CastKind::Misc, Operand::Move(tmp), iter_ty)),
766                 self.assign(
767                     &length_or_end,
768                     Rvalue::BinaryOp(BinOp::Offset, Operand::Copy(cur), Operand::Move(length)),
769                 ),
770             ]
771         } else {
772             // cur = 0 (length already pushed)
773             let zero = self.constant_usize(0);
774             vec![self.assign(&cur, Rvalue::Use(zero))]
775         };
776         let drop_block = self.elaborator.patch().new_block(BasicBlockData {
777             statements: drop_block_stmts,
778             is_cleanup: unwind.is_cleanup(),
779             terminator: Some(Terminator {
780                 source_info: self.source_info,
781                 kind: TerminatorKind::Goto { target: loop_block },
782             }),
783         });
784
785         // FIXME(#34708): handle partially-dropped array/slice elements.
786         let reset_block = self.drop_flag_reset_block(DropFlagMode::Deep, drop_block, unwind);
787         self.drop_flag_test_block(reset_block, self.succ, unwind)
788     }
789
790     /// The slow-path - create an "open", elaborated drop for a type
791     /// which is moved-out-of only partially, and patch `bb` to a jump
792     /// to it. This must not be called on ADTs with a destructor,
793     /// as these can't be moved-out-of, except for `Box<T>`, which is
794     /// special-cased.
795     ///
796     /// This creates a "drop ladder" that drops the needed fields of the
797     /// ADT, both in the success case or if one of the destructors fail.
798     fn open_drop(&mut self) -> BasicBlock {
799         let ty = self.place_ty(self.place);
800         match ty.kind {
801             ty::Closure(def_id, substs) => {
802                 let tys: Vec<_> = substs.as_closure().upvar_tys(def_id, self.tcx()).collect();
803                 self.open_drop_for_tuple(&tys)
804             }
805             // Note that `elaborate_drops` only drops the upvars of a generator,
806             // and this is ok because `open_drop` here can only be reached
807             // within that own generator's resume function.
808             // This should only happen for the self argument on the resume function.
809             // It effetively only contains upvars until the generator transformation runs.
810             // See librustc_body/transform/generator.rs for more details.
811             ty::Generator(def_id, substs, _) => {
812                 let tys: Vec<_> = substs.as_generator().upvar_tys(def_id, self.tcx()).collect();
813                 self.open_drop_for_tuple(&tys)
814             }
815             ty::Tuple(..) => {
816                 let tys: Vec<_> = ty.tuple_fields().collect();
817                 self.open_drop_for_tuple(&tys)
818             }
819             ty::Adt(def, substs) => {
820                 if def.is_box() {
821                     self.open_drop_for_box(def, substs)
822                 } else {
823                     self.open_drop_for_adt(def, substs)
824                 }
825             }
826             ty::Dynamic(..) => {
827                 let unwind = self.unwind; // FIXME(#43234)
828                 let succ = self.succ;
829                 self.complete_drop(Some(DropFlagMode::Deep), succ, unwind)
830             }
831             ty::Array(ety, size) => {
832                 let size = size.try_eval_usize(self.tcx(), self.elaborator.param_env());
833                 self.open_drop_for_array(ety, size)
834             }
835             ty::Slice(ety) => self.open_drop_for_array(ety, None),
836
837             _ => bug!("open drop from non-ADT `{:?}`", ty),
838         }
839     }
840
841     /// Returns a basic block that drop a place using the context
842     /// and path in `c`. If `mode` is something, also clear `c`
843     /// according to it.
844     ///
845     /// if FLAG(self.path)
846     ///     if let Some(mode) = mode: FLAG(self.path)[mode] = false
847     ///     drop(self.place)
848     fn complete_drop(
849         &mut self,
850         drop_mode: Option<DropFlagMode>,
851         succ: BasicBlock,
852         unwind: Unwind,
853     ) -> BasicBlock {
854         debug!("complete_drop({:?},{:?})", self, drop_mode);
855
856         let drop_block = self.drop_block(succ, unwind);
857         let drop_block = if let Some(mode) = drop_mode {
858             self.drop_flag_reset_block(mode, drop_block, unwind)
859         } else {
860             drop_block
861         };
862
863         self.drop_flag_test_block(drop_block, succ, unwind)
864     }
865
866     fn drop_flag_reset_block(
867         &mut self,
868         mode: DropFlagMode,
869         succ: BasicBlock,
870         unwind: Unwind,
871     ) -> BasicBlock {
872         debug!("drop_flag_reset_block({:?},{:?})", self, mode);
873
874         let block = self.new_block(unwind, TerminatorKind::Goto { target: succ });
875         let block_start = Location { block, statement_index: 0 };
876         self.elaborator.clear_drop_flag(block_start, self.path, mode);
877         block
878     }
879
880     fn elaborated_drop_block(&mut self) -> BasicBlock {
881         debug!("elaborated_drop_block({:?})", self);
882         let unwind = self.unwind; // FIXME(#43234)
883         let succ = self.succ;
884         let blk = self.drop_block(succ, unwind);
885         self.elaborate_drop(blk);
886         blk
887     }
888
889     fn box_free_block(
890         &mut self,
891         adt: &'tcx ty::AdtDef,
892         substs: SubstsRef<'tcx>,
893         target: BasicBlock,
894         unwind: Unwind,
895     ) -> BasicBlock {
896         let block = self.unelaborated_free_block(adt, substs, target, unwind);
897         self.drop_flag_test_block(block, target, unwind)
898     }
899
900     fn unelaborated_free_block(
901         &mut self,
902         adt: &'tcx ty::AdtDef,
903         substs: SubstsRef<'tcx>,
904         target: BasicBlock,
905         unwind: Unwind,
906     ) -> BasicBlock {
907         let tcx = self.tcx();
908         let unit_temp = Place::from(self.new_temp(tcx.mk_unit()));
909         let free_func =
910             tcx.require_lang_item(lang_items::BoxFreeFnLangItem, Some(self.source_info.span));
911         let args = adt.variants[VariantIdx::new(0)]
912             .fields
913             .iter()
914             .enumerate()
915             .map(|(i, f)| {
916                 let field = Field::new(i);
917                 let field_ty = f.ty(tcx, substs);
918                 Operand::Move(tcx.mk_place_field(self.place.clone(), field, field_ty))
919             })
920             .collect();
921
922         let call = TerminatorKind::Call {
923             func: Operand::function_handle(tcx, free_func, substs, self.source_info.span),
924             args,
925             destination: Some((unit_temp, target)),
926             cleanup: None,
927             from_hir_call: false,
928         }; // FIXME(#43234)
929         let free_block = self.new_block(unwind, call);
930
931         let block_start = Location { block: free_block, statement_index: 0 };
932         self.elaborator.clear_drop_flag(block_start, self.path, DropFlagMode::Shallow);
933         free_block
934     }
935
936     fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
937         let block =
938             TerminatorKind::Drop { location: *self.place, target, unwind: unwind.into_option() };
939         self.new_block(unwind, block)
940     }
941
942     fn goto_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
943         let block = TerminatorKind::Goto { target };
944         self.new_block(unwind, block)
945     }
946
947     fn drop_flag_test_block(
948         &mut self,
949         on_set: BasicBlock,
950         on_unset: BasicBlock,
951         unwind: Unwind,
952     ) -> BasicBlock {
953         let style = self.elaborator.drop_style(self.path, DropFlagMode::Shallow);
954         debug!(
955             "drop_flag_test_block({:?},{:?},{:?},{:?}) - {:?}",
956             self, on_set, on_unset, unwind, style
957         );
958
959         match style {
960             DropStyle::Dead => on_unset,
961             DropStyle::Static => on_set,
962             DropStyle::Conditional | DropStyle::Open => {
963                 let flag = self.elaborator.get_drop_flag(self.path).unwrap();
964                 let term = TerminatorKind::if_(self.tcx(), flag, on_set, on_unset);
965                 self.new_block(unwind, term)
966             }
967         }
968     }
969
970     fn new_block(&mut self, unwind: Unwind, k: TerminatorKind<'tcx>) -> BasicBlock {
971         self.elaborator.patch().new_block(BasicBlockData {
972             statements: vec![],
973             terminator: Some(Terminator { source_info: self.source_info, kind: k }),
974             is_cleanup: unwind.is_cleanup(),
975         })
976     }
977
978     fn new_temp(&mut self, ty: Ty<'tcx>) -> Local {
979         self.elaborator.patch().new_temp(ty, self.source_info.span)
980     }
981
982     fn terminator_loc(&mut self, bb: BasicBlock) -> Location {
983         let body = self.elaborator.body();
984         self.elaborator.patch().terminator_loc(body, bb)
985     }
986
987     fn constant_usize(&self, val: u16) -> Operand<'tcx> {
988         Operand::Constant(box Constant {
989             span: self.source_info.span,
990             user_ty: None,
991             literal: ty::Const::from_usize(self.tcx(), val.into()),
992         })
993     }
994
995     fn assign(&self, lhs: &Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> {
996         Statement { source_info: self.source_info, kind: StatementKind::Assign(box (*lhs, rhs)) }
997     }
998 }