Skip to content

Commit 287ffb8

Browse files
committed
test another version of 'creating a shared ref must not leak the Unique'
1 parent 0a5e541 commit 287ffb8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Creating a shared reference does not leak the data to raw pointers,
2+
// not even when interior mutability is involved.
3+
4+
use std::cell::Cell;
5+
use std::ptr;
6+
7+
fn main() { unsafe {
8+
let x = &mut Cell::new(0);
9+
let raw = x as *mut Cell<i32>;
10+
let x = &mut *raw;
11+
let _shr = &*x;
12+
// The state here is interesting because the top of the stack is [Unique, SharedReadWrite],
13+
// just like if we had done `x as *mut _`.
14+
// If we said that reading from a lower item is fine if the top item is `SharedReadWrite`
15+
// (one way to maybe preserve a stack discipline), then we could now read from `raw`
16+
// without invalidating `x`. That would be bad! It would mean that creating `shr`
17+
// leaked `x` to `raw`.
18+
let _val = ptr::read(raw);
19+
let _val = *x.get_mut(); //~ ERROR borrow stack
20+
} }

0 commit comments

Comments
 (0)