about summary refs log tree commit diff
path: root/src/libcore/str
diff options
context:
space:
mode:
authorMichael Lamparski <diagonaldevice@gmail.com>2018-04-30 07:36:27 -0400
committerMichael Lamparski <diagonaldevice@gmail.com>2018-04-30 07:36:27 -0400
commit5ab4c811caba9cfac1488919efae6c6675e68e4c (patch)
tree9cacb8f7dacc4f8646c017a8bb5fa89ae8377c23 /src/libcore/str
parent7fbc4d881da41782233f8e0993e7613cee73d57b (diff)
downloadrust-5ab4c811caba9cfac1488919efae6c6675e68e4c.tar.gz
rust-5ab4c811caba9cfac1488919efae6c6675e68e4c.zip
str/slice: factor out overflow error messages
Diffstat (limited to 'src/libcore/str')
-rw-r--r--src/libcore/str/mod.rs18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index b39d9feb35b..f7555700ebc 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -1849,6 +1849,12 @@ mod traits {
         }
     }
 
+    #[inline(never)]
+    #[cold]
+    fn str_index_overflow_fail() -> ! {
+        panic!("attempted to index str up to maximum usize");
+    }
+
     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
     impl SliceIndex<str> for ops::RangeFull {
         type Output = str;
@@ -2053,14 +2059,12 @@ mod traits {
         }
         #[inline]
         fn index(self, slice: &str) -> &Self::Output {
-            assert!(self.end != usize::max_value(),
-                "attempted to index str up to maximum usize");
+            if self.end == usize::max_value() { str_index_overflow_fail(); }
             (self.start..self.end+1).index(slice)
         }
         #[inline]
         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
-            assert!(self.end != usize::max_value(),
-                "attempted to index str up to maximum usize");
+            if self.end == usize::max_value() { str_index_overflow_fail(); }
             (self.start..self.end+1).index_mut(slice)
         }
     }
@@ -2098,14 +2102,12 @@ mod traits {
         }
         #[inline]
         fn index(self, slice: &str) -> &Self::Output {
-            assert!(self.end != usize::max_value(),
-                "attempted to index str up to maximum usize");
+            if self.end == usize::max_value() { str_index_overflow_fail(); }
             (..self.end+1).index(slice)
         }
         #[inline]
         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
-            assert!(self.end != usize::max_value(),
-                "attempted to index str up to maximum usize");
+            if self.end == usize::max_value() { str_index_overflow_fail(); }
             (..self.end+1).index_mut(slice)
         }
     }