about summary refs log tree commit diff
path: root/tests/codegen
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-07 00:04:20 +0000
committerbors <bors@rust-lang.org>2024-04-07 00:04:20 +0000
commit6f837503aa120ca69c2985b6c9a474c00674cef1 (patch)
treeebf23d09993158f5984fd3af4ec8b9f114ca3887 /tests/codegen
parentaa1c45908df252a5b0c14e1bcb38c6c55ae02efe (diff)
parent96dbde796ced67ac17d1dd15851cd8737bf6589a (diff)
Auto merge of #123576 - matthiaskrgr:rollup-9wiqkfa, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - #119224 (Drop panic hook after running tests)
 - #123411 (Put checks that detect UB under their own flag below debug_assertions)
 - #123516 (Do not ICE on field access check on expr with `ty::Error`)
 - #123522 (Stabilize const Atomic*::into_inner)
 - #123559 (Add a debug asserts call to match_projection_projections to ensure invariant)
 - #123563 (Rewrite `version` test run-make as an UI test)

Failed merges:

 - #123569 (Move some tests)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'tests/codegen')
-rw-r--r--tests/codegen/ub-checks.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/codegen/ub-checks.rs b/tests/codegen/ub-checks.rs
new file mode 100644
index 00000000000..de48d74e652
--- /dev/null
+++ b/tests/codegen/ub-checks.rs
@@ -0,0 +1,28 @@
+// With -Zub-checks=yes (enabled by default by -Cdebug-assertions=yes) we will produce a runtime
+// check that the index to slice::get_unchecked is in-bounds of the slice. That is tested for by
+// tests/ui/precondition-checks/out-of-bounds-get-unchecked.rs
+//
+// This test ensures that such a runtime check is *not* emitted when debug-assertions are enabled,
+// but ub-checks are explicitly disabled.
+
+//@ revisions: DEBUG NOCHECKS
+//@ [DEBUG] compile-flags:
+//@ [NOCHECKS] compile-flags: -Zub-checks=no
+//@ compile-flags: -O -Cdebug-assertions=yes
+
+#![crate_type = "lib"]
+
+use std::ops::Range;
+
+// CHECK-LABEL: @slice_get_unchecked(
+#[no_mangle]
+pub unsafe fn slice_get_unchecked(x: &[i32], i: usize) -> &i32 {
+    //    CHECK: icmp ult
+    // NOCHECKS: tail call void @llvm.assume
+    //    DEBUG: br i1
+    //    DEBUG: call core::panicking::panic_nounwind
+    //    DEBUG: unreachable
+    //    CHECK: getelementptr inbounds
+    //    CHECK: ret ptr
+    x.get_unchecked(i)
+}