rust/tests/fail/stacked_borrows/interior_mut1.rs
2022-06-20 23:50:40 -07:00

18 lines
531 B
Rust

use std::cell::UnsafeCell;
fn main() {
unsafe {
let c = &UnsafeCell::new(UnsafeCell::new(0));
let inner_uniq = &mut *c.get();
// stack: [c: SharedReadWrite, inner_uniq: Unique]
let inner_shr = &*inner_uniq; // adds a SharedReadWrite
// stack: [c: SharedReadWrite, inner_uniq: Unique, inner_shr: SharedReadWrite]
*c.get() = UnsafeCell::new(1); // invalidates inner_shr
// stack: [c: SharedReadWrite]
let _val = *inner_shr.get(); //~ ERROR borrow stack
}
}