File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
tests/compile-fail/stacked_borrows Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ #![ feature( untagged_unions) ]
2+ // A callee may not read the destination of our `&mut` without
3+ // us noticing.
4+ // Thise code got carefully checked to not introduce any reborrows
5+ // that are not explicit in the source. Let's hope the compiler does not break this later!
6+
7+ use std:: mem;
8+
9+ fn main ( ) {
10+ let mut x: i32 = 15 ;
11+ let xref1 = & mut x;
12+ let xref1_sneaky: usize = unsafe { mem:: transmute_copy ( & xref1) } ;
13+ let xref2 = & mut * xref1; // derived from xref1, so using raw is still okay...
14+ callee ( xref1_sneaky) ;
15+ let _val = * xref2; // ...but any use of it will invalidate our ref.
16+ //~^ ERROR: does not exist on the stack
17+ }
18+
19+ fn callee ( xref1 : usize ) {
20+ // Transmuting through a union to avoid retagging
21+ union UsizeToRef {
22+ from : usize ,
23+ to : & ' static mut i32 ,
24+ }
25+ let xref1 = UsizeToRef { from : xref1 } ;
26+ // Doing the deref and the transmute (through the union) in the same place expression
27+ // should avoid retagging.
28+ let _val = unsafe { * xref1. to } ;
29+ }
You can’t perform that action at this time.
0 commit comments