Skip to content

Commit 5fb797c

Browse files
committed
Make slice drop shims use AddressOf
1 parent 35919ac commit 5fb797c

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

src/librustc_mir/util/elaborate_drops.rs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,10 @@ where
557557
/// if can_go then succ else drop-block
558558
/// drop-block:
559559
/// if ptr_based {
560-
/// ptr = &mut *cur
560+
/// ptr = cur
561561
/// cur = cur.offset(1)
562562
/// } else {
563-
/// ptr = &mut P[cur]
563+
/// ptr = &raw mut P[cur]
564564
/// cur = cur + 1
565565
/// }
566566
/// drop(ptr)
@@ -574,34 +574,28 @@ where
574574
unwind: Unwind,
575575
ptr_based: bool,
576576
) -> BasicBlock {
577-
let copy = |place: &Place<'tcx>| Operand::Copy(place.clone());
578-
let move_ = |place: &Place<'tcx>| Operand::Move(place.clone());
577+
let copy = |place: Place<'tcx>| Operand::Copy(place);
578+
let move_ = |place: Place<'tcx>| Operand::Move(place);
579579
let tcx = self.tcx();
580580

581-
let ref_ty = tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut {
581+
let ptr_ty = tcx.mk_ptr(ty::TypeAndMut {
582582
ty: ety,
583583
mutbl: hir::Mutability::Mutable
584584
});
585-
let ptr = &Place::from(self.new_temp(ref_ty));
586-
let can_go = &Place::from(self.new_temp(tcx.types.bool));
585+
let ptr = &Place::from(self.new_temp(ptr_ty));
586+
let can_go = Place::from(self.new_temp(tcx.types.bool));
587587

588588
let one = self.constant_usize(1);
589589
let (ptr_next, cur_next) = if ptr_based {
590-
(Rvalue::Ref(
591-
tcx.lifetimes.re_erased,
592-
BorrowKind::Mut { allow_two_phase_borrow: false },
593-
Place {
594-
base: PlaceBase::Local(cur),
595-
projection: tcx.intern_place_elems(&vec![ProjectionElem::Deref]),
596-
}
597-
),
598-
Rvalue::BinaryOp(BinOp::Offset, move_(&Place::from(cur)), one))
590+
(
591+
Rvalue::Use(copy(cur.into())),
592+
Rvalue::BinaryOp(BinOp::Offset, move_(cur.into()), one),
593+
)
599594
} else {
600-
(Rvalue::Ref(
601-
tcx.lifetimes.re_erased,
602-
BorrowKind::Mut { allow_two_phase_borrow: false },
603-
tcx.mk_place_index(self.place.clone(), cur)),
604-
Rvalue::BinaryOp(BinOp::Add, move_(&Place::from(cur)), one))
595+
(
596+
Rvalue::AddressOf(Mutability::Mut, tcx.mk_place_index(self.place.clone(), cur)),
597+
Rvalue::BinaryOp(BinOp::Add, move_(cur.into()), one),
598+
)
605599
};
606600

607601
let drop_block = BasicBlockData {
@@ -620,9 +614,9 @@ where
620614

621615
let loop_block = BasicBlockData {
622616
statements: vec![
623-
self.assign(can_go, Rvalue::BinaryOp(BinOp::Eq,
624-
copy(&Place::from(cur)),
625-
copy(length_or_end)))
617+
self.assign(&can_go, Rvalue::BinaryOp(BinOp::Eq,
618+
copy(Place::from(cur)),
619+
copy(length_or_end.clone())))
626620
],
627621
is_cleanup: unwind.is_cleanup(),
628622
terminator: Some(Terminator {
@@ -725,8 +719,6 @@ where
725719

726720
let cur = self.new_temp(iter_ty);
727721
let length_or_end = if ptr_based {
728-
// FIXME check if we want to make it return a `Place` directly
729-
// if all use sites want a `Place::Base` anyway.
730722
Place::from(self.new_temp(iter_ty))
731723
} else {
732724
length.clone()
@@ -753,23 +745,16 @@ where
753745
let drop_block_stmts = if ptr_based {
754746
let tmp_ty = tcx.mk_mut_ptr(self.place_ty(self.place));
755747
let tmp = Place::from(self.new_temp(tmp_ty));
756-
// tmp = &mut P;
748+
// tmp = &raw mut P;
757749
// cur = tmp as *mut T;
758750
// end = Offset(cur, len);
759751
vec![
760-
self.assign(&tmp, Rvalue::Ref(
761-
tcx.lifetimes.re_erased,
762-
BorrowKind::Mut { allow_two_phase_borrow: false },
763-
self.place.clone()
764-
)),
765-
self.assign(
766-
&cur,
767-
Rvalue::Cast(CastKind::Misc, Operand::Move(tmp), iter_ty),
768-
),
752+
self.assign(&tmp, Rvalue::AddressOf(Mutability::Mut, self.place.clone())),
753+
self.assign(&cur, Rvalue::Cast(CastKind::Misc, Operand::Move(tmp), iter_ty)),
769754
self.assign(
770755
&length_or_end,
771-
Rvalue::BinaryOp(BinOp::Offset, Operand::Copy(cur), Operand::Move(length)
772-
)),
756+
Rvalue::BinaryOp(BinOp::Offset, Operand::Copy(cur), Operand::Move(length)),
757+
),
773758
]
774759
} else {
775760
// cur = 0 (length already pushed)

src/test/mir-opt/slice-drop-shim.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ fn main() {
1010
// let mut _2: usize;
1111
// let mut _3: usize;
1212
// let mut _4: usize;
13-
// let mut _5: &mut std::string::String;
13+
// let mut _5: *mut std::string::String;
1414
// let mut _6: bool;
15-
// let mut _7: &mut std::string::String;
15+
// let mut _7: *mut std::string::String;
1616
// let mut _8: bool;
1717
// let mut _9: *mut std::string::String;
1818
// let mut _10: *mut std::string::String;
19-
// let mut _11: &mut std::string::String;
19+
// let mut _11: *mut std::string::String;
2020
// let mut _12: bool;
21-
// let mut _13: &mut std::string::String;
21+
// let mut _13: *mut std::string::String;
2222
// let mut _14: bool;
2323
// let mut _15: *mut [std::string::String];
2424
// bb0: {
@@ -31,7 +31,7 @@ fn main() {
3131
// resume;
3232
// }
3333
// bb3 (cleanup): {
34-
// _5 = &mut (*_1)[_4];
34+
// _5 = &raw mut (*_1)[_4];
3535
// _4 = Add(move _4, const 1usize);
3636
// drop((*_5)) -> bb4;
3737
// }
@@ -40,7 +40,7 @@ fn main() {
4040
// switchInt(move _6) -> [false: bb3, otherwise: bb2];
4141
// }
4242
// bb5: {
43-
// _7 = &mut (*_1)[_4];
43+
// _7 = &raw mut (*_1)[_4];
4444
// _4 = Add(move _4, const 1usize);
4545
// drop((*_7)) -> [return: bb6, unwind: bb4];
4646
// }
@@ -56,7 +56,7 @@ fn main() {
5656
// goto -> bb7;
5757
// }
5858
// bb9 (cleanup): {
59-
// _11 = &mut (*_9);
59+
// _11 = _9;
6060
// _9 = Offset(move _9, const 1usize);
6161
// drop((*_11)) -> bb10;
6262
// }
@@ -65,7 +65,7 @@ fn main() {
6565
// switchInt(move _12) -> [false: bb9, otherwise: bb2];
6666
// }
6767
// bb11: {
68-
// _13 = &mut (*_9);
68+
// _13 = _9;
6969
// _9 = Offset(move _9, const 1usize);
7070
// drop((*_13)) -> [return: bb12, unwind: bb10];
7171
// }
@@ -74,7 +74,7 @@ fn main() {
7474
// switchInt(move _14) -> [false: bb11, otherwise: bb1];
7575
// }
7676
// bb13: {
77-
// _15 = &mut (*_1);
77+
// _15 = &raw mut (*_1);
7878
// _9 = move _15 as *mut std::string::String (Misc);
7979
// _10 = Offset(_9, move _3);
8080
// goto -> bb12;

0 commit comments

Comments
 (0)