Skip to content

Commit

Permalink
Make overflowing_div and overflowing_rem const
Browse files Browse the repository at this point in the history
With rust-lang#49146 merged, {u8,i8,u16,...}::overflowing_div and
::overflowing_rem can be const; see rust-lang#53718.
  • Loading branch information
9999years committed Dec 19, 2019
1 parent 20db398 commit 09994af
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
63 changes: 59 additions & 4 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,9 @@ $EndFeature, "
#[stable(feature = "wrapping", since = "1.7.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
#[rustc_const_unstable(feature = "const_int_overflowing")]
#[cfg(not(bootstrap))]
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
(self, true)
} else {
Expand All @@ -1604,6 +1606,20 @@ $EndFeature, "
}
}

/// No docs for bootstrap.
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[cfg(bootstrap)]
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
(self, true)
} else {
(self / rhs, false)
}
}

doc_comment! {
concat!("Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
Expand Down Expand Up @@ -1663,7 +1679,9 @@ $EndFeature, "
#[stable(feature = "wrapping", since = "1.7.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
#[rustc_const_unstable(feature = "const_int_overflowing")]
#[cfg(not(bootstrap))]
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
(0, true)
} else {
Expand All @@ -1672,6 +1690,19 @@ $EndFeature, "
}
}

/// No docs for bootstrap.
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[cfg(bootstrap)]
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
(0, true)
} else {
(self % rhs, false)
}
}

doc_comment! {
concat!("Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`.
Expand Down Expand Up @@ -3517,11 +3548,23 @@ Basic usage
#[stable(feature = "wrapping", since = "1.7.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
#[rustc_const_unstable(feature = "const_int_overflowing")]
#[cfg(not(bootstrap))]
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
(self / rhs, false)
}
}

/// No docs for bootstrap.
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[cfg(bootstrap)]
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
(self / rhs, false)
}

doc_comment! {
concat!("Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
Expand Down Expand Up @@ -3576,11 +3619,23 @@ Basic usage
#[stable(feature = "wrapping", since = "1.7.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
#[rustc_const_unstable(feature = "const_int_overflowing")]
#[cfg(not(bootstrap))]
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
(self % rhs, false)
}
}

/// No docs for bootstrap.
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[cfg(bootstrap)]
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
(self % rhs, false)
}

doc_comment! {
concat!("Calculates the remainder `self.rem_euclid(rhs)` as if by Euclidean division.
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/consts/const-int-overflowing-rpass.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// run-pass
#![feature(const_int_overflowing)]

const ADD_A: (u32, bool) = 5u32.overflowing_add(2);
const ADD_B: (u32, bool) = u32::max_value().overflowing_add(1);
Expand All @@ -22,6 +23,18 @@ const ABS_POS: (i32, bool) = 10i32.overflowing_abs();
const ABS_NEG: (i32, bool) = (-10i32).overflowing_abs();
const ABS_MIN: (i32, bool) = i32::min_value().overflowing_abs();

const DIV_A: (i8, bool) = 8i8.overflowing_div(2);
const DIV_B: (i8, bool) = 8i8.overflowing_div(3);
const DIV_C: (i8, bool) = i8::min_value().overflowing_div(-1i8);
const DIV_D: (u8, bool) = 8u8.overflowing_div(2);
const DIV_E: (u8, bool) = 8u8.overflowing_div(3);

const REM_A: (i8, bool) = 8i8.overflowing_rem(2);
const REM_B: (i8, bool) = 8i8.overflowing_rem(3);
const REM_C: (i8, bool) = i8::min_value().overflowing_rem(-1i8);
const REM_D: (u8, bool) = 8u8.overflowing_rem(2);
const REM_E: (u8, bool) = 8u8.overflowing_rem(3);

fn main() {
assert_eq!(ADD_A, (7, false));
assert_eq!(ADD_B, (0, true));
Expand All @@ -44,4 +57,16 @@ fn main() {
assert_eq!(ABS_POS, (10, false));
assert_eq!(ABS_NEG, (10, false));
assert_eq!(ABS_MIN, (i32::min_value(), true));

assert_eq!(DIV_A, (4i8, false));
assert_eq!(DIV_B, (2i8, false));
assert_eq!(DIV_C, (i8::min_value(), true));
assert_eq!(DIV_D, (4u8, false));
assert_eq!(DIV_E, (2u8, false));

assert_eq!(REM_A, (0i8, false));
assert_eq!(REM_B, (2i8, false));
assert_eq!(REM_C, (0i8, true));
assert_eq!(REM_D, (0u8, false));
assert_eq!(REM_E, (2u8, false));
}

0 comments on commit 09994af

Please sign in to comment.