@@ -265,7 +265,10 @@ unsafe impl<T> SliceIndex<[T]> for usize {
265265 ( this: usize = self , len: usize = slice. len( ) ) => this < len
266266 ) ;
267267 // SAFETY: see comments for `get_unchecked` above.
268- unsafe { get_mut_noubcheck ( slice, self ) }
268+ unsafe {
269+ crate :: intrinsics:: assume ( self < slice. len ( ) ) ;
270+ get_mut_noubcheck ( slice, self )
271+ }
269272 }
270273
271274 #[ inline]
@@ -317,7 +320,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
317320 // cannot be longer than `isize::MAX`. They also guarantee that
318321 // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
319322 // so the call to `add` is safe.
320- unsafe { get_offset_len_noubcheck ( slice, self . start ( ) , self . len ( ) ) }
323+ unsafe {
324+ crate :: intrinsics:: assume ( self . end ( ) <= slice. len ( ) ) ;
325+ get_offset_len_noubcheck ( slice, self . start ( ) , self . len ( ) )
326+ }
321327 }
322328
323329 #[ inline]
@@ -329,7 +335,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
329335 ) ;
330336
331337 // SAFETY: see comments for `get_unchecked` above.
332- unsafe { get_offset_len_mut_noubcheck ( slice, self . start ( ) , self . len ( ) ) }
338+ unsafe {
339+ crate :: intrinsics:: assume ( self . end ( ) <= slice. len ( ) ) ;
340+ get_offset_len_mut_noubcheck ( slice, self . start ( ) , self . len ( ) )
341+ }
333342 }
334343
335344 #[ inline]
@@ -404,6 +413,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
404413 unsafe {
405414 // Using the intrinsic avoids a superfluous UB check,
406415 // since the one on this method already checked `end >= start`.
416+ crate :: intrinsics:: assume ( self . end <= slice. len ( ) ) ;
407417 let new_len = crate :: intrinsics:: unchecked_sub ( self . end , self . start ) ;
408418 get_offset_len_noubcheck ( slice, self . start , new_len)
409419 }
@@ -422,6 +432,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
422432 ) ;
423433 // SAFETY: see comments for `get_unchecked` above.
424434 unsafe {
435+ crate :: intrinsics:: assume ( self . end <= slice. len ( ) ) ;
425436 let new_len = crate :: intrinsics:: unchecked_sub ( self . end , self . start ) ;
426437 get_offset_len_mut_noubcheck ( slice, self . start , new_len)
427438 }
0 commit comments