1 use rustc_middle
::mir
::*;
2 use rustc_middle
::ty
::TyCtxt
;
6 use rustc_middle
::mir
::patch
::MirPatch
;
8 // This pass moves values being dropped that are within a packed
9 // struct to a separate local before dropping them, to ensure that
10 // they are dropped from an aligned address.
12 // For example, if we have something like
23 // We want to call `drop_in_place::<Vec<u8>>` on `data` from an aligned
24 // address. This means we can't simply drop `foo.data` directly, because
25 // its address is not aligned.
27 // Instead, we move `foo.data` to a local and drop that:
29 // storage.live(drop_temp)
30 // drop_temp = foo.data;
31 // drop(drop_temp) -> next
33 // storage.dead(drop_temp)
36 // The storage instructions are required to avoid stack space
39 pub struct AddMovesForPackedDrops
;
41 impl<'tcx
> MirPass
<'tcx
> for AddMovesForPackedDrops
{
42 fn run_pass(&self, tcx
: TyCtxt
<'tcx
>, body
: &mut Body
<'tcx
>) {
43 debug
!("add_moves_for_packed_drops({:?} @ {:?})", body
.source
, body
.span
);
44 add_moves_for_packed_drops(tcx
, body
);
48 pub fn add_moves_for_packed_drops
<'tcx
>(tcx
: TyCtxt
<'tcx
>, body
: &mut Body
<'tcx
>) {
49 let patch
= add_moves_for_packed_drops_patch(tcx
, body
);
53 fn add_moves_for_packed_drops_patch
<'tcx
>(tcx
: TyCtxt
<'tcx
>, body
: &Body
<'tcx
>) -> MirPatch
<'tcx
> {
54 let def_id
= body
.source
.def_id();
55 let mut patch
= MirPatch
::new(body
);
56 let param_env
= tcx
.param_env(def_id
);
58 for (bb
, data
) in body
.basic_blocks
.iter_enumerated() {
59 let loc
= Location { block: bb, statement_index: data.statements.len() }
;
60 let terminator
= data
.terminator();
62 match terminator
.kind
{
63 TerminatorKind
::Drop { place, .. }
64 if util
::is_disaligned(tcx
, body
, param_env
, place
) =>
66 add_move_for_packed_drop(tcx
, body
, &mut patch
, terminator
, loc
, data
.is_cleanup
);
68 TerminatorKind
::DropAndReplace { .. }
=> {
69 span_bug
!(terminator
.source_info
.span
, "replace in AddMovesForPackedDrops");
78 fn add_move_for_packed_drop
<'tcx
>(
81 patch
: &mut MirPatch
<'tcx
>,
82 terminator
: &Terminator
<'tcx
>,
86 debug
!("add_move_for_packed_drop({:?} @ {:?})", terminator
, loc
);
87 let TerminatorKind
::Drop { ref place, target, unwind }
= terminator
.kind
else {
91 let source_info
= terminator
.source_info
;
92 let ty
= place
.ty(body
, tcx
).ty
;
93 let temp
= patch
.new_temp(ty
, terminator
.source_info
.span
);
95 let storage_dead_block
= patch
.new_block(BasicBlockData
{
96 statements
: vec
![Statement { source_info, kind: StatementKind::StorageDead(temp) }
],
97 terminator
: Some(Terminator { source_info, kind: TerminatorKind::Goto { target }
}),
101 patch
.add_statement(loc
, StatementKind
::StorageLive(temp
));
102 patch
.add_assign(loc
, Place
::from(temp
), Rvalue
::Use(Operand
::Move(*place
)));
103 patch
.patch_terminator(
105 TerminatorKind
::Drop { place: Place::from(temp), target: storage_dead_block, unwind }
,