Skip to content

Commit 09994af

Browse files
committed
Make overflowing_div and overflowing_rem const
With rust-lang#49146 merged, {u8,i8,u16,...}::overflowing_div and ::overflowing_rem can be const; see rust-lang#53718.
1 parent 20db398 commit 09994af

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

src/libcore/num/mod.rs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,9 @@ $EndFeature, "
15951595
#[stable(feature = "wrapping", since = "1.7.0")]
15961596
#[must_use = "this returns the result of the operation, \
15971597
without modifying the original"]
1598-
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1598+
#[rustc_const_unstable(feature = "const_int_overflowing")]
1599+
#[cfg(not(bootstrap))]
1600+
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
15991601
if self == Self::min_value() && rhs == -1 {
16001602
(self, true)
16011603
} else {
@@ -1604,6 +1606,20 @@ $EndFeature, "
16041606
}
16051607
}
16061608

1609+
/// No docs for bootstrap.
1610+
#[inline]
1611+
#[stable(feature = "wrapping", since = "1.7.0")]
1612+
#[must_use = "this returns the result of the operation, \
1613+
without modifying the original"]
1614+
#[cfg(bootstrap)]
1615+
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1616+
if self == Self::min_value() && rhs == -1 {
1617+
(self, true)
1618+
} else {
1619+
(self / rhs, false)
1620+
}
1621+
}
1622+
16071623
doc_comment! {
16081624
concat!("Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
16091625
@@ -1663,7 +1679,9 @@ $EndFeature, "
16631679
#[stable(feature = "wrapping", since = "1.7.0")]
16641680
#[must_use = "this returns the result of the operation, \
16651681
without modifying the original"]
1666-
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1682+
#[rustc_const_unstable(feature = "const_int_overflowing")]
1683+
#[cfg(not(bootstrap))]
1684+
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
16671685
if self == Self::min_value() && rhs == -1 {
16681686
(0, true)
16691687
} else {
@@ -1672,6 +1690,19 @@ $EndFeature, "
16721690
}
16731691
}
16741692

1693+
/// No docs for bootstrap.
1694+
#[inline]
1695+
#[stable(feature = "wrapping", since = "1.7.0")]
1696+
#[must_use = "this returns the result of the operation, \
1697+
without modifying the original"]
1698+
#[cfg(bootstrap)]
1699+
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1700+
if self == Self::min_value() && rhs == -1 {
1701+
(0, true)
1702+
} else {
1703+
(self % rhs, false)
1704+
}
1705+
}
16751706

16761707
doc_comment! {
16771708
concat!("Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`.
@@ -3517,11 +3548,23 @@ Basic usage
35173548
#[stable(feature = "wrapping", since = "1.7.0")]
35183549
#[must_use = "this returns the result of the operation, \
35193550
without modifying the original"]
3520-
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
3551+
#[rustc_const_unstable(feature = "const_int_overflowing")]
3552+
#[cfg(not(bootstrap))]
3553+
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
35213554
(self / rhs, false)
35223555
}
35233556
}
35243557

3558+
/// No docs for bootstrap.
3559+
#[inline]
3560+
#[stable(feature = "wrapping", since = "1.7.0")]
3561+
#[must_use = "this returns the result of the operation, \
3562+
without modifying the original"]
3563+
#[cfg(bootstrap)]
3564+
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
3565+
(self / rhs, false)
3566+
}
3567+
35253568
doc_comment! {
35263569
concat!("Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
35273570
@@ -3576,11 +3619,23 @@ Basic usage
35763619
#[stable(feature = "wrapping", since = "1.7.0")]
35773620
#[must_use = "this returns the result of the operation, \
35783621
without modifying the original"]
3579-
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
3622+
#[rustc_const_unstable(feature = "const_int_overflowing")]
3623+
#[cfg(not(bootstrap))]
3624+
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
35803625
(self % rhs, false)
35813626
}
35823627
}
35833628

3629+
/// No docs for bootstrap.
3630+
#[inline]
3631+
#[stable(feature = "wrapping", since = "1.7.0")]
3632+
#[must_use = "this returns the result of the operation, \
3633+
without modifying the original"]
3634+
#[cfg(bootstrap)]
3635+
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
3636+
(self % rhs, false)
3637+
}
3638+
35843639
doc_comment! {
35853640
concat!("Calculates the remainder `self.rem_euclid(rhs)` as if by Euclidean division.
35863641

src/test/ui/consts/const-int-overflowing-rpass.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-pass
2+
#![feature(const_int_overflowing)]
23

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

26+
const DIV_A: (i8, bool) = 8i8.overflowing_div(2);
27+
const DIV_B: (i8, bool) = 8i8.overflowing_div(3);
28+
const DIV_C: (i8, bool) = i8::min_value().overflowing_div(-1i8);
29+
const DIV_D: (u8, bool) = 8u8.overflowing_div(2);
30+
const DIV_E: (u8, bool) = 8u8.overflowing_div(3);
31+
32+
const REM_A: (i8, bool) = 8i8.overflowing_rem(2);
33+
const REM_B: (i8, bool) = 8i8.overflowing_rem(3);
34+
const REM_C: (i8, bool) = i8::min_value().overflowing_rem(-1i8);
35+
const REM_D: (u8, bool) = 8u8.overflowing_rem(2);
36+
const REM_E: (u8, bool) = 8u8.overflowing_rem(3);
37+
2538
fn main() {
2639
assert_eq!(ADD_A, (7, false));
2740
assert_eq!(ADD_B, (0, true));
@@ -44,4 +57,16 @@ fn main() {
4457
assert_eq!(ABS_POS, (10, false));
4558
assert_eq!(ABS_NEG, (10, false));
4659
assert_eq!(ABS_MIN, (i32::min_value(), true));
60+
61+
assert_eq!(DIV_A, (4i8, false));
62+
assert_eq!(DIV_B, (2i8, false));
63+
assert_eq!(DIV_C, (i8::min_value(), true));
64+
assert_eq!(DIV_D, (4u8, false));
65+
assert_eq!(DIV_E, (2u8, false));
66+
67+
assert_eq!(REM_A, (0i8, false));
68+
assert_eq!(REM_B, (2i8, false));
69+
assert_eq!(REM_C, (0i8, true));
70+
assert_eq!(REM_D, (0u8, false));
71+
assert_eq!(REM_E, (2u8, false));
4772
}

0 commit comments

Comments
 (0)