File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
tests/compile-fail/stacked_borrows Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Creating a shared reference does not leak the data to raw pointers,
2
+ // not even when interior mutability is involved.
3
+
4
+ use std:: cell:: Cell ;
5
+ use std:: ptr;
6
+
7
+ fn main ( ) { unsafe {
8
+ let x = & mut Cell :: new ( 0 ) ;
9
+ let raw = x as * mut Cell < i32 > ;
10
+ let x = & mut * raw;
11
+ let _shr = & * x;
12
+ // The state here is interesting because the top of the stack is [Unique, SharedReadWrite],
13
+ // just like if we had done `x as *mut _`.
14
+ // If we said that reading from a lower item is fine if the top item is `SharedReadWrite`
15
+ // (one way to maybe preserve a stack discipline), then we could now read from `raw`
16
+ // without invalidating `x`. That would be bad! It would mean that creating `shr`
17
+ // leaked `x` to `raw`.
18
+ let _val = ptr:: read ( raw) ;
19
+ let _val = * x. get_mut ( ) ; //~ ERROR borrow stack
20
+ } }
You can’t perform that action at this time.
0 commit comments