]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/util/elaborate_drops.rs
Rollup merge of #61844 - AaronKutch:master, r=Centril
[rust.git] / src / librustc_mir / util / elaborate_drops.rs
1 use std::fmt;
2 use rustc::hir;
3 use rustc::mir::*;
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;
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 where F: FnOnce(BasicBlock) -> BasicBlock {
66         match self {
67             Unwind::To(bb) => Unwind::To(f(bb)),
68             Unwind::InCleanup => Unwind::InCleanup
69         }
70     }
71 }
72
73 pub trait DropElaborator<'a, 'tcx: 'a>: fmt::Debug {
74     type Path : Copy + fmt::Debug;
75
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>;
80
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);
84
85
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>;
90 }
91
92 #[derive(Debug)]
93 struct DropCtxt<'l, 'b: 'l, 'tcx: 'b, D>
94     where D : DropElaborator<'b, 'tcx> + 'l
95 {
96     elaborator: &'l mut D,
97
98     source_info: SourceInfo,
99
100     place: &'l Place<'tcx>,
101     path: D::Path,
102     succ: BasicBlock,
103     unwind: Unwind,
104 }
105
106 pub fn elaborate_drop<'b, 'tcx, D>(
107     elaborator: &mut D,
108     source_info: SourceInfo,
109     place: &Place<'tcx>,
110     path: D::Path,
111     succ: BasicBlock,
112     unwind: Unwind,
113     bb: BasicBlock)
114     where D: DropElaborator<'b, 'tcx>
115 {
116     DropCtxt {
117         elaborator, source_info, place, path, succ, unwind
118     }.elaborate_drop(bb)
119 }
120
121 impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
122 where
123     D: DropElaborator<'b, 'tcx>,
124 {
125     fn place_ty(&self, place: &Place<'tcx>) -> Ty<'tcx> {
126         place.ty(self.elaborator.body(), self.tcx()).ty
127     }
128
129     fn tcx(&self) -> TyCtxt<'tcx> {
130         self.elaborator.tcx()
131     }
132
133     /// This elaborates a single drop instruction, located at `bb`, and
134     /// patches over it.
135     ///
136     /// The elaborated drop checks the drop flags to only drop what
137     /// is initialized.
138     ///
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.
143     ///
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).
148     //
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);
155         match style {
156             DropStyle::Dead => {
157                 self.elaborator.patch().patch_terminator(bb, TerminatorKind::Goto {
158                     target: self.succ
159                 });
160             }
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(),
166                     target: self.succ,
167                     unwind: self.unwind.into_option(),
168                 });
169             }
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 {
175                     target: drop_bb
176                 });
177             }
178             DropStyle::Open => {
179                 let drop_bb = self.open_drop();
180                 self.elaborator.patch().patch_terminator(bb, TerminatorKind::Goto {
181                     target: drop_bb
182                 });
183             }
184         }
185     }
186
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>)>
195     {
196         variant.fields.iter().enumerate().map(|(i, f)| {
197             let field = Field::new(i);
198             let subpath = self.elaborator.field_subpath(variant_path, field);
199
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),
204             );
205             (base_place.clone().field(field, field_ty), subpath)
206         }).collect()
207     }
208
209     fn drop_subpath(&mut self,
210                     place: &Place<'tcx>,
211                     path: Option<D::Path>,
212                     succ: BasicBlock,
213                     unwind: Unwind)
214                     -> BasicBlock
215     {
216         if let Some(path) = path {
217             debug!("drop_subpath: for std field {:?}", place);
218
219             DropCtxt {
220                 elaborator: self.elaborator,
221                 source_info: self.source_info,
222                 path, place, succ, unwind,
223             }.elaborated_drop_block()
224         } else {
225             debug!("drop_subpath: for rest field {:?}", place);
226
227             DropCtxt {
228                 elaborator: self.elaborator,
229                 source_info: self.source_info,
230                 place, succ, unwind,
231                 // Using `self.path` here to condition the drop on
232                 // our own drop flag.
233                 path: self.path
234             }.complete_drop(None, succ, unwind)
235         }
236     }
237
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.
241     ///
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>)])
248                        -> Vec<BasicBlock>
249     {
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);
254                     succ
255                 })
256         ).collect()
257     }
258
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)
264         (
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)
268             })
269         )
270     }
271
272     /// Creates a full drop ladder, consisting of 2 connected half-drop-ladders
273     ///
274     /// For example, with 3 fields, the drop ladder is
275     ///
276     /// .d0:
277     ///     ELAB(drop location.0 [target=.d1, unwind=.c1])
278     /// .d1:
279     ///     ELAB(drop location.1 [target=.d2, unwind=.c2])
280     /// .d2:
281     ///     ELAB(drop location.2 [target=`self.succ`, unwind=`self.unwind`])
282     /// .c1:
283     ///     ELAB(drop location.1 [target=.c2])
284     /// .c2:
285     ///     ELAB(drop location.2 [target=`self.unwind`])
286     ///
287     /// NOTE: this does not clear the master drop flag, so you need
288     /// to point succ/unwind on a `drop_ladder_bottom`.
289     fn drop_ladder(
290         &mut self,
291         fields: Vec<(Place<'tcx>, Option<D::Path>)>,
292         succ: BasicBlock,
293         unwind: Unwind,
294     ) -> (BasicBlock, Unwind) {
295         debug!("drop_ladder({:?}, {:?})", self, fields);
296
297         let mut fields = fields;
298         fields.retain(|&(ref place, _)| {
299             self.place_ty(place).needs_drop(self.tcx(), self.elaborator.param_env())
300         });
301
302         debug!("drop_ladder - fields needing drop: {:?}", fields);
303
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()
308         } else {
309             unwind_ladder
310         };
311
312         let normal_ladder =
313             self.drop_halfladder(&unwind_ladder, succ, &fields);
314
315         (*normal_ladder.last().unwrap(), *unwind_ladder.last().unwrap())
316     }
317
318     fn open_drop_for_tuple(&mut self, tys: &[Ty<'tcx>]) -> BasicBlock {
319         debug!("open_drop_for_tuple({:?}, {:?})", self, tys);
320
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)))
324         }).collect();
325
326         let (succ, unwind) = self.drop_ladder_bottom();
327         self.drop_ladder(fields, succ, unwind).0
328     }
329
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);
332
333         let interior = self.place.clone().deref();
334         let interior_path = self.elaborator.deref_subpath(self.path);
335
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)
341         });
342
343         self.drop_subpath(&interior, interior_path, succ, unwind_succ)
344     }
345
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 {
350                 statements: vec![],
351                 terminator: Some(Terminator {
352                     source_info: self.source_info,
353                     kind: TerminatorKind::Unreachable
354                 }),
355                 is_cleanup: self.unwind.is_cleanup()
356             });
357         }
358
359         let skip_contents =
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)
363         } else {
364             self.open_drop_for_adt_contents(adt, substs)
365         };
366
367         if adt.has_dtor(self.tcx()) {
368             self.destructor_call_block(contents_drop)
369         } else {
370             contents_drop.0
371         }
372     }
373
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();
378         if !adt.is_enum() {
379             let fields = self.move_paths_for_fields(
380                 self.place,
381                 self.path,
382                 &adt.variants[VariantIdx::new(0)],
383                 substs
384             );
385             self.drop_ladder(fields, succ, unwind)
386         } else {
387             self.open_drop_for_multivariant(adt, substs, succ, unwind)
388         }
389     }
390
391     fn open_drop_for_multivariant(&mut self, adt: &'tcx ty::AdtDef,
392                                   substs: SubstsRef<'tcx>,
393                                   succ: BasicBlock,
394                                   unwind: Unwind)
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() {
399             None
400         } else {
401             Some(Vec::with_capacity(adt.variants.len()))
402         };
403
404         let mut have_otherwise = false;
405
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),
412                                              variant_index));
413                 let fields = self.move_paths_for_fields(
414                     &base_place,
415                     variant_path,
416                     &adt.variants[variant_index],
417                     substs);
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:
424                     //
425                     //           switch       unwind-switch
426                     //          /      \         /        \
427                     //         v1.0    v2.0  v2.0-unwind  v1.0-unwind
428                     //         |        |      /             |
429                     //    v1.1-unwind  v2.1-unwind           |
430                     //      ^                                |
431                     //       \-------------------------------/
432                     //
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
436                     // and non-MSVC.
437
438                     let unwind_blocks = unwind_blocks.as_mut().unwrap();
439                     let unwind_ladder = vec![Unwind::InCleanup; fields.len() + 1];
440                     let halfladder =
441                         self.drop_halfladder(&unwind_ladder, unwind, &fields);
442                     unwind_blocks.push(halfladder.last().cloned().unwrap());
443                 }
444                 let (normal, _) = self.drop_ladder(fields, succ, unwind);
445                 normal_blocks.push(normal);
446             } else {
447                 have_otherwise = true;
448             }
449         }
450
451         if have_otherwise {
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)
456                         );
457             }
458         } else {
459             values.pop();
460         }
461
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
466              )
467          }))
468     }
469
470     fn adt_switch_block(&mut self,
471                         adt: &'tcx ty::AdtDef,
472                         blocks: Vec<BasicBlock>,
473                         values: &[u128],
474                         succ: BasicBlock,
475                         unwind: Unwind)
476                         -> 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.
480         //
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),
493                     switch_ty: discr_ty,
494                     values: From::from(values.to_owned()),
495                     targets: blocks,
496                 }
497             }),
498             is_cleanup: unwind.is_cleanup(),
499         };
500         let switch_block = self.elaborator.patch().new_block(switch_block);
501         self.drop_flag_test_block(switch_block, succ, unwind)
502     }
503
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, &[]);
511
512         let ref_ty = tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut {
513             ty,
514             mutbl: hir::Mutability::MutMutable
515         });
516         let ref_place = self.new_temp(ref_ty);
517         let unit_temp = Place::Base(PlaceBase::Local(self.new_temp(tcx.mk_unit())));
518
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 },
524                             self.place.clone())
525             )],
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(),
533                     from_hir_call: true,
534                 },
535                 source_info: self.source_info,
536             }),
537             is_cleanup: unwind.is_cleanup(),
538         };
539         self.elaborator.patch().new_block(result)
540     }
541
542     /// Create a loop that drops an array:
543     ///
544     /// ```text
545     /// loop-block:
546     ///    can_go = cur == length_or_end
547     ///    if can_go then succ else drop-block
548     /// drop-block:
549     ///    if ptr_based {
550     ///        ptr = &mut *cur
551     ///        cur = cur.offset(1)
552     ///    } else {
553     ///        ptr = &mut P[cur]
554     ///        cur = cur + 1
555     ///    }
556     ///    drop(ptr)
557     /// ```
558     fn drop_loop(
559         &mut self,
560         succ: BasicBlock,
561         cur: Local,
562         length_or_end: &Place<'tcx>,
563         ety: Ty<'tcx>,
564         unwind: Unwind,
565         ptr_based: bool,
566     ) -> BasicBlock {
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();
570
571         let ref_ty = tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut {
572             ty: ety,
573             mutbl: hir::Mutability::MutMutable
574         });
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)));
577
578         let one = self.constant_usize(1);
579         let (ptr_next, cur_next) = if ptr_based {
580             (Rvalue::Ref(
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,
586                 }))
587              ),
588              Rvalue::BinaryOp(BinOp::Offset, move_(&Place::Base(PlaceBase::Local(cur))), one))
589         } else {
590             (Rvalue::Ref(
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))
595         };
596
597         let drop_block = BasicBlockData {
598             statements: vec![
599                 self.assign(ptr, ptr_next),
600                 self.assign(&Place::Base(PlaceBase::Local(cur)), cur_next)
601             ],
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,
607             })
608         };
609         let drop_block = self.elaborator.patch().new_block(drop_block);
610
611         let loop_block = BasicBlockData {
612             statements: vec![
613                 self.assign(can_go, Rvalue::BinaryOp(BinOp::Eq,
614                                                      copy(&Place::Base(PlaceBase::Local(cur))),
615                                                      copy(length_or_end)))
616             ],
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)
621             })
622         };
623         let loop_block = self.elaborator.patch().new_block(loop_block);
624
625         self.elaborator.patch().patch_terminator(drop_block, TerminatorKind::Drop {
626             location: ptr.clone().deref(),
627             target: loop_block,
628             unwind: unwind.into_option()
629         });
630
631         loop_block
632     }
633
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);
636
637         // if size_of::<ety>() == 0 {
638         //     index_based_loop
639         // } else {
640         //     ptr_based_loop
641         // }
642
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");
646             });
647             let fields: Vec<(Place<'tcx>, Option<D::Path>)> = (0..size).map(|i| {
648                 (self.place.clone().elem(ProjectionElem::ConstantIndex{
649                     offset: i,
650                     min_length: size,
651                     from_end: false
652                 }),
653                  self.elaborator.array_subpath(self.path, i, size))
654             }).collect();
655
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
659             }
660         }
661
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)));
666
667         static USIZE_SWITCH_ZERO: &[u128] = &[0];
668
669         let base_block = BasicBlockData {
670             statements: vec![
671                 self.assign(elem_size, Rvalue::NullaryOp(NullOp::SizeOf, ety)),
672                 self.assign(len, Rvalue::Len(self.place.clone())),
673             ],
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),
681                     targets: vec![
682                         self.drop_loop_pair(ety, false, len.clone()),
683                         self.drop_loop_pair(ety, true, len.clone()),
684                     ],
685                 },
686             })
687         };
688         self.elaborator.patch().new_block(base_block)
689     }
690
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.
694     fn drop_loop_pair(
695         &mut self,
696         ety: Ty<'tcx>,
697         ptr_based: bool,
698         length: Place<'tcx>,
699     ) -> BasicBlock {
700         debug!("drop_loop_pair({:?}, {:?})", ety, ptr_based);
701         let tcx = self.tcx();
702         let iter_ty = if ptr_based {
703             tcx.mk_mut_ptr(ety)
704         } else {
705             tcx.types.usize
706         };
707
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))
714         } else {
715             length.clone()
716         };
717
718         let unwind = self.unwind.map(|unwind| {
719             self.drop_loop(unwind,
720                            cur,
721                            &length_or_end,
722                            ety,
723                            Unwind::InCleanup,
724                            ptr_based)
725         });
726
727         let loop_block = self.drop_loop(
728             self.succ,
729             cur,
730             &length_or_end,
731             ety,
732             unwind,
733             ptr_based);
734
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)));
739             // tmp = &mut P;
740             // cur = tmp as *mut T;
741             // end = Offset(cur, len);
742             vec![
743                 self.assign(&tmp, Rvalue::Ref(
744                     tcx.lifetimes.re_erased,
745                     BorrowKind::Mut { allow_two_phase_borrow: false },
746                     self.place.clone()
747                 )),
748                 self.assign(
749                     &cur,
750                     Rvalue::Cast(CastKind::Misc, Operand::Move(tmp), iter_ty),
751                 ),
752                 self.assign(
753                     &length_or_end,
754                     Rvalue::BinaryOp(BinOp::Offset, Operand::Copy(cur), Operand::Move(length)
755                 )),
756             ]
757         } else {
758             // cur = 0 (length already pushed)
759             let zero = self.constant_usize(0);
760             vec![self.assign(&cur, Rvalue::Use(zero))]
761         };
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 }
768             })
769         });
770
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)
774     }
775
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
780     /// special-cased.
781     ///
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);
786         match ty.sty {
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)
790             }
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)
800             }
801             ty::Tuple(tys) => {
802                 let tys: Vec<_> = tys.iter().map(|k| k.expect_ty()).collect();
803                 self.open_drop_for_tuple(&tys)
804             }
805             ty::Adt(def, substs) => {
806                 if def.is_box() {
807                     self.open_drop_for_box(def, substs)
808                 } else {
809                     self.open_drop_for_adt(def, substs)
810                 }
811             }
812             ty::Dynamic(..) => {
813                 let unwind = self.unwind; // FIXME(#43234)
814                 let succ = self.succ;
815                 self.complete_drop(Some(DropFlagMode::Deep), succ, unwind)
816             }
817             ty::Array(ety, size) => {
818                 let size = size.assert_usize(self.tcx());
819                 self.open_drop_for_array(ety, size)
820             },
821             ty::Slice(ety) => self.open_drop_for_array(ety, None),
822
823             _ => bug!("open drop from non-ADT `{:?}`", ty)
824         }
825     }
826
827     /// Returns a basic block that drop a place using the context
828     /// and path in `c`. If `mode` is something, also clear `c`
829     /// according to it.
830     ///
831     /// if FLAG(self.path)
832     ///     if let Some(mode) = mode: FLAG(self.path)[mode] = false
833     ///     drop(self.place)
834     fn complete_drop(
835         &mut self,
836         drop_mode: Option<DropFlagMode>,
837         succ: BasicBlock,
838         unwind: Unwind,
839     ) -> BasicBlock {
840         debug!("complete_drop({:?},{:?})", self, drop_mode);
841
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)
845         } else {
846             drop_block
847         };
848
849         self.drop_flag_test_block(drop_block, succ, unwind)
850     }
851
852     fn drop_flag_reset_block(&mut self,
853                              mode: DropFlagMode,
854                              succ: BasicBlock,
855                              unwind: Unwind) -> BasicBlock
856     {
857         debug!("drop_flag_reset_block({:?},{:?})", self, mode);
858
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);
862         block
863     }
864
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);
871         blk
872     }
873
874     fn box_free_block(
875         &mut self,
876         adt: &'tcx ty::AdtDef,
877         substs: SubstsRef<'tcx>,
878         target: BasicBlock,
879         unwind: Unwind,
880     ) -> BasicBlock {
881         let block = self.unelaborated_free_block(adt, substs, target, unwind);
882         self.drop_flag_test_block(block, target, unwind)
883     }
884
885     fn unelaborated_free_block(
886         &mut self,
887         adt: &'tcx ty::AdtDef,
888         substs: SubstsRef<'tcx>,
889         target: BasicBlock,
890         unwind: Unwind,
891     ) -> BasicBlock {
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))
899         }).collect();
900
901         let call = TerminatorKind::Call {
902             func: Operand::function_handle(tcx, free_func, substs, self.source_info.span),
903             args: args,
904             destination: Some((unit_temp, target)),
905             cleanup: None,
906             from_hir_call: false,
907         }; // FIXME(#43234)
908         let free_block = self.new_block(unwind, call);
909
910         let block_start = Location { block: free_block, statement_index: 0 };
911         self.elaborator.clear_drop_flag(block_start, self.path, DropFlagMode::Shallow);
912         free_block
913     }
914
915     fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
916         let block = TerminatorKind::Drop {
917             location: self.place.clone(),
918             target,
919             unwind: unwind.into_option()
920         };
921         self.new_block(unwind, block)
922     }
923
924     fn drop_flag_test_block(&mut self,
925                             on_set: BasicBlock,
926                             on_unset: BasicBlock,
927                             unwind: Unwind)
928                             -> BasicBlock
929     {
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);
933
934         match 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)
941             }
942         }
943     }
944
945     fn new_block(&mut self, unwind: Unwind, k: TerminatorKind<'tcx>) -> BasicBlock {
946         self.elaborator.patch().new_block(BasicBlockData {
947             statements: vec![],
948             terminator: Some(Terminator {
949                 source_info: self.source_info, kind: k
950             }),
951             is_cleanup: unwind.is_cleanup()
952         })
953     }
954
955     fn new_temp(&mut self, ty: Ty<'tcx>) -> Local {
956         self.elaborator.patch().new_temp(ty, self.source_info.span)
957     }
958
959     fn terminator_loc(&mut self, bb: BasicBlock) -> Location {
960         let body = self.elaborator.body();
961         self.elaborator.patch().terminator_loc(body, bb)
962     }
963
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,
968             user_ty: None,
969             literal: ty::Const::from_usize(self.tcx(), val.into()),
970         })
971     }
972
973     fn assign(&self, lhs: &Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> {
974         Statement {
975             source_info: self.source_info,
976             kind: StatementKind::Assign(lhs.clone(), box rhs)
977         }
978     }
979 }