Skip to content

Commit e1ed855

Browse files
committed
more tests -- also one showing why we are not done yet
1 parent 0a31318 commit e1ed855

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// We want to test that granting a SharedReadWrite will be added
2+
// *below* an already granted Unique -- so writing to
3+
// the SharedReadWrite will invalidate the Unique.
4+
5+
use std::mem;
6+
use std::cell::Cell;
7+
8+
fn main() { unsafe {
9+
let x = &mut Cell::new(0);
10+
let y: &mut Cell<i32> = mem::transmute(&mut *x); // launder lifetime
11+
let shr_rw = &*x; // thanks to interior mutability this will be a SharedReadWrite
12+
shr_rw.set(1);
13+
y.get_mut(); //~ ERROR borrow stack
14+
} }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// We want to test that granting a SharedReadWrite will be added
2+
// *below* an already granted SharedReadWrite -- so writing to
3+
// the SharedReadWrite will invalidate the SharedReadWrite.
4+
5+
use std::mem;
6+
use std::cell::RefCell;
7+
8+
fn main() { unsafe {
9+
let x = &mut RefCell::new(0);
10+
let y: &i32 = mem::transmute(&*x.borrow()); // launder lifetime
11+
let shr_rw = &*x; // thanks to interior mutability this will be a SharedReadWrite
12+
shr_rw.replace(1);
13+
let _val = *y; //~ ERROR borrow stack
14+
} }

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

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fn main() {
1111
drop_after_sharing();
1212
direct_mut_to_const_raw();
1313
two_raw();
14+
shr_and_raw();
1415
}
1516

1617
// Deref a raw ptr to access a field of a large struct, where the field
@@ -136,3 +137,15 @@ fn two_raw() { unsafe {
136137
*y1 += 2;
137138
*y2 += 1;
138139
} }
140+
141+
// Make sure that creating a *mut does not invalidate existing shared references.
142+
fn shr_and_raw() { /* unsafe {
143+
use std::mem;
144+
// FIXME: This is currently disabled because "as *mut _" incurs a reborrow.
145+
let x = &mut 0;
146+
let y1: &i32 = mem::transmute(&*x); // launder lifetimes
147+
let y2 = x as *mut _;
148+
let _val = *y1;
149+
*y2 += 1;
150+
// TODO: Once this works, add compile-fail test that tries to read from y1 again.
151+
} */ }

0 commit comments

Comments
 (0)