]> git.lizzy.rs Git - rust.git/commitdiff
add tests for fixes: sharing no longer leaks, and we can handle entering interior...
authorRalf Jung <post@ralfj.de>
Wed, 17 Apr 2019 13:20:33 +0000 (15:20 +0200)
committerRalf Jung <post@ralfj.de>
Wed, 17 Apr 2019 14:02:57 +0000 (16:02 +0200)
tests/compile-fail/stacked_borrows/illegal_read6.rs [new file with mode: 0644]
tests/run-pass/stacked-borrows/interior_mutability.rs [new file with mode: 0644]
tests/run-pass/stacked-borrows/refcell.rs [deleted file]

diff --git a/tests/compile-fail/stacked_borrows/illegal_read6.rs b/tests/compile-fail/stacked_borrows/illegal_read6.rs
new file mode 100644 (file)
index 0000000..dc37814
--- /dev/null
@@ -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/interior_mutability.rs b/tests/run-pass/stacked-borrows/interior_mutability.rs
new file mode 100644 (file)
index 0000000..33f44d0
--- /dev/null
@@ -0,0 +1,59 @@
+#![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() {
+    fn inner(rc: &RefCell<i32>, aliasing: &mut i32) {
+        *aliasing += 4;
+        let _escape_to_raw = rc as *const _;
+        *aliasing += 4;
+        let _shr = &*rc;
+        *aliasing += 4;
+        // also turning this into a frozen ref now must work
+        let aliasing = &*aliasing;
+        let _val = *aliasing;
+        let _escape_to_raw = rc as *const _; // this must NOT unfreeze
+        let _val = *aliasing;
+        let _shr = &*rc; // this must NOT unfreeze
+        let _val = *aliasing;
+    }
+
+    let rc = RefCell::new(23);
+    let mut bmut = rc.borrow_mut();
+    inner(&rc, &mut *bmut);
+    drop(bmut);
+    assert_eq!(*rc.borrow(), 23+12);
+}
+
+fn aliasing_frz_and_shr() {
+    fn inner(rc: &RefCell<i32>, aliasing: &i32) {
+        let _val = *aliasing;
+        let _escape_to_raw = rc as *const _; // this must NOT unfreeze
+        let _val = *aliasing;
+        let _shr = &*rc; // this must NOT unfreeze
+        let _val = *aliasing;
+    }
+
+    let rc = RefCell::new(23);
+    let bshr = rc.borrow();
+    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>, u32)> = MaybeUninit::uninit();
+    x.as_ptr();
+    x.write((Cell::new(0), 1));
+    let ptr = unsafe { x.get_ref() };
+    assert_eq!(ptr.1, 1);
+}
diff --git a/tests/run-pass/stacked-borrows/refcell.rs b/tests/run-pass/stacked-borrows/refcell.rs
deleted file mode 100644 (file)
index dddc708..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-use std::cell::RefCell;
-
-fn main() {
-    aliasing_mut_and_shr();
-    aliasing_frz_and_shr();
-}
-
-fn aliasing_mut_and_shr() {
-    fn inner(rc: &RefCell<i32>, aliasing: &mut i32) {
-        *aliasing += 4;
-        let _escape_to_raw = rc as *const _;
-        *aliasing += 4;
-        let _shr = &*rc;
-        *aliasing += 4;
-        // also turning this into a frozen ref now must work
-        let aliasing = &*aliasing;
-        let _val = *aliasing;
-        let _escape_to_raw = rc as *const _; // this must NOT unfreeze
-        let _val = *aliasing;
-        let _shr = &*rc; // this must NOT unfreeze
-        let _val = *aliasing;
-    }
-
-    let rc = RefCell::new(23);
-    let mut bmut = rc.borrow_mut();
-    inner(&rc, &mut *bmut);
-    drop(bmut);
-    assert_eq!(*rc.borrow(), 23+12);
-}
-
-fn aliasing_frz_and_shr() {
-    fn inner(rc: &RefCell<i32>, aliasing: &i32) {
-        let _val = *aliasing;
-        let _escape_to_raw = rc as *const _; // this must NOT unfreeze
-        let _val = *aliasing;
-        let _shr = &*rc; // this must NOT unfreeze
-        let _val = *aliasing;
-    }
-
-    let rc = RefCell::new(23);
-    let bshr = rc.borrow();
-    inner(&rc, &*bshr);
-    assert_eq!(*rc.borrow(), 23);
-}