Skip to content

Commit

Permalink
Rollup merge of rust-lang#134892 - alex:codegen-55147, r=durin42
Browse files Browse the repository at this point in the history
Added codegen test for elidings bounds check when indexes are manually checked

Closes rust-lang#55147
  • Loading branch information
matthiaskrgr authored Dec 29, 2024
2 parents e809de4 + dab1c57 commit d0dd350
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 d0dd350

Please sign in to comment.