about summary refs log tree commit diff
path: root/tests/mir-opt/null_check_references.rs
diff options
context:
space:
mode:
authorBastian Kersting <bkersting@google.com>2024-12-17 13:00:22 +0000
committerBastian Kersting <bkersting@google.com>2025-01-31 11:13:34 +0000
commitb151b513ba2b65c7506ec1a80f2712bbd09154d1 (patch)
treefc1505e76399304e8da3aca3808ad1b494dfe4e8 /tests/mir-opt/null_check_references.rs
parent851322b74db9ac91a1b9d206c5f80fc51a97f7c1 (diff)
downloadrust-b151b513ba2b65c7506ec1a80f2712bbd09154d1.tar.gz
rust-b151b513ba2b65c7506ec1a80f2712bbd09154d1.zip
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.
Diffstat (limited to 'tests/mir-opt/null_check_references.rs')
-rw-r--r--tests/mir-opt/null_check_references.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/tests/mir-opt/null_check_references.rs b/tests/mir-opt/null_check_references.rs
new file mode 100644
index 00000000000..85f98865646
--- /dev/null
+++ b/tests/mir-opt/null_check_references.rs
@@ -0,0 +1,16 @@
+//@ 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;
+}