]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/stacked_borrows/illegal_read3.rs
Auto merge of #102169 - scottmcm:constify-some-conditions, r=thomcc
[rust.git] / src / tools / miri / tests / 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 use std::mem;
6
7 union HiddenRef {
8     // We avoid retagging at this type, and we only read, so shared vs mutable does not matter.
9     r: &'static i32,
10 }
11
12 fn main() {
13     let mut x: i32 = 15;
14     let xref1 = &mut x;
15     let xref1_sneaky: HiddenRef = unsafe { mem::transmute_copy(&xref1) };
16     // Derived from `xref1`, so using raw value is still ok, ...
17     let xref2 = &mut *xref1;
18     callee(xref1_sneaky);
19     // ... though any use of it will invalidate our ref.
20     let _val = *xref2;
21     //~^ ERROR: /read access .* tag does not exist in the borrow stack/
22 }
23
24 fn callee(xref1: HiddenRef) {
25     // Doing the deref and the transmute (through the union) in the same place expression
26     // should avoid retagging.
27     let _val = unsafe { *xref1.r };
28 }