@@ -293,7 +293,10 @@ unsafe impl<T> SliceIndex<[T]> for usize {
293293 ( this: usize = self , len: usize = slice. len( ) ) => this < len
294294 ) ;
295295 // SAFETY: see comments for `get_unchecked` above.
296- unsafe { get_mut_noubcheck ( slice, self ) }
296+ unsafe {
297+ crate :: intrinsics:: assume ( self < slice. len ( ) ) ;
298+ get_mut_noubcheck ( slice, self )
299+ }
297300 }
298301
299302 #[ inline]
@@ -346,7 +349,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
346349 // cannot be longer than `isize::MAX`. They also guarantee that
347350 // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
348351 // so the call to `add` is safe.
349- unsafe { get_offset_len_noubcheck ( slice, self . start ( ) , self . len ( ) ) }
352+ unsafe {
353+ crate :: intrinsics:: assume ( self . end ( ) <= slice. len ( ) ) ;
354+ get_offset_len_noubcheck ( slice, self . start ( ) , self . len ( ) )
355+ }
350356 }
351357
352358 #[ inline]
@@ -358,7 +364,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
358364 ) ;
359365
360366 // SAFETY: see comments for `get_unchecked` above.
361- unsafe { get_offset_len_mut_noubcheck ( slice, self . start ( ) , self . len ( ) ) }
367+ unsafe {
368+ crate :: intrinsics:: assume ( self . end ( ) <= slice. len ( ) ) ;
369+ get_offset_len_mut_noubcheck ( slice, self . start ( ) , self . len ( ) )
370+ }
362371 }
363372
364373 #[ inline]
@@ -434,6 +443,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
434443 unsafe {
435444 // Using the intrinsic avoids a superfluous UB check,
436445 // since the one on this method already checked `end >= start`.
446+ crate :: intrinsics:: assume ( self . end <= slice. len ( ) ) ;
437447 let new_len = crate :: intrinsics:: unchecked_sub ( self . end , self . start ) ;
438448 get_offset_len_noubcheck ( slice, self . start , new_len)
439449 }
@@ -452,6 +462,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
452462 ) ;
453463 // SAFETY: see comments for `get_unchecked` above.
454464 unsafe {
465+ crate :: intrinsics:: assume ( self . end <= slice. len ( ) ) ;
455466 let new_len = crate :: intrinsics:: unchecked_sub ( self . end , self . start ) ;
456467 get_offset_len_mut_noubcheck ( slice, self . start , new_len)
457468 }
0 commit comments