]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/stacked_borrows/illegal_read7.rs
Merge commit '4bdfb0741dbcecd5279a2635c3280726db0604b5' into clippyup
[rust.git] / src / tools / miri / tests / fail / stacked_borrows / illegal_read7.rs
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() {
8     unsafe {
9         let x = &mut Cell::new(0);
10         let raw = x as *mut Cell<i32>;
11         let x = &mut *raw;
12         let _shr = &*x;
13         // The state here is interesting because the top of the stack is [Unique, SharedReadWrite],
14         // just like if we had done `x as *mut _`.
15         // If we said that reading from a lower item is fine if the top item is `SharedReadWrite`
16         // (one way to maybe preserve a stack discipline), then we could now read from `raw`
17         // without invalidating `x`.  That would be bad!  It would mean that creating `shr`
18         // leaked `x` to `raw`.
19         let _val = ptr::read(raw);
20         let _val = *x.get_mut(); //~ ERROR: /retag .* tag does not exist in the borrow stack/
21     }
22 }