]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/add_moves_for_packed_drops.rs
Rollup merge of #69949 - rust-lang:triagebot-ping-alias, r=Mark-Simulacrum
[rust.git] / src / librustc_mir / transform / add_moves_for_packed_drops.rs
1 use rustc::mir::*;
2 use rustc::ty::TyCtxt;
3 use rustc_hir::def_id::DefId;
4
5 use crate::transform::{MirPass, MirSource};
6 use crate::util;
7 use crate::util::patch::MirPatch;
8
9 // This pass moves values being dropped that are within a packed
10 // struct to a separate local before dropping them, to ensure that
11 // they are dropped from an aligned address.
12 //
13 // For example, if we have something like
14 // ```Rust
15 //     #[repr(packed)]
16 //     struct Foo {
17 //         dealign: u8,
18 //         data: Vec<u8>
19 //     }
20 //
21 //     let foo = ...;
22 // ```
23 //
24 // We want to call `drop_in_place::<Vec<u8>>` on `data` from an aligned
25 // address. This means we can't simply drop `foo.data` directly, because
26 // its address is not aligned.
27 //
28 // Instead, we move `foo.data` to a local and drop that:
29 // ```
30 //     storage.live(drop_temp)
31 //     drop_temp = foo.data;
32 //     drop(drop_temp) -> next
33 // next:
34 //     storage.dead(drop_temp)
35 // ```
36 //
37 // The storage instructions are required to avoid stack space
38 // blowup.
39
40 pub struct AddMovesForPackedDrops;
41
42 impl<'tcx> MirPass<'tcx> for AddMovesForPackedDrops {
43     fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
44         debug!("add_moves_for_packed_drops({:?} @ {:?})", src, body.span);
45         add_moves_for_packed_drops(tcx, body, src.def_id());
46     }
47 }
48
49 pub fn add_moves_for_packed_drops<'tcx>(
50     tcx: TyCtxt<'tcx>,
51     body: &mut BodyAndCache<'tcx>,
52     def_id: DefId,
53 ) {
54     let patch = add_moves_for_packed_drops_patch(tcx, body, def_id);
55     patch.apply(body);
56 }
57
58 fn add_moves_for_packed_drops_patch<'tcx>(
59     tcx: TyCtxt<'tcx>,
60     body: &Body<'tcx>,
61     def_id: DefId,
62 ) -> MirPatch<'tcx> {
63     let mut patch = MirPatch::new(body);
64     let param_env = tcx.param_env(def_id);
65
66     for (bb, data) in body.basic_blocks().iter_enumerated() {
67         let loc = Location { block: bb, statement_index: data.statements.len() };
68         let terminator = data.terminator();
69
70         match terminator.kind {
71             TerminatorKind::Drop { ref location, .. }
72                 if util::is_disaligned(tcx, body, param_env, location) =>
73             {
74                 add_move_for_packed_drop(tcx, body, &mut patch, terminator, loc, data.is_cleanup);
75             }
76             TerminatorKind::DropAndReplace { .. } => {
77                 span_bug!(terminator.source_info.span, "replace in AddMovesForPackedDrops");
78             }
79             _ => {}
80         }
81     }
82
83     patch
84 }
85
86 fn add_move_for_packed_drop<'tcx>(
87     tcx: TyCtxt<'tcx>,
88     body: &Body<'tcx>,
89     patch: &mut MirPatch<'tcx>,
90     terminator: &Terminator<'tcx>,
91     loc: Location,
92     is_cleanup: bool,
93 ) {
94     debug!("add_move_for_packed_drop({:?} @ {:?})", terminator, loc);
95     let (location, target, unwind) = match terminator.kind {
96         TerminatorKind::Drop { ref location, target, unwind } => (location, target, unwind),
97         _ => unreachable!(),
98     };
99
100     let source_info = terminator.source_info;
101     let ty = location.ty(body, tcx).ty;
102     let temp = patch.new_temp(ty, terminator.source_info.span);
103
104     let storage_dead_block = patch.new_block(BasicBlockData {
105         statements: vec![Statement { source_info, kind: StatementKind::StorageDead(temp) }],
106         terminator: Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
107         is_cleanup,
108     });
109
110     patch.add_statement(loc, StatementKind::StorageLive(temp));
111     patch.add_assign(loc, Place::from(temp), Rvalue::Use(Operand::Move(*location)));
112     patch.patch_terminator(
113         loc.block,
114         TerminatorKind::Drop { location: Place::from(temp), target: storage_dead_block, unwind },
115     );
116 }