Skip to content

Commit 94e7512

Browse files
committed
add another mean test case
1 parent 020313d commit 94e7512

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![feature(untagged_unions)]
2+
// A callee may not read the destination of our `&mut` without
3+
// us noticing.
4+
// Thise code got carefully checked to not introduce any reborrows
5+
// that are not explicit in the source. Let's hope the compiler does not break this later!
6+
7+
use std::mem;
8+
9+
fn main() {
10+
let mut x: i32 = 15;
11+
let xref1 = &mut x;
12+
let xref1_sneaky: usize = unsafe { mem::transmute_copy(&xref1) };
13+
let xref2 = &mut *xref1; // derived from xref1, so using raw is still okay...
14+
callee(xref1_sneaky);
15+
let _val = *xref2; // ...but any use of it will invalidate our ref.
16+
//~^ ERROR: does not exist on the stack
17+
}
18+
19+
fn callee(xref1: usize) {
20+
// Transmuting through a union to avoid retagging
21+
union UsizeToRef {
22+
from: usize,
23+
to: &'static mut i32,
24+
}
25+
let xref1 = UsizeToRef { from: xref1 };
26+
// Doing the deref and the transmute (through the union) in the same place expression
27+
// should avoid retagging.
28+
let _val = unsafe { *xref1.to };
29+
}

0 commit comments

Comments
 (0)