]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/zst2.rs
Add 'src/tools/miri/' from commit '75dd959a3a40eb5b4574f8d2e23aa6efbeb33573'
[rust.git] / src / tools / miri / tests / fail / zst2.rs
1 // Some optimizations remove ZST accesses, thus masking this UB.
2 //@compile-flags: -Zmir-opt-level=0
3
4 fn main() {
5     // Not using the () type here, as writes of that type do not even have MIR generated.
6     // Also not assigning directly as that's array initialization, not assignment.
7     let zst_val = [1u8; 0];
8
9     // make sure ZST accesses are checked against being "truly" dangling pointers
10     // (into deallocated allocations).
11     let mut x_box = Box::new(1u8);
12     let x = &mut *x_box as *mut _ as *mut [u8; 0];
13     drop(x_box);
14     unsafe { *x = zst_val }; //~ ERROR: dereferenced after this allocation got freed
15 }