mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-29 10:13:54 +00:00
Slightly improve panic messages when range indices are out of bounds
This commit is contained in:
parent
ce338046c8
commit
0d64b01639
@ -2974,8 +2974,15 @@ where
|
||||
#[inline(never)]
|
||||
#[cold]
|
||||
#[track_caller]
|
||||
fn slice_index_len_fail(index: usize, len: usize) -> ! {
|
||||
panic!("index {} out of range for slice of length {}", index, len);
|
||||
fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
|
||||
panic!("range start index {} out of range for slice of length {}", index, len);
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cold]
|
||||
#[track_caller]
|
||||
fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
|
||||
panic!("range end index {} out of range for slice of length {}", index, len);
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
@ -3160,7 +3167,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
|
||||
if self.start > self.end {
|
||||
slice_index_order_fail(self.start, self.end);
|
||||
} else if self.end > slice.len() {
|
||||
slice_index_len_fail(self.end, slice.len());
|
||||
slice_end_index_len_fail(self.end, slice.len());
|
||||
}
|
||||
unsafe { &*self.get_unchecked(slice) }
|
||||
}
|
||||
@ -3170,7 +3177,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
|
||||
if self.start > self.end {
|
||||
slice_index_order_fail(self.start, self.end);
|
||||
} else if self.end > slice.len() {
|
||||
slice_index_len_fail(self.end, slice.len());
|
||||
slice_end_index_len_fail(self.end, slice.len());
|
||||
}
|
||||
unsafe { &mut *self.get_unchecked_mut(slice) }
|
||||
}
|
||||
@ -3242,7 +3249,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
|
||||
#[inline]
|
||||
fn index(self, slice: &[T]) -> &[T] {
|
||||
if self.start > slice.len() {
|
||||
slice_index_len_fail(self.start, slice.len());
|
||||
slice_start_index_len_fail(self.start, slice.len());
|
||||
}
|
||||
unsafe { &*self.get_unchecked(slice) }
|
||||
}
|
||||
@ -3250,7 +3257,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
|
||||
#[inline]
|
||||
fn index_mut(self, slice: &mut [T]) -> &mut [T] {
|
||||
if self.start > slice.len() {
|
||||
slice_index_len_fail(self.start, slice.len());
|
||||
slice_start_index_len_fail(self.start, slice.len());
|
||||
}
|
||||
unsafe { &mut *self.get_unchecked_mut(slice) }
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
// CHECK-LABEL: @already_sliced_no_bounds_check
|
||||
#[no_mangle]
|
||||
pub fn already_sliced_no_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) {
|
||||
// CHECK: slice_index_len_fail
|
||||
// CHECK: slice_end_index_len_fail
|
||||
// CHECK-NOT: panic_bounds_check
|
||||
let _ = (&a[..2048], &b[..2048], &mut c[..2048]);
|
||||
for i in 0..1024 {
|
||||
@ -23,7 +23,7 @@ pub fn already_sliced_no_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) {
|
||||
// CHECK-LABEL: @already_sliced_no_bounds_check_exact
|
||||
#[no_mangle]
|
||||
pub fn already_sliced_no_bounds_check_exact(a: &[u8], b: &[u8], c: &mut [u8]) {
|
||||
// CHECK: slice_index_len_fail
|
||||
// CHECK: slice_end_index_len_fail
|
||||
// CHECK-NOT: panic_bounds_check
|
||||
let _ = (&a[..1024], &b[..1024], &mut c[..1024]);
|
||||
for i in 0..1024 {
|
||||
@ -35,7 +35,7 @@ pub fn already_sliced_no_bounds_check_exact(a: &[u8], b: &[u8], c: &mut [u8]) {
|
||||
// CHECK-LABEL: @already_sliced_bounds_check
|
||||
#[no_mangle]
|
||||
pub fn already_sliced_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) {
|
||||
// CHECK: slice_index_len_fail
|
||||
// CHECK: slice_end_index_len_fail
|
||||
// CHECK: panic_bounds_check
|
||||
let _ = (&a[..1023], &b[..2048], &mut c[..2048]);
|
||||
for i in 0..1024 {
|
||||
|
Loading…
Reference in New Issue
Block a user