rust/tests/mir-opt/null_check_references.rs
Bastian Kersting b151b513ba Insert null checks for pointer dereferences when debug assertions are enabled
Similar to how the alignment is already checked, this adds a check
for null pointer dereferences in debug mode. It is implemented similarly
to the alignment check as a MirPass.

This is related to a 2025H1 project goal for better UB checks in debug
mode: https://github.com/rust-lang/rust-project-goals/pull/177.
2025-01-31 11:13:34 +00:00

16 lines
296 B
Rust

//@ compile-flags: -C debug-assertions
struct Null {
a: u32,
}
fn main() {
// CHECK-LABEL: fn main(
// CHECK-NOT: {{assert.*}}
let val: u32 = 42;
let val_ref: &u32 = &val;
let _access1: &u32 = &*val_ref;
let val = Null { a: 42 };
let _access2: &u32 = &val.a;
}