Skip to content

Commit 9e095fa

Browse files
committed
signed strict division: just use normal division
1 parent 267c405 commit 9e095fa

2 files changed

Lines changed: 14 additions & 13 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: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,13 @@ macro_rules! int_impl {
982982
///
983983
/// This function will always panic on overflow, regardless of whether overflow checks are enabled.
984984
///
985-
/// 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
987-
/// that is too large to represent in the type.
985+
/// The only case where such an overflow can occur is when one divides `MIN / -1` on a
986+
/// signed type (where `MIN` is the negative minimal value for the type); in wrapping
987+
/// arithmetic, this is equivalent to `-MIN`, a positive value that is too large to
988+
/// represent in the type.
989+
///
990+
/// Note that this is equivalent to normal division: `MIN / -1` will also panic both in
991+
/// debug and release builds.
988992
///
989993
/// # Examples
990994
///
@@ -1010,8 +1014,8 @@ macro_rules! int_impl {
10101014
#[inline]
10111015
#[track_caller]
10121016
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 }
1017+
// Normal division already checks for "div-by-minus-1".
1018+
self / rhs
10151019
}
10161020

10171021
/// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
@@ -1053,6 +1057,9 @@ macro_rules! int_impl {
10531057
/// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
10541058
/// that is too large to represent in the type.
10551059
///
1060+
/// Note that this is equivalent to `div_euclid`: `MIN.div_euclid(-1)` will also panic both
1061+
/// in debug and release builds.
1062+
///
10561063
/// # Examples
10571064
///
10581065
/// ```
@@ -1077,8 +1084,8 @@ macro_rules! int_impl {
10771084
#[inline]
10781085
#[track_caller]
10791086
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 }
1087+
// Normal `div_euclid` already checks for "div-by-minus-1".
1088+
self.div_euclid(rhs)
10821089
}
10831090

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

0 commit comments

Comments
 (0)