]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/stacked_borrows/illegal_read3.rs
b4abbb4a1aedf8005817f285b2441823bad4c02d
[rust.git] / tests / compile-fail / stacked_borrows / illegal_read3.rs
1 // A callee may not read the destination of our `&mut` without us noticing.
2 // Thise code got carefully checked to not introduce any reborrows
3 // that are not explicit in the source. Let's hope the compiler does not break this later!
4
5 #![feature(untagged_unions)]
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     // Derived from `xref1`, so using raw value is still ok, ...
14     let xref2 = &mut *xref1;
15     callee(xref1_sneaky);
16     // ... though any use of it will invalidate our ref.
17     let _val = *xref2;
18     //~^ ERROR: does not exist on the borrow stack
19 }
20
21 fn callee(xref1: usize) {
22     // Transmuting through a union to avoid retagging.
23     union UsizeToRef {
24         from: usize,
25         to: &'static mut i32,
26     }
27     let xref1 = UsizeToRef { from: xref1 };
28     // Doing the deref and the transmute (through the union) in the same place expression
29     // should avoid retagging.
30     let _val = unsafe { *xref1.to };
31 }