rust/tests/compile-fail/stacked_borrows/illegal_read3.rs

32 lines
1.0 KiB
Rust
Raw Normal View History

2019-02-15 19:43:56 -06:00
// A callee may not read the destination of our `&mut` without us noticing.
2018-11-07 10:33:41 -06:00
// Thise code got carefully checked to not introduce any reborrows
2019-02-15 19:43:56 -06:00
// that are not explicit in the source. Let's hope the compiler does not break this later!
#![feature(untagged_unions)]
2018-11-07 10:33:41 -06:00
use std::mem;
fn main() {
let mut x: i32 = 15;
let xref1 = &mut x;
let xref1_sneaky: usize = unsafe { mem::transmute_copy(&xref1) };
2019-02-15 19:43:56 -06:00
// Derived from `xref1`, so using raw value is still ok, ...
let xref2 = &mut *xref1;
2018-11-07 10:33:41 -06:00
callee(xref1_sneaky);
2019-02-15 19:43:56 -06:00
// ... though any use of it will invalidate our ref.
let _val = *xref2;
//~^ ERROR: does not exist on the borrow stack
2018-11-07 10:33:41 -06:00
}
fn callee(xref1: usize) {
2019-02-15 19:43:56 -06:00
// Transmuting through a union to avoid retagging.
2018-11-07 10:33:41 -06:00
union UsizeToRef {
from: usize,
to: &'static mut i32,
}
let xref1 = UsizeToRef { from: xref1 };
// Doing the deref and the transmute (through the union) in the same place expression
// should avoid retagging.
let _val = unsafe { *xref1.to };
}