2018-10-29 13:48:43 -05:00
|
|
|
// A callee may not read the destination of our `&mut` without
|
|
|
|
// us noticing.
|
|
|
|
|
2022-04-30 13:07:36 -05:00
|
|
|
#[rustfmt::skip] // rustfmt bug: https://github.com/rust-lang/rustfmt/issues/5391
|
2018-10-29 13:48:43 -05:00
|
|
|
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...
|
2018-10-29 13:48:43 -05:00
|
|
|
callee(xraw);
|
|
|
|
let _val = *xref; // ...but any use of raw will invalidate our ref.
|
2019-04-16 10:17:28 -05:00
|
|
|
//~^ ERROR: borrow stack
|
2018-10-29 13:48:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|