]>
Commit | Line | Data |
---|---|---|
add651ee FG |
1 | // unit-test: CopyProp |
2 | // | |
9ffffee4 FG |
3 | // This attempts to mutate `a` via a pointer derived from `addr_of!(a)`. That is UB |
4 | // according to Miri. However, the decision to make this UB - and to allow | |
5 | // rustc to rely on that fact for the purpose of optimizations - has not been | |
6 | // finalized. | |
7 | // | |
8 | // As such, we include this test to ensure that copy prop does not rely on that | |
9 | // fact. Specifically, if `addr_of!(a)` could not be used to modify a, it would | |
10 | // be correct for CopyProp to replace all occurrences of `a` with `c` - but that | |
11 | // would cause `f(true)` to output `false` instead of `true`. | |
12 | ||
13 | #![feature(custom_mir, core_intrinsics)] | |
14 | #![allow(unused_assignments)] | |
15 | extern crate core; | |
16 | use core::intrinsics::mir::*; | |
17 | ||
18 | #[custom_mir(dialect = "analysis", phase = "post-cleanup")] | |
19 | fn f(c: bool) -> bool { | |
20 | mir!({ | |
21 | let a = c; | |
22 | let p = core::ptr::addr_of!(a); | |
23 | let p2 = core::ptr::addr_of_mut!(*p); | |
24 | *p2 = false; | |
25 | RET = c; | |
26 | Return() | |
27 | }) | |
28 | } | |
29 | ||
30 | fn main() { | |
31 | assert_eq!(true, f(true)); | |
32 | } | |
33 | ||
34 | // EMIT_MIR mutate_through_pointer.f.CopyProp.diff |