@@ -265,7 +265,10 @@ unsafe impl<T> SliceIndex<[T]> for usize {
265
265
( this: usize = self , len: usize = slice. len( ) ) => this < len
266
266
) ;
267
267
// 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
+ }
269
272
}
270
273
271
274
#[ inline]
@@ -317,7 +320,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
317
320
// cannot be longer than `isize::MAX`. They also guarantee that
318
321
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
319
322
// 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
+ }
321
327
}
322
328
323
329
#[ inline]
@@ -329,7 +335,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
329
335
) ;
330
336
331
337
// 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
+ }
333
342
}
334
343
335
344
#[ inline]
@@ -404,6 +413,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
404
413
unsafe {
405
414
// Using the intrinsic avoids a superfluous UB check,
406
415
// since the one on this method already checked `end >= start`.
416
+ crate :: intrinsics:: assume ( self . end <= slice. len ( ) ) ;
407
417
let new_len = crate :: intrinsics:: unchecked_sub ( self . end , self . start ) ;
408
418
get_offset_len_noubcheck ( slice, self . start , new_len)
409
419
}
@@ -422,6 +432,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
422
432
) ;
423
433
// SAFETY: see comments for `get_unchecked` above.
424
434
unsafe {
435
+ crate :: intrinsics:: assume ( self . end <= slice. len ( ) ) ;
425
436
let new_len = crate :: intrinsics:: unchecked_sub ( self . end , self . start ) ;
426
437
get_offset_len_mut_noubcheck ( slice, self . start , new_len)
427
438
}
0 commit comments