rust/tests/fail/stacked_borrows/interior_mut2.rs
Ben Kimock 14e72e7ffa Improve information sharing across SB diagnostics
Previous Stacked Borrows diagnostics were missing a lot of information
about the state of the interpreter, and it was difficult to add
additional state because it was threaded through all the intervening
function signatures.

This change factors a lot of the arguments which used to be passed
individually to many stacked borrows functions into a single
`DiagnosticCx`, which is built in `Stacks::for_each`, and since it
wraps a handle to `AllocHistory`, we can now handle more nuanced
things like heterogeneous borrow of `!Freeze` types.
2022-08-18 15:01:03 -04:00

31 lines
1.1 KiB
Rust

use std::cell::UnsafeCell;
use std::mem;
// Like `&mut *x.get()`, but without intermediate raw pointers.
#[allow(mutable_transmutes)]
unsafe fn unsafe_cell_get<T>(x: &UnsafeCell<T>) -> &'static mut T {
mem::transmute(x)
}
fn main() {
unsafe {
let c = &UnsafeCell::new(UnsafeCell::new(0));
let inner_uniq = &mut *c.get();
let inner_shr = &*inner_uniq;
// stack: [c: SharedReadWrite, inner_uniq: Unique, inner_shr: SharedReadWrite]
let _val = c.get().read(); // invalidates inner_uniq
// stack: [c: SharedReadWrite, inner_uniq: Disabled, inner_shr: SharedReadWrite]
// We have to be careful not to add any raw pointers above inner_uniq in
// the stack, hence the use of unsafe_cell_get.
let _val = *unsafe_cell_get(inner_shr); // this still works
*c.get() = UnsafeCell::new(0); // now inner_shr gets invalidated
// stack: [c: SharedReadWrite]
// now this does not work any more
let _val = *inner_shr.get(); //~ ERROR: /retag .* tag does not exist in the borrow stack/
}
}