Skip to content

Commit d92dead

Browse files
authored
Rollup merge of rust-lang#147620 - saethlin:RangeFrom-noubcheck, r=scottmcm
Avoid redundant UB check in RangeFrom slice indexing I noticed this while picking through the IR we generate for rust-lang#134938. I think we just forgot to apply this trick to `RangeFrom`?
2 parents 23c518d + 4f1b945 commit d92dead

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/core/src/slice/index.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,10 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeFrom<usize> {
564564
slice_index_fail(self.start, slice.len(), slice.len())
565565
}
566566
// SAFETY: `self` is checked to be valid and in bounds above.
567-
unsafe { &*self.get_unchecked(slice) }
567+
unsafe {
568+
let new_len = crate::intrinsics::unchecked_sub(slice.len(), self.start);
569+
&*get_offset_len_noubcheck(slice, self.start, new_len)
570+
}
568571
}
569572

570573
#[inline]
@@ -573,7 +576,10 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeFrom<usize> {
573576
slice_index_fail(self.start, slice.len(), slice.len())
574577
}
575578
// SAFETY: `self` is checked to be valid and in bounds above.
576-
unsafe { &mut *self.get_unchecked_mut(slice) }
579+
unsafe {
580+
let new_len = crate::intrinsics::unchecked_sub(slice.len(), self.start);
581+
&mut *get_offset_len_mut_noubcheck(slice, self.start, new_len)
582+
}
577583
}
578584
}
579585

0 commit comments

Comments
 (0)