@@ -34,36 +34,41 @@ where
34
34
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) , cold) ]
35
35
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
36
36
#[ track_caller]
37
- const fn slice_start_index_len_fail ( index : usize , len : usize ) -> ! {
38
- const_panic ! (
39
- "slice start index is out of range for slice" ,
40
- "range start index {index} out of range for slice of length {len}" ,
41
- index: usize ,
42
- len: usize ,
43
- )
44
- }
45
-
46
- #[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) , cold) ]
47
- #[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
48
- #[ track_caller]
49
- const fn slice_end_index_len_fail ( index : usize , len : usize ) -> ! {
37
+ const fn slice_index_fail ( start : usize , end : usize , len : usize ) -> ! {
38
+ if start > len {
39
+ const_panic ! (
40
+ "slice start index is out of range for slice" ,
41
+ "range start index {start} out of range for slice of length {len}" ,
42
+ start: usize ,
43
+ len: usize ,
44
+ )
45
+ }
46
+
47
+ if end > len {
48
+ const_panic ! (
49
+ "slice end index is out of range for slice" ,
50
+ "range end index {end} out of range for slice of length {len}" ,
51
+ end: usize ,
52
+ len: usize ,
53
+ )
54
+ }
55
+
56
+ if start > end {
57
+ const_panic ! (
58
+ "slice index start is larger than end" ,
59
+ "slice index starts at {start} but ends at {end}" ,
60
+ start: usize ,
61
+ end: usize ,
62
+ )
63
+ }
64
+
65
+ // Only reachable if the range was a `RangeInclusive` or a
66
+ // `RangeToInclusive`, with `end == len`.
50
67
const_panic ! (
51
68
"slice end index is out of range for slice" ,
52
- "range end index {index} out of range for slice of length {len}" ,
53
- index: usize ,
54
- len: usize ,
55
- )
56
- }
57
-
58
- #[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) , cold) ]
59
- #[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
60
- #[ track_caller]
61
- const fn slice_index_order_fail ( index : usize , end : usize ) -> ! {
62
- const_panic ! (
63
- "slice index start is larger than end" ,
64
- "slice index starts at {index} but ends at {end}" ,
65
- index: usize ,
69
+ "range end index {end} out of range for slice of length {len}" ,
66
70
end: usize ,
71
+ len: usize ,
67
72
)
68
73
}
69
74
@@ -327,7 +332,7 @@ unsafe impl<T> const SliceIndex<[T]> for ops::IndexRange {
327
332
// SAFETY: `self` is checked to be valid and in bounds above.
328
333
unsafe { & * get_offset_len_noubcheck ( slice, self . start ( ) , self . len ( ) ) }
329
334
} else {
330
- slice_end_index_len_fail ( self . end ( ) , slice. len ( ) )
335
+ slice_index_fail ( self . start ( ) , self . end ( ) , slice. len ( ) )
331
336
}
332
337
}
333
338
@@ -337,7 +342,7 @@ unsafe impl<T> const SliceIndex<[T]> for ops::IndexRange {
337
342
// SAFETY: `self` is checked to be valid and in bounds above.
338
343
unsafe { & mut * get_offset_len_mut_noubcheck ( slice, self . start ( ) , self . len ( ) ) }
339
344
} else {
340
- slice_end_index_len_fail ( self . end ( ) , slice. len ( ) )
345
+ slice_index_fail ( self . start ( ) , self . end ( ) , slice. len ( ) )
341
346
}
342
347
}
343
348
}
@@ -423,10 +428,10 @@ unsafe impl<T> const SliceIndex<[T]> for ops::Range<usize> {
423
428
fn index ( self , slice : & [ T ] ) -> & [ T ] {
424
429
// Using checked_sub is a safe way to get `SubUnchecked` in MIR
425
430
let Some ( new_len) = usize:: checked_sub ( self . end , self . start ) else {
426
- slice_index_order_fail ( self . start , self . end )
431
+ slice_index_fail ( self . start , self . end , slice . len ( ) )
427
432
} ;
428
433
if self . end > slice. len ( ) {
429
- slice_end_index_len_fail ( self . end , slice. len ( ) ) ;
434
+ slice_index_fail ( self . start , self . end , slice. len ( ) )
430
435
}
431
436
// SAFETY: `self` is checked to be valid and in bounds above.
432
437
unsafe { & * get_offset_len_noubcheck ( slice, self . start , new_len) }
@@ -435,10 +440,10 @@ unsafe impl<T> const SliceIndex<[T]> for ops::Range<usize> {
435
440
#[ inline]
436
441
fn index_mut ( self , slice : & mut [ T ] ) -> & mut [ T ] {
437
442
let Some ( new_len) = usize:: checked_sub ( self . end , self . start ) else {
438
- slice_index_order_fail ( self . start , self . end )
443
+ slice_index_fail ( self . start , self . end , slice . len ( ) )
439
444
} ;
440
445
if self . end > slice. len ( ) {
441
- slice_end_index_len_fail ( self . end , slice. len ( ) ) ;
446
+ slice_index_fail ( self . start , self . end , slice. len ( ) )
442
447
}
443
448
// SAFETY: `self` is checked to be valid and in bounds above.
444
449
unsafe { & mut * get_offset_len_mut_noubcheck ( slice, self . start , new_len) }
@@ -553,7 +558,7 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeFrom<usize> {
553
558
#[ inline]
554
559
fn index ( self , slice : & [ T ] ) -> & [ T ] {
555
560
if self . start > slice. len ( ) {
556
- slice_start_index_len_fail ( self . start , slice. len ( ) ) ;
561
+ slice_index_fail ( self . start , slice. len ( ) , slice . len ( ) )
557
562
}
558
563
// SAFETY: `self` is checked to be valid and in bounds above.
559
564
unsafe { & * self . get_unchecked ( slice) }
@@ -562,7 +567,7 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeFrom<usize> {
562
567
#[ inline]
563
568
fn index_mut ( self , slice : & mut [ T ] ) -> & mut [ T ] {
564
569
if self . start > slice. len ( ) {
565
- slice_start_index_len_fail ( self . start , slice. len ( ) ) ;
570
+ slice_index_fail ( self . start , slice. len ( ) , slice . len ( ) )
566
571
}
567
572
// SAFETY: `self` is checked to be valid and in bounds above.
568
573
unsafe { & mut * self . get_unchecked_mut ( slice) }
@@ -678,15 +683,15 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
678
683
#[ inline]
679
684
fn index ( self , slice : & [ T ] ) -> & [ T ] {
680
685
if * self . end ( ) >= slice. len ( ) {
681
- slice_end_index_len_fail ( * self . end ( ) , slice. len ( ) ) ;
686
+ slice_index_fail ( self . start , self . end , slice. len ( ) )
682
687
}
683
688
self . into_slice_range ( ) . index ( slice)
684
689
}
685
690
686
691
#[ inline]
687
692
fn index_mut ( self , slice : & mut [ T ] ) -> & mut [ T ] {
688
693
if * self . end ( ) >= slice. len ( ) {
689
- slice_end_index_len_fail ( * self . end ( ) , slice. len ( ) ) ;
694
+ slice_index_fail ( self . start , self . end , slice. len ( ) )
690
695
}
691
696
self . into_slice_range ( ) . index_mut ( slice)
692
697
}
@@ -840,22 +845,22 @@ where
840
845
let len = bounds. end ;
841
846
842
847
let end = match range. end_bound ( ) {
843
- ops:: Bound :: Included ( & end) if end >= len => slice_end_index_len_fail ( end, len) ,
848
+ ops:: Bound :: Included ( & end) if end >= len => slice_index_fail ( 0 , end, len) ,
844
849
// Cannot overflow because `end < len` implies `end < usize::MAX`.
845
850
ops:: Bound :: Included ( & end) => end + 1 ,
846
851
847
- ops:: Bound :: Excluded ( & end) if end > len => slice_end_index_len_fail ( end, len) ,
852
+ ops:: Bound :: Excluded ( & end) if end > len => slice_index_fail ( 0 , end, len) ,
848
853
ops:: Bound :: Excluded ( & end) => end,
849
854
850
855
ops:: Bound :: Unbounded => len,
851
856
} ;
852
857
853
858
let start = match range. start_bound ( ) {
854
- ops:: Bound :: Excluded ( & start) if start >= end => slice_index_order_fail ( start, end) ,
859
+ ops:: Bound :: Excluded ( & start) if start >= end => slice_index_fail ( start, end, len ) ,
855
860
// Cannot overflow because `start < end` implies `start < usize::MAX`.
856
861
ops:: Bound :: Excluded ( & start) => start + 1 ,
857
862
858
- ops:: Bound :: Included ( & start) if start > end => slice_index_order_fail ( start, end) ,
863
+ ops:: Bound :: Included ( & start) if start > end => slice_index_fail ( start, end, len ) ,
859
864
ops:: Bound :: Included ( & start) => start,
860
865
861
866
ops:: Bound :: Unbounded => 0 ,
@@ -985,22 +990,22 @@ pub(crate) fn into_slice_range(
985
990
( start, end) : ( ops:: Bound < usize > , ops:: Bound < usize > ) ,
986
991
) -> ops:: Range < usize > {
987
992
let end = match end {
988
- ops:: Bound :: Included ( end) if end >= len => slice_end_index_len_fail ( end, len) ,
993
+ ops:: Bound :: Included ( end) if end >= len => slice_index_fail ( 0 , end, len) ,
989
994
// Cannot overflow because `end < len` implies `end < usize::MAX`.
990
995
ops:: Bound :: Included ( end) => end + 1 ,
991
996
992
- ops:: Bound :: Excluded ( end) if end > len => slice_end_index_len_fail ( end, len) ,
997
+ ops:: Bound :: Excluded ( end) if end > len => slice_index_fail ( 0 , end, len) ,
993
998
ops:: Bound :: Excluded ( end) => end,
994
999
995
1000
ops:: Bound :: Unbounded => len,
996
1001
} ;
997
1002
998
1003
let start = match start {
999
- ops:: Bound :: Excluded ( start) if start >= end => slice_index_order_fail ( start, end) ,
1004
+ ops:: Bound :: Excluded ( start) if start >= end => slice_index_fail ( start, end, len ) ,
1000
1005
// Cannot overflow because `start < end` implies `start < usize::MAX`.
1001
1006
ops:: Bound :: Excluded ( start) => start + 1 ,
1002
1007
1003
- ops:: Bound :: Included ( start) if start > end => slice_index_order_fail ( start, end) ,
1008
+ ops:: Bound :: Included ( start) if start > end => slice_index_fail ( start, end, len ) ,
1004
1009
ops:: Bound :: Included ( start) => start,
1005
1010
1006
1011
ops:: Bound :: Unbounded => 0 ,
0 commit comments