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
main()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 } }
21