1 #![feature(maybe_uninit, maybe_uninit_ref)]
2 use std::mem::MaybeUninit;
4 use std::cell::RefCell;
7 aliasing_mut_and_shr();
8 aliasing_frz_and_shr();
9 into_interior_mutability();
12 fn aliasing_mut_and_shr() {
13 fn inner(rc: &RefCell<i32>, aliasing: &mut i32) {
15 let _escape_to_raw = rc as *const _;
19 // also turning this into a frozen ref now must work
20 let aliasing = &*aliasing;
22 let _escape_to_raw = rc as *const _; // this must NOT unfreeze
24 let _shr = &*rc; // this must NOT unfreeze
28 let rc = RefCell::new(23);
29 let mut bmut = rc.borrow_mut();
30 inner(&rc, &mut *bmut);
32 assert_eq!(*rc.borrow(), 23+12);
35 fn aliasing_frz_and_shr() {
36 fn inner(rc: &RefCell<i32>, aliasing: &i32) {
38 let _escape_to_raw = rc as *const _; // this must NOT unfreeze
40 let _shr = &*rc; // this must NOT unfreeze
44 let rc = RefCell::new(23);
45 let bshr = rc.borrow();
47 assert_eq!(*rc.borrow(), 23);
50 // Getting a pointer into a union with interior mutability used to be tricky
51 // business (https://github.com/rust-lang/miri/issues/615), but it should work
53 fn into_interior_mutability() {
54 let mut x: MaybeUninit<(Cell<u32>, u32)> = MaybeUninit::uninit();
56 x.write((Cell::new(0), 1));
57 let ptr = unsafe { x.get_ref() };