]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/null_pointer_write_zst.rs
63474d9651756444bce8152e0dc09587c99ffab5
[rust.git] / tests / compile-fail / null_pointer_write_zst.rs
1 // Some optimizations remove ZST accesses, thus masking this UB.
2 // compile-flags: -Zmir-opt-level=0
3 // error-pattern: memory access failed: null pointer is not a valid pointer
4
5 #[allow(deref_nullptr)]
6 fn main() {
7     // Not using the () type here, as writes of that type do not even have MIR generated.
8     // Also not assigning directly as that's array initialization, not assignment.
9     let zst_val = [1u8; 0];
10     unsafe { std::ptr::null_mut::<[u8; 0]>().write(zst_val) };
11 }