about summary refs log tree commit diff
path: root/tests/codegen-llvm/bounds-check-elision-slice-min.rs
diff options
context:
space:
mode:
authorlucarlig <luca.carlig@huawei.com>2025-08-01 18:27:59 +0100
committerlucarlig <luca.carlig@huawei.com>2025-08-01 18:38:22 +0100
commit19c6815a212d86c389ef54c3ddb02697ffc45f93 (patch)
tree0de4521bb1f704d0194b71ee0b17a965a0e7311e /tests/codegen-llvm/bounds-check-elision-slice-min.rs
parent4b55fe199cfe9c710555a5af7f2a49491ad38254 (diff)
downloadrust-19c6815a212d86c389ef54c3ddb02697ffc45f93.tar.gz
rust-19c6815a212d86c389ef54c3ddb02697ffc45f93.zip
Multiple bounds checking elision failures
Diffstat (limited to 'tests/codegen-llvm/bounds-check-elision-slice-min.rs')
-rw-r--r--tests/codegen-llvm/bounds-check-elision-slice-min.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/codegen-llvm/bounds-check-elision-slice-min.rs b/tests/codegen-llvm/bounds-check-elision-slice-min.rs
new file mode 100644
index 00000000000..e160e5da50f
--- /dev/null
+++ b/tests/codegen-llvm/bounds-check-elision-slice-min.rs
@@ -0,0 +1,19 @@
+//! Regression test for #<https://github.com/rust-lang/rust/issues/120433>:
+//! Multiple bounds checking elision failures
+//! (ensures bounds checks are properly elided,
+//! with no calls to panic_bounds_check in the LLVM IR).
+
+//@ compile-flags: -C opt-level=3
+
+#![crate_type = "lib"]
+
+// CHECK-LABEL: @foo
+// CHECK-NOT: panic_bounds_check
+#[no_mangle]
+pub fn foo(buf: &[u8], alloced_size: usize) -> &[u8] {
+    if alloced_size.checked_add(1).map(|total| buf.len() < total).unwrap_or(true) {
+        return &[];
+    }
+    let size = buf[0];
+    &buf[1..1 + usize::min(alloced_size, usize::from(size))]
+}