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