1
Fork 0

Make slice drop shims use AddressOf

This commit is contained in:
Matthew Jasper 2019-04-20 18:06:10 +01:00
parent 35919ace70
commit 5fb797ca75
2 changed files with 32 additions and 47 deletions

View file

@ -557,10 +557,10 @@ where
/// if can_go then succ else drop-block
/// drop-block:
/// if ptr_based {
/// ptr = &mut *cur
/// ptr = cur
/// cur = cur.offset(1)
/// } else {
/// ptr = &mut P[cur]
/// ptr = &raw mut P[cur]
/// cur = cur + 1
/// }
/// drop(ptr)
@ -574,34 +574,28 @@ where
unwind: Unwind,
ptr_based: bool,
) -> BasicBlock {
let copy = |place: &Place<'tcx>| Operand::Copy(place.clone());
let move_ = |place: &Place<'tcx>| Operand::Move(place.clone());
let copy = |place: Place<'tcx>| Operand::Copy(place);
let move_ = |place: Place<'tcx>| Operand::Move(place);
let tcx = self.tcx();
let ref_ty = tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut {
let ptr_ty = tcx.mk_ptr(ty::TypeAndMut {
ty: ety,
mutbl: hir::Mutability::Mutable
});
let ptr = &Place::from(self.new_temp(ref_ty));
let can_go = &Place::from(self.new_temp(tcx.types.bool));
let ptr = &Place::from(self.new_temp(ptr_ty));
let can_go = Place::from(self.new_temp(tcx.types.bool));
let one = self.constant_usize(1);
let (ptr_next, cur_next) = if ptr_based {
(Rvalue::Ref(
tcx.lifetimes.re_erased,
BorrowKind::Mut { allow_two_phase_borrow: false },
Place {
base: PlaceBase::Local(cur),
projection: tcx.intern_place_elems(&vec![ProjectionElem::Deref]),
}
),
Rvalue::BinaryOp(BinOp::Offset, move_(&Place::from(cur)), one))
(
Rvalue::Use(copy(cur.into())),
Rvalue::BinaryOp(BinOp::Offset, move_(cur.into()), one),
)
} else {
(Rvalue::Ref(
tcx.lifetimes.re_erased,
BorrowKind::Mut { allow_two_phase_borrow: false },
tcx.mk_place_index(self.place.clone(), cur)),
Rvalue::BinaryOp(BinOp::Add, move_(&Place::from(cur)), one))
(
Rvalue::AddressOf(Mutability::Mut, tcx.mk_place_index(self.place.clone(), cur)),
Rvalue::BinaryOp(BinOp::Add, move_(cur.into()), one),
)
};
let drop_block = BasicBlockData {
@ -620,9 +614,9 @@ where
let loop_block = BasicBlockData {
statements: vec![
self.assign(can_go, Rvalue::BinaryOp(BinOp::Eq,
copy(&Place::from(cur)),
copy(length_or_end)))
self.assign(&can_go, Rvalue::BinaryOp(BinOp::Eq,
copy(Place::from(cur)),
copy(length_or_end.clone())))
],
is_cleanup: unwind.is_cleanup(),
terminator: Some(Terminator {
@ -725,8 +719,6 @@ where
let cur = self.new_temp(iter_ty);
let length_or_end = if ptr_based {
// FIXME check if we want to make it return a `Place` directly
// if all use sites want a `Place::Base` anyway.
Place::from(self.new_temp(iter_ty))
} else {
length.clone()
@ -753,23 +745,16 @@ where
let drop_block_stmts = if ptr_based {
let tmp_ty = tcx.mk_mut_ptr(self.place_ty(self.place));
let tmp = Place::from(self.new_temp(tmp_ty));
// tmp = &mut P;
// tmp = &raw mut P;
// cur = tmp as *mut T;
// end = Offset(cur, len);
vec![
self.assign(&tmp, Rvalue::Ref(
tcx.lifetimes.re_erased,
BorrowKind::Mut { allow_two_phase_borrow: false },
self.place.clone()
)),
self.assign(
&cur,
Rvalue::Cast(CastKind::Misc, Operand::Move(tmp), iter_ty),
),
self.assign(&tmp, Rvalue::AddressOf(Mutability::Mut, self.place.clone())),
self.assign(&cur, Rvalue::Cast(CastKind::Misc, Operand::Move(tmp), iter_ty)),
self.assign(
&length_or_end,
Rvalue::BinaryOp(BinOp::Offset, Operand::Copy(cur), Operand::Move(length)
)),
Rvalue::BinaryOp(BinOp::Offset, Operand::Copy(cur), Operand::Move(length)),
),
]
} else {
// cur = 0 (length already pushed)

View file

@ -10,15 +10,15 @@ fn main() {
// let mut _2: usize;
// let mut _3: usize;
// let mut _4: usize;
// let mut _5: &mut std::string::String;
// let mut _5: *mut std::string::String;
// let mut _6: bool;
// let mut _7: &mut std::string::String;
// let mut _7: *mut std::string::String;
// let mut _8: bool;
// let mut _9: *mut std::string::String;
// let mut _10: *mut std::string::String;
// let mut _11: &mut std::string::String;
// let mut _11: *mut std::string::String;
// let mut _12: bool;
// let mut _13: &mut std::string::String;
// let mut _13: *mut std::string::String;
// let mut _14: bool;
// let mut _15: *mut [std::string::String];
// bb0: {
@ -31,7 +31,7 @@ fn main() {
// resume;
// }
// bb3 (cleanup): {
// _5 = &mut (*_1)[_4];
// _5 = &raw mut (*_1)[_4];
// _4 = Add(move _4, const 1usize);
// drop((*_5)) -> bb4;
// }
@ -40,7 +40,7 @@ fn main() {
// switchInt(move _6) -> [false: bb3, otherwise: bb2];
// }
// bb5: {
// _7 = &mut (*_1)[_4];
// _7 = &raw mut (*_1)[_4];
// _4 = Add(move _4, const 1usize);
// drop((*_7)) -> [return: bb6, unwind: bb4];
// }
@ -56,7 +56,7 @@ fn main() {
// goto -> bb7;
// }
// bb9 (cleanup): {
// _11 = &mut (*_9);
// _11 = _9;
// _9 = Offset(move _9, const 1usize);
// drop((*_11)) -> bb10;
// }
@ -65,7 +65,7 @@ fn main() {
// switchInt(move _12) -> [false: bb9, otherwise: bb2];
// }
// bb11: {
// _13 = &mut (*_9);
// _13 = _9;
// _9 = Offset(move _9, const 1usize);
// drop((*_13)) -> [return: bb12, unwind: bb10];
// }
@ -74,7 +74,7 @@ fn main() {
// switchInt(move _14) -> [false: bb11, otherwise: bb1];
// }
// bb13: {
// _15 = &mut (*_1);
// _15 = &raw mut (*_1);
// _9 = move _15 as *mut std::string::String (Misc);
// _10 = Offset(_9, move _3);
// goto -> bb12;