Skip to content

Commit c359ab0

Browse files
committed
Retag argument to drop_in_place unconditionally
1 parent 102040c commit c359ab0

File tree

6 files changed

+34
-47
lines changed

6 files changed

+34
-47
lines changed

compiler/rustc_mir_transform/src/shim.rs

+29-28
Original file line numberDiff line numberDiff line change
@@ -174,35 +174,36 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
174174
let mut body =
175175
new_body(source, blocks, local_decls_for_sig(&sig, span), sig.inputs().len(), span);
176176

177-
if ty.is_some() {
178-
// The first argument (index 0), but add 1 for the return value.
179-
let mut dropee_ptr = Place::from(Local::new(1 + 0));
180-
if tcx.sess.opts.unstable_opts.mir_emit_retag {
181-
// We want to treat the function argument as if it was passed by `&mut`. As such, we
182-
// generate
183-
// ```
184-
// temp = &mut *arg;
185-
// Retag(temp, FnEntry)
186-
// ```
187-
// It's important that we do this first, before anything that depends on `dropee_ptr`
188-
// has been put into the body.
189-
let reborrow = Rvalue::Ref(
190-
tcx.lifetimes.re_erased,
191-
BorrowKind::Mut { allow_two_phase_borrow: false },
192-
tcx.mk_place_deref(dropee_ptr),
193-
);
194-
let ref_ty = reborrow.ty(body.local_decls(), tcx);
195-
dropee_ptr = body.local_decls.push(LocalDecl::new(ref_ty, span)).into();
196-
let new_statements = [
197-
StatementKind::Assign(Box::new((dropee_ptr, reborrow))),
198-
StatementKind::Retag(RetagKind::FnEntry, Box::new(dropee_ptr)),
199-
];
200-
for s in new_statements {
201-
body.basic_blocks_mut()[START_BLOCK]
202-
.statements
203-
.push(Statement { source_info, kind: s });
204-
}
177+
// The first argument (index 0), but add 1 for the return value.
178+
let mut dropee_ptr = Place::from(Local::new(1 + 0));
179+
if tcx.sess.opts.unstable_opts.mir_emit_retag {
180+
// We want to treat the function argument as if it was passed by `&mut`. As such, we
181+
// generate
182+
// ```
183+
// temp = &mut *arg;
184+
// Retag(temp, FnEntry)
185+
// ```
186+
// It's important that we do this first, before anything that depends on `dropee_ptr`
187+
// has been put into the body.
188+
let reborrow = Rvalue::Ref(
189+
tcx.lifetimes.re_erased,
190+
BorrowKind::Mut { allow_two_phase_borrow: false },
191+
tcx.mk_place_deref(dropee_ptr),
192+
);
193+
let ref_ty = reborrow.ty(body.local_decls(), tcx);
194+
dropee_ptr = body.local_decls.push(LocalDecl::new(ref_ty, span)).into();
195+
let new_statements = [
196+
StatementKind::Assign(Box::new((dropee_ptr, reborrow))),
197+
StatementKind::Retag(RetagKind::FnEntry, Box::new(dropee_ptr)),
198+
];
199+
for s in new_statements {
200+
body.basic_blocks_mut()[START_BLOCK]
201+
.statements
202+
.push(Statement { source_info, kind: s });
205203
}
204+
}
205+
206+
if ty.is_some() {
206207
let patch = {
207208
let param_env = tcx.param_env_reveal_all_normalized(def_id);
208209
let mut elaborator =

src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Drop for HasDrop {
1010
fn drop(&mut self) {
1111
unsafe {
1212
let _val = *P;
13-
//~^ ERROR: /not granting access .* because that would remove .* which is protected/
13+
//~^ ERROR: /not granting access .* because that would remove .* which is strongly protected/
1414
}
1515
}
1616
}

src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is protected because it is an argument of call ID
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
22
--> $DIR/drop_in_place_protector.rs:LL:CC
33
|
44
LL | let _val = *P;
5-
| ^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is protected because it is an argument of call ID
5+
| ^^ not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected because it is an argument of call ID
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33
44
//@error-pattern: /retag .* for Unique permission .* only grants SharedReadOnly permission/
55

6-
#[repr(transparent)]
7-
struct HasDrop;
8-
9-
impl Drop for HasDrop {
10-
fn drop(&mut self) {}
11-
}
12-
136
fn main() {
147
unsafe {
15-
let x = (0u8, HasDrop);
8+
let x = 0u8;
169
let x = core::ptr::addr_of!(x);
1710
core::ptr::drop_in_place(x.cast_mut());
1811
}

src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ help: <TAG> was created by a SharedReadOnly retag at offsets [0x0..0x1]
1515
LL | let x = core::ptr::addr_of!(x);
1616
| ^^^^^^^^^^^^^^^^^^^^^^
1717
= note: BACKTRACE:
18-
= note: inside `std::ptr::drop_in_place::<(u8, HasDrop)> - shim(Some((u8, HasDrop)))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC
18+
= note: inside `std::ptr::drop_in_place::<u8> - shim(None)` at RUSTLIB/core/src/ptr/mod.rs:LL:CC
1919
note: inside `main` at $DIR/drop_in_place_retag.rs:LL:CC
2020
--> $DIR/drop_in_place_retag.rs:LL:CC
2121
|

src/tools/miri/tests/pass/drop_in_place_null.rs

-7
This file was deleted.

0 commit comments

Comments
 (0)