Skip to content

Commit 5ab4c81

Browse files
committed
str/slice: factor out overflow error messages
1 parent 7fbc4d8 commit 5ab4c81

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/libcore/slice/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,12 @@ fn slice_index_order_fail(index: usize, end: usize) -> ! {
22622262
panic!("slice index starts at {} but ends at {}", index, end);
22632263
}
22642264

2265+
#[inline(never)]
2266+
#[cold]
2267+
fn slice_index_overflow_fail() -> ! {
2268+
panic!("attempted to index slice up to maximum usize");
2269+
}
2270+
22652271
/// A helper trait used for indexing operations.
22662272
#[unstable(feature = "slice_get_slice", issue = "35729")]
22672273
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
@@ -2538,15 +2544,13 @@ impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
25382544

25392545
#[inline]
25402546
fn index(self, slice: &[T]) -> &[T] {
2541-
assert!(self.end != usize::max_value(),
2542-
"attempted to index slice up to maximum usize");
2547+
if self.end == usize::max_value() { slice_index_overflow_fail(); }
25432548
(self.start..self.end + 1).index(slice)
25442549
}
25452550

25462551
#[inline]
25472552
fn index_mut(self, slice: &mut [T]) -> &mut [T] {
2548-
assert!(self.end != usize::max_value(),
2549-
"attempted to index slice up to maximum usize");
2553+
if self.end == usize::max_value() { slice_index_overflow_fail(); }
25502554
(self.start..self.end + 1).index_mut(slice)
25512555
}
25522556
}

src/libcore/str/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,12 @@ mod traits {
18491849
}
18501850
}
18511851

1852+
#[inline(never)]
1853+
#[cold]
1854+
fn str_index_overflow_fail() -> ! {
1855+
panic!("attempted to index str up to maximum usize");
1856+
}
1857+
18521858
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
18531859
impl SliceIndex<str> for ops::RangeFull {
18541860
type Output = str;
@@ -2053,14 +2059,12 @@ mod traits {
20532059
}
20542060
#[inline]
20552061
fn index(self, slice: &str) -> &Self::Output {
2056-
assert!(self.end != usize::max_value(),
2057-
"attempted to index str up to maximum usize");
2062+
if self.end == usize::max_value() { str_index_overflow_fail(); }
20582063
(self.start..self.end+1).index(slice)
20592064
}
20602065
#[inline]
20612066
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
2062-
assert!(self.end != usize::max_value(),
2063-
"attempted to index str up to maximum usize");
2067+
if self.end == usize::max_value() { str_index_overflow_fail(); }
20642068
(self.start..self.end+1).index_mut(slice)
20652069
}
20662070
}
@@ -2098,14 +2102,12 @@ mod traits {
20982102
}
20992103
#[inline]
21002104
fn index(self, slice: &str) -> &Self::Output {
2101-
assert!(self.end != usize::max_value(),
2102-
"attempted to index str up to maximum usize");
2105+
if self.end == usize::max_value() { str_index_overflow_fail(); }
21032106
(..self.end+1).index(slice)
21042107
}
21052108
#[inline]
21062109
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
2107-
assert!(self.end != usize::max_value(),
2108-
"attempted to index str up to maximum usize");
2110+
if self.end == usize::max_value() { str_index_overflow_fail(); }
21092111
(..self.end+1).index_mut(slice)
21102112
}
21112113
}

0 commit comments

Comments
 (0)