File tree 3 files changed +41
-0
lines changed
compile-fail/stacked_borrows
3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ } }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ fn main() {
11
11
drop_after_sharing ( ) ;
12
12
direct_mut_to_const_raw ( ) ;
13
13
two_raw ( ) ;
14
+ shr_and_raw ( ) ;
14
15
}
15
16
16
17
// Deref a raw ptr to access a field of a large struct, where the field
@@ -136,3 +137,15 @@ fn two_raw() { unsafe {
136
137
* y1 += 2 ;
137
138
* y2 += 1 ;
138
139
} }
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
+ } */ }
You can’t perform that action at this time.
0 commit comments