From 72cec0562cf3ff5a50f4318e316e188325cda54c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 17 Apr 2019 15:20:33 +0200 Subject: [PATCH] add tests for fixes: sharing no longer leaks, and we can handle entering interior mutability --- .../compile-fail/stacked_borrows/illegal_read6.rs | 8 ++++++++ .../{refcell.rs => interior_mutability.rs} | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 tests/compile-fail/stacked_borrows/illegal_read6.rs rename tests/run-pass/stacked-borrows/{refcell.rs => interior_mutability.rs} (70%) diff --git a/tests/compile-fail/stacked_borrows/illegal_read6.rs b/tests/compile-fail/stacked_borrows/illegal_read6.rs new file mode 100644 index 00000000000..dc378147290 --- /dev/null +++ b/tests/compile-fail/stacked_borrows/illegal_read6.rs @@ -0,0 +1,8 @@ +// Creating a shared reference does not leak the data to raw pointers. +fn main() { unsafe { + let x = &mut 0; + let raw = x as *mut _; + let x = &mut *x; // kill `raw` + let _y = &*x; // this should not activate `raw` again + let _val = *raw; //~ ERROR borrow stack +} } diff --git a/tests/run-pass/stacked-borrows/refcell.rs b/tests/run-pass/stacked-borrows/interior_mutability.rs similarity index 70% rename from tests/run-pass/stacked-borrows/refcell.rs rename to tests/run-pass/stacked-borrows/interior_mutability.rs index dddc7089d02..33f44d0093e 100644 --- a/tests/run-pass/stacked-borrows/refcell.rs +++ b/tests/run-pass/stacked-borrows/interior_mutability.rs @@ -1,8 +1,12 @@ +#![feature(maybe_uninit, maybe_uninit_ref)] +use std::mem::MaybeUninit; +use std::cell::Cell; use std::cell::RefCell; fn main() { aliasing_mut_and_shr(); aliasing_frz_and_shr(); + into_interior_mutability(); } fn aliasing_mut_and_shr() { @@ -42,3 +46,14 @@ fn inner(rc: &RefCell, aliasing: &i32) { inner(&rc, &*bshr); assert_eq!(*rc.borrow(), 23); } + +// Getting a pointer into a union with interior mutability used to be tricky +// business (https://github.com/rust-lang/miri/issues/615), but it should work +// now. +fn into_interior_mutability() { + let mut x: MaybeUninit<(Cell, u32)> = MaybeUninit::uninit(); + x.as_ptr(); + x.write((Cell::new(0), 1)); + let ptr = unsafe { x.get_ref() }; + assert_eq!(ptr.1, 1); +} -- 2.44.0