]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/stacked_borrows/illegal_read2.rs
Rollup merge of #101664 - mejrs:similarity, r=fee1-dead
[rust.git] / src / tools / miri / tests / fail / stacked_borrows / illegal_read2.rs
1 // A callee may not read the destination of our `&mut` without
2 // us noticing.
3
4 #[rustfmt::skip] // rustfmt bug: https://github.com/rust-lang/rustfmt/issues/5391
5 fn main() {
6     let mut x = 15;
7     let xraw = &mut x as *mut _;
8     let xref = unsafe { &mut *xraw }; // derived from raw, so using raw is still ok...
9     callee(xraw);
10     let _val = *xref; // ...but any use of raw will invalidate our ref.
11     //~^ ERROR: /read access .* tag does not exist in the borrow stack/
12 }
13
14 fn callee(xraw: *mut i32) {
15     // We are a bit sneaky: We first create a shared ref, exploiting the reborrowing rules,
16     // and then we read through that.
17     let shr = unsafe { &*xraw };
18     let _val = *shr;
19 }