about summary refs log tree commit diff
path: root/tests/codegen-llvm/issues/saturating-sub-index-139759.rs
blob: eac2f4d306b93a9d44afefe65810c898681eaab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Test that calculating an index with saturating subtraction from an in-bounds
// index doesn't generate another bounds check.

//@ compile-flags: -Copt-level=3
//@ min-llvm-version: 21

#![crate_type = "lib"]

// CHECK-LABEL: @bounds_check_is_elided
#[no_mangle]
pub fn bounds_check_is_elided(s: &[i32], index: usize) -> i32 {
    // CHECK-NOT: panic_bounds_check
    if index < s.len() {
        let lower_bound = index.saturating_sub(1);
        s[lower_bound]
    } else {
        -1
    }
}