about summary refs log tree commit diff
path: root/tests/codegen/precondition-checks.rs
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2024-02-21 18:58:12 -0500
committerBen Kimock <kimockb@gmail.com>2024-03-12 19:01:04 -0400
commit81d630453b08253dc1d6bcc4139dde16530887d9 (patch)
tree6dbde7257f90320e73a477b9a4bd4e4aa8a22de4 /tests/codegen/precondition-checks.rs
parenta165f1f65015b1bd4afd2ec50700aaacf2e0c485 (diff)
downloadrust-81d630453b08253dc1d6bcc4139dde16530887d9.tar.gz
rust-81d630453b08253dc1d6bcc4139dde16530887d9.zip
Avoid lowering code under dead SwitchInt targets
Diffstat (limited to 'tests/codegen/precondition-checks.rs')
-rw-r--r--tests/codegen/precondition-checks.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/codegen/precondition-checks.rs b/tests/codegen/precondition-checks.rs
new file mode 100644
index 00000000000..19149445003
--- /dev/null
+++ b/tests/codegen/precondition-checks.rs
@@ -0,0 +1,27 @@
+//@ compile-flags: -Cno-prepopulate-passes -Copt-level=0 -Cdebug-assertions=no
+
+// This test ensures that in a debug build which turns off debug assertions, we do not monomorphize
+// any of the standard library's unsafe precondition checks.
+// The naive codegen of those checks contains the actual check underneath an `if false`, which
+// could be optimized out if optimizations are enabled. But if we rely on optimizations to remove
+// panic branches, then we can't link compiler_builtins without optimizing it, which means that
+// -Zbuild-std doesn't work with -Copt-level=0.
+//
+// In other words, this tests for a mandatory optimization.
+
+#![crate_type = "lib"]
+
+use std::ptr::NonNull;
+
+// CHECK-LABEL: ; core::ptr::non_null::NonNull<T>::new_unchecked
+// CHECK-NOT: call
+// CHECK: }
+
+// CHECK-LABEL: @nonnull_new
+#[no_mangle]
+pub unsafe fn nonnull_new(ptr: *mut u8) -> NonNull<u8> {
+    // CHECK: ; call core::ptr::non_null::NonNull<T>::new_unchecked
+    unsafe {
+        NonNull::new_unchecked(ptr)
+    }
+}