@@ -293,7 +293,10 @@ unsafe impl<T> SliceIndex<[T]> for usize {
293
293
( this: usize = self , len: usize = slice. len( ) ) => this < len
294
294
) ;
295
295
// 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
+ }
297
300
}
298
301
299
302
#[ inline]
@@ -346,7 +349,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
346
349
// cannot be longer than `isize::MAX`. They also guarantee that
347
350
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
348
351
// 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
+ }
350
356
}
351
357
352
358
#[ inline]
@@ -358,7 +364,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
358
364
) ;
359
365
360
366
// 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
+ }
362
371
}
363
372
364
373
#[ inline]
@@ -434,6 +443,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
434
443
unsafe {
435
444
// Using the intrinsic avoids a superfluous UB check,
436
445
// since the one on this method already checked `end >= start`.
446
+ crate :: intrinsics:: assume ( self . end <= slice. len ( ) ) ;
437
447
let new_len = crate :: intrinsics:: unchecked_sub ( self . end , self . start ) ;
438
448
get_offset_len_noubcheck ( slice, self . start , new_len)
439
449
}
@@ -452,6 +462,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
452
462
) ;
453
463
// SAFETY: see comments for `get_unchecked` above.
454
464
unsafe {
465
+ crate :: intrinsics:: assume ( self . end <= slice. len ( ) ) ;
455
466
let new_len = crate :: intrinsics:: unchecked_sub ( self . end , self . start ) ;
456
467
get_offset_len_mut_noubcheck ( slice, self . start , new_len)
457
468
}
0 commit comments