From 287ffb8bba8d137c9dc04ade49c0bc65e0ab07f7 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 19 Apr 2019 08:46:09 +0200 Subject: [PATCH] test another version of 'creating a shared ref must not leak the Unique' --- .../stacked_borrows/illegal_read7.rs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/compile-fail/stacked_borrows/illegal_read7.rs diff --git a/tests/compile-fail/stacked_borrows/illegal_read7.rs b/tests/compile-fail/stacked_borrows/illegal_read7.rs new file mode 100644 index 00000000000..25d0878c045 --- /dev/null +++ b/tests/compile-fail/stacked_borrows/illegal_read7.rs @@ -0,0 +1,20 @@ +// Creating a shared reference does not leak the data to raw pointers, +// not even when interior mutability is involved. + +use std::cell::Cell; +use std::ptr; + +fn main() { unsafe { + let x = &mut Cell::new(0); + let raw = x as *mut Cell; + let x = &mut *raw; + let _shr = &*x; + // The state here is interesting because the top of the stack is [Unique, SharedReadWrite], + // just like if we had done `x as *mut _`. + // If we said that reading from a lower item is fine if the top item is `SharedReadWrite` + // (one way to maybe preserve a stack discipline), then we could now read from `raw` + // without invalidating `x`. That would be bad! It would mean that creating `shr` + // leaked `x` to `raw`. + let _val = ptr::read(raw); + let _val = *x.get_mut(); //~ ERROR borrow stack +} }