rust/tests/fail/stacked_borrows/illegal_read2.rs

20 lines
693 B
Rust
Raw Normal View History

// A callee may not read the destination of our `&mut` without
// us noticing.
#[rustfmt::skip] // rustfmt bug: https://github.com/rust-lang/rustfmt/issues/5391
fn main() {
let mut x = 15;
let xraw = &mut x as *mut _;
2019-02-15 19:43:56 -06:00
let xref = unsafe { &mut *xraw }; // derived from raw, so using raw is still ok...
callee(xraw);
let _val = *xref; // ...but any use of raw will invalidate our ref.
2022-07-13 17:59:33 -05:00
//~^ ERROR: /read access .* tag does not exist in the borrow stack/
}
fn callee(xraw: *mut i32) {
// We are a bit sneaky: We first create a shared ref, exploiting the reborrowing rules,
// and then we read through that.
let shr = unsafe { &*xraw };
let _val = *shr;
}