Skip to content

Commit 163594c

Browse files
committed
Add regression test for saturating_sub bounds check issue
Add codegen test for issue where `valid_index.saturating_sub(X)` produced an extra bounds check. This was fixed by the LLVM upgrade.
1 parent cd43430 commit 163594c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Test that calculating an index with saturating subtraction from an in-bounds
2+
// index doesn't generate another bounds check.
3+
4+
//@ compile-flags: -Copt-level=3
5+
//@ min-llvm-version: 21
6+
7+
#![crate_type = "lib"]
8+
9+
// CHECK-LABEL: @bounds_check_is_elided
10+
#[no_mangle]
11+
pub fn bounds_check_is_elided(s: &[i32], index: usize) -> i32 {
12+
// CHECK-NOT: panic_bounds_check
13+
if index < s.len() {
14+
let lower_bound = index.saturating_sub(1);
15+
s[lower_bound]
16+
} else {
17+
-1
18+
}
19+
}

0 commit comments

Comments
 (0)