]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/stacked_borrows/interior_mut2.rs
Rollup merge of #105427 - GuillaumeGomez:dont-silently-ignore-rustdoc-errors, r=notriddle
[rust.git] / src / tools / miri / tests / fail / stacked_borrows / interior_mut2.rs
1 use std::cell::UnsafeCell;
2 use std::mem;
3
4 // Like `&mut *x.get()`, but without intermediate raw pointers.
5 #[allow(mutable_transmutes)]
6 unsafe fn unsafe_cell_get<T>(x: &UnsafeCell<T>) -> &'static mut T {
7     mem::transmute(x)
8 }
9
10 fn main() {
11     unsafe {
12         let c = &UnsafeCell::new(UnsafeCell::new(0));
13         let inner_uniq = &mut *c.get();
14         let inner_shr = &*inner_uniq;
15         // stack: [c: SharedReadWrite, inner_uniq: Unique, inner_shr: SharedReadWrite]
16
17         let _val = c.get().read(); // invalidates inner_uniq
18         // stack: [c: SharedReadWrite, inner_uniq: Disabled, inner_shr: SharedReadWrite]
19
20         // We have to be careful not to add any raw pointers above inner_uniq in
21         // the stack, hence the use of unsafe_cell_get.
22         let _val = *unsafe_cell_get(inner_shr); // this still works
23
24         *c.get() = UnsafeCell::new(0); // now inner_shr gets invalidated
25         // stack: [c: SharedReadWrite]
26
27         // now this does not work any more
28         let _val = *inner_shr.get(); //~ ERROR: /retag .* tag does not exist in the borrow stack/
29     }
30 }