about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2016-10-08 16:52:43 +0530
committerGitHub <noreply@github.com>2016-10-08 16:52:43 +0530
commit73a9b8accd1b71e5333f15bddcb8f84674f1079b (patch)
tree85fc213da488ba8216dcc9a649f8c0f2cbee2f9f
parent3e6cc822d33e458dbfac53a1701ceac7543801e9 (diff)
parentc66ae29883faaed0fd32ad03165e6882069c4832 (diff)
downloadrust-73a9b8accd1b71e5333f15bddcb8f84674f1079b.tar.gz
rust-73a9b8accd1b71e5333f15bddcb8f84674f1079b.zip
Rollup merge of #37029 - japaric:no-panics-in-checked-ops, r=alexcrichton
rewrite checked_{div,rem} to no contain any reference to panics

even without optimizations

r? @alexcrichton
-rw-r--r--src/libcore/num/mod.rs14
1 files changed, 6 insertions, 8 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index 9a403891ebf..516d6f7c4a0 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -516,11 +516,10 @@ macro_rules! int_impl {
         #[stable(feature = "rust1", since = "1.0.0")]
         #[inline]
         pub fn checked_div(self, other: Self) -> Option<Self> {
-            if other == 0 {
+            if other == 0 || (self == Self::min_value() && other == -1) {
                 None
             } else {
-                let (a, b) = self.overflowing_div(other);
-                if b {None} else {Some(a)}
+                Some(unsafe { intrinsics::unchecked_div(self, other) })
             }
         }
 
@@ -541,11 +540,10 @@ macro_rules! int_impl {
         #[stable(feature = "wrapping", since = "1.7.0")]
         #[inline]
         pub fn checked_rem(self, other: Self) -> Option<Self> {
-            if other == 0 {
+            if other == 0 || (self == Self::min_value() && other == -1) {
                 None
             } else {
-                let (a, b) = self.overflowing_rem(other);
-                if b {None} else {Some(a)}
+                Some(unsafe { intrinsics::unchecked_rem(self, other) })
             }
         }
 
@@ -1688,7 +1686,7 @@ macro_rules! uint_impl {
         pub fn checked_div(self, other: Self) -> Option<Self> {
             match other {
                 0 => None,
-                other => Some(self / other),
+                other => Some(unsafe { intrinsics::unchecked_div(self, other) }),
             }
         }
 
@@ -1709,7 +1707,7 @@ macro_rules! uint_impl {
             if other == 0 {
                 None
             } else {
-                Some(self % other)
+                Some(unsafe { intrinsics::unchecked_rem(self, other) })
             }
         }