Skip to content

Commit 8e814f3

Browse files
committed
signed strict division: just use normal division
1 parent 267c405 commit 8e814f3

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

library/core/src/num/imp/overflow_panic.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ pub(in crate::num) const fn mul() -> ! {
2020
panic!("attempt to multiply with overflow")
2121
}
2222

23-
#[cold]
24-
#[track_caller]
25-
pub(in crate::num) const fn div() -> ! {
26-
panic!("attempt to divide with overflow")
27-
}
28-
2923
#[cold]
3024
#[track_caller]
3125
pub(in crate::num) const fn rem() -> ! {

library/core/src/num/int_macros.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,12 @@ macro_rules! int_impl {
983983
/// This function will always panic on overflow, regardless of whether overflow checks are enabled.
984984
///
985985
/// The only case where such an overflow can occur is when one divides `MIN / -1` on a signed type (where
986-
/// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
986+
/// `MIN` is the negative minimal value for the type): the result of this is `-MIN`, a positive value
987987
/// that is too large to represent in the type.
988988
///
989+
/// Note that this is equivalent to normal division: `MIN / -1` will also panic both in
990+
/// debug and release builds.
991+
///
989992
/// # Examples
990993
///
991994
/// ```
@@ -1010,8 +1013,8 @@ macro_rules! int_impl {
10101013
#[inline]
10111014
#[track_caller]
10121015
pub const fn strict_div(self, rhs: Self) -> Self {
1013-
let (a, b) = self.overflowing_div(rhs);
1014-
if b { imp::overflow_panic::div() } else { a }
1016+
// Normal division already checks for "div-by-minus-1".
1017+
self / rhs
10151018
}
10161019

10171020
/// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
@@ -1050,9 +1053,12 @@ macro_rules! int_impl {
10501053
/// This function will always panic on overflow, regardless of whether overflow checks are enabled.
10511054
///
10521055
/// The only case where such an overflow can occur is when one divides `MIN / -1` on a signed type (where
1053-
/// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
1056+
/// `MIN` is the negative minimal value for the type); the result of this is `-MIN`, a positive value
10541057
/// that is too large to represent in the type.
10551058
///
1059+
/// Note that this is equivalent to `div_euclid`: `MIN.div_euclid(-1)` will also panic both
1060+
/// in debug and release builds.
1061+
///
10561062
/// # Examples
10571063
///
10581064
/// ```
@@ -1077,8 +1083,8 @@ macro_rules! int_impl {
10771083
#[inline]
10781084
#[track_caller]
10791085
pub const fn strict_div_euclid(self, rhs: Self) -> Self {
1080-
let (a, b) = self.overflowing_div_euclid(rhs);
1081-
if b { imp::overflow_panic::div() } else { a }
1086+
// Normal `div_euclid` already checks for "div-by-minus-1".
1087+
self.div_euclid(rhs)
10821088
}
10831089

10841090
/// Checked integer division without remainder. Computes `self / rhs`,

0 commit comments

Comments
 (0)