]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/stacked_borrows/illegal_read5.rs
0f4737f16e63d500343b78b28f15582b805ab0da
[rust.git] / tests / compile-fail / stacked_borrows / illegal_read5.rs
1 // We *can* have aliasing &RefCell<T> and &mut T, but we cannot read through the former.
2 // Else we couldn't optimize based on the assumption that `xref` below is truly unique.
3
4 use std::cell::RefCell;
5 use std::{mem, ptr};
6
7 fn main() {
8     let rc = RefCell::new(0);
9     let mut refmut = rc.borrow_mut();
10     let xref: &mut i32 = &mut *refmut;
11     let xshr = &rc; // creating this is ok
12     let _val = *xref; // we can even still use our mutable reference
13     mem::forget(unsafe { ptr::read(xshr) }); // but after reading through the shared ref
14     let _val = *xref; // the mutable one is dead and gone
15     //~^ ERROR does not exist on the borrow stack
16 }