2020-02-24 09:22:02 -06:00
|
|
|
// Stacked Borrows catches this (correctly) as UB.
|
2022-07-08 11:08:32 -05:00
|
|
|
//@compile-flags: -Zmiri-disable-stacked-borrows
|
2018-10-22 11:01:32 -05:00
|
|
|
|
2016-10-18 22:45:11 -05:00
|
|
|
// This test is intended to guard against the problem described in commit
|
|
|
|
// 39bb1254d1eaf74f45a4e741097e33fc942168d5.
|
|
|
|
//
|
|
|
|
// As written, it might be considered UB in compiled Rust, but of course Miri gives it a safe,
|
|
|
|
// deterministic behaviour (one that might not correspond with how an eventual Rust spec would
|
|
|
|
// defined this).
|
|
|
|
//
|
|
|
|
// An alternative way to write the test without `unsafe` would be to use `Cell<i32>`, but it would
|
|
|
|
// only surface the bug described by the above commit if `Cell<i32>` on the stack got represented
|
|
|
|
// as a primitive `PrimVal::I32` which is not yet the case.
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut x = 0;
|
|
|
|
let y: *const i32 = &x;
|
|
|
|
x = 1;
|
|
|
|
|
|
|
|
// When the described bug is in place, this results in `0`, not observing the `x = 1` line.
|
|
|
|
assert_eq!(unsafe { *y }, 1);
|
|
|
|
|
|
|
|
assert_eq!(x, 1);
|
|
|
|
}
|