Skip to content

Commit 858077e

Browse files
authored
test and support two-phase reborrows of raw pointers (#727)
test and support two-phase reborrows of raw pointers
2 parents 77737cd + 9c161b8 commit 858077e

File tree

7 files changed

+148
-173
lines changed

7 files changed

+148
-173
lines changed

src/stacked_borrows.rs

Lines changed: 120 additions & 157 deletions
Large diffs are not rendered by default.

test-cargo-miri/test.stdout.ref

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ test test::rng ... ok
55
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
66

77

8-
running 3 tests
8+
running 2 tests
99
test entropy_rng ... ok
10-
test fixed_rng ... ok
1110
test simple ... ok
1211

13-
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
12+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
1413

test-cargo-miri/test.stdout.ref2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
77
running 1 test
88
test simple ... ok
99

10-
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out
10+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
1111

test-cargo-miri/tests/test.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
use rand::{SeedableRng, FromEntropy, Rng, rngs::SmallRng};
1+
use rand::{FromEntropy, Rng, rngs::SmallRng};
22

3+
// Having more than 1 test does seem to make a difference
4+
// (i.e., this calls ptr::swap which having just one test does not).
35
#[test]
46
fn simple() {
57
assert_eq!(4, 4);
68
}
79

810
// Having more than 1 test does seem to make a difference
911
// (i.e., this calls ptr::swap which having just one test does not).
10-
#[test]
11-
fn fixed_rng() {
12-
let mut rng = rand::rngs::StdRng::seed_from_u64(0xdeadcafe);
13-
let x: u32 = rng.gen();
14-
let y: u32 = rng.gen();
15-
assert_ne!(x, y);
16-
}
17-
1812
#[test]
1913
fn entropy_rng() {
2014
// Use this opportunity to test querying the RNG (needs an external crate, hence tested here and not in the compiletest suite)

tests/compile-fail/stacked_borrows/deallocate_against_barrier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern: deallocating with active protect
1+
// error-pattern: deallocating while item is protected
22

33
fn inner(x: &mut i32, f: fn(&mut i32)) {
44
// `f` may mutate, but it may not deallocate!
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::cell::UnsafeCell;
2+
3+
fn main() { unsafe {
4+
let c = &UnsafeCell::new(UnsafeCell::new(0));
5+
let inner_uniq = &mut *c.get();
6+
let inner_shr = &*inner_uniq; // a SharedRW with a tag
7+
*c.get() = UnsafeCell::new(1); // invalidates the SharedRW
8+
let _val = *inner_shr.get(); //~ ERROR borrow stack
9+
let _val = *inner_uniq.get();
10+
} }

tests/run-pass/stacked-borrows/interior_mutability.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#![feature(maybe_uninit, maybe_uninit_ref)]
22
use std::mem::MaybeUninit;
3-
use std::cell::Cell;
4-
use std::cell::RefCell;
3+
use std::cell::{Cell, RefCell, UnsafeCell};
54

65
fn main() {
76
aliasing_mut_and_shr();
87
aliasing_frz_and_shr();
98
into_interior_mutability();
9+
unsafe_cell_2phase();
1010
}
1111

1212
fn aliasing_mut_and_shr() {
@@ -57,3 +57,12 @@ fn into_interior_mutability() {
5757
let ptr = unsafe { x.get_ref() };
5858
assert_eq!(ptr.1, 1);
5959
}
60+
61+
// Two-phase borrows of the pointer returned by UnsafeCell::get() should not
62+
// invalidate aliases.
63+
fn unsafe_cell_2phase() { unsafe {
64+
let x = &UnsafeCell::new(vec![]);
65+
let x2 = &*x;
66+
(*x.get()).push(0);
67+
let _val = (*x2.get()).get(0);
68+
} }

0 commit comments

Comments
 (0)