]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/observed_local_mut.rs
Add 'src/tools/miri/' from commit '75dd959a3a40eb5b4574f8d2e23aa6efbeb33573'
[rust.git] / src / tools / miri / tests / pass / observed_local_mut.rs
1 // Stacked Borrows catches this (correctly) as UB.
2 //@compile-flags: -Zmiri-disable-stacked-borrows
3
4 // This test is intended to guard against the problem described in commit
5 // 39bb1254d1eaf74f45a4e741097e33fc942168d5.
6 //
7 // As written, it might be considered UB in compiled Rust, but of course Miri gives it a safe,
8 // deterministic behaviour (one that might not correspond with how an eventual Rust spec would
9 // defined this).
10 //
11 // An alternative way to write the test without `unsafe` would be to use `Cell<i32>`, but it would
12 // only surface the bug described by the above commit if `Cell<i32>` on the stack got represented
13 // as a primitive `PrimVal::I32` which is not yet the case.
14
15 fn main() {
16     let mut x = 0;
17     let y: *const i32 = &x;
18     x = 1;
19
20     // When the described bug is in place, this results in `0`, not observing the `x = 1` line.
21     assert_eq!(unsafe { *y }, 1);
22
23     assert_eq!(x, 1);
24 }