Skip to content

Added codegen test for elidings bounds check when indexes are manually checked #134892

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
Dec 30, 2024
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
37 changes: 37 additions & 0 deletions tests/codegen/slice-indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,40 @@ pub unsafe fn str_get_unchecked_mut_by_range(x: &mut str, r: Range<usize>) -> &m
// CHECK: sub nuw i64
x.get_unchecked_mut(r)
}

// CHECK-LABEL: @slice_repeated_indexing(
#[no_mangle]
pub fn slice_repeated_indexing(dst: &mut [u8], offset: usize) {
let mut i = offset;
// CHECK: panic_bounds_check
dst[i] = 1;
i += 1;
// CHECK: panic_bounds_check
dst[i] = 2;
i += 1;
// CHECK: panic_bounds_check
dst[i] = 3;
i += 1;
// CHECK: panic_bounds_check
dst[i] = 4;
}

// CHECK-LABEL: @slice_repeated_indexing_coalesced(
#[no_mangle]
pub fn slice_repeated_indexing_coalesced(dst: &mut [u8], offset: usize) {
let mut i = offset;
if i.checked_add(4).unwrap() <= dst.len() {
// CHECK-NOT: panic_bounds_check
dst[i] = 1;
i += 1;
// CHECK-NOT: panic_bounds_check
dst[i] = 2;
i += 1;
// CHECK-NOT: panic_bounds_check
dst[i] = 3;
i += 1;
// CHECK-NOT: panic_bounds_check
dst[i] = 4;
}
// CHECK: ret
}
Loading