Skip to content

Add regression test for saturating_sub bounds check issue #145064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/codegen-llvm/issues/saturating-sub-index-139759.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
}
Loading