Skip to content

Commit

Permalink
Added codegen test for elidings bounds check when indexes are manuall…
Browse files Browse the repository at this point in the history
…y checked

Closes #55147
  • Loading branch information
alex committed Dec 29, 2024
1 parent e7738af commit dab1c57
Showing 1 changed file with 37 additions and 0 deletions.
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
}

0 comments on commit dab1c57

Please sign in to comment.