Skip to content

Commit 17ab2c4

Browse files
committed
Make wrapping int fns const
With rust-lang#49146 merged, these can be const; see rust-lang#53718.
1 parent 5e44a24 commit 17ab2c4

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

src/libcore/num/mod.rs

+53-5
Original file line numberDiff line numberDiff line change
@@ -1338,11 +1338,23 @@ $EndFeature, "
13381338
#[must_use = "this returns the result of the operation, \
13391339
without modifying the original"]
13401340
#[inline]
1341-
pub fn wrapping_div(self, rhs: Self) -> Self {
1341+
#[rustc_const_unstable(feature = "const_int_wrapping")]
1342+
#[cfg(not(bootstrap))]
1343+
pub const fn wrapping_div(self, rhs: Self) -> Self {
13421344
self.overflowing_div(rhs).0
13431345
}
13441346
}
13451347

1348+
/// No docs for bootstrap.
1349+
#[stable(feature = "num_wrapping", since = "1.2.0")]
1350+
#[must_use = "this returns the result of the operation, \
1351+
without modifying the original"]
1352+
#[inline]
1353+
#[cfg(bootstrap)]
1354+
pub fn wrapping_div(self, rhs: Self) -> Self {
1355+
self.overflowing_div(rhs).0
1356+
}
1357+
13461358
doc_comment! {
13471359
concat!("Wrapping Euclidean division. Computes `self.div_euclid(rhs)`,
13481360
wrapping around at the boundary of the type.
@@ -1397,11 +1409,23 @@ $EndFeature, "
13971409
#[must_use = "this returns the result of the operation, \
13981410
without modifying the original"]
13991411
#[inline]
1400-
pub fn wrapping_rem(self, rhs: Self) -> Self {
1412+
#[rustc_const_unstable(feature = "const_int_wrapping")]
1413+
#[cfg(not(bootstrap))]
1414+
pub const fn wrapping_rem(self, rhs: Self) -> Self {
14011415
self.overflowing_rem(rhs).0
14021416
}
14031417
}
14041418

1419+
/// No docs for bootstrap.
1420+
#[stable(feature = "num_wrapping", since = "1.2.0")]
1421+
#[must_use = "this returns the result of the operation, \
1422+
without modifying the original"]
1423+
#[inline]
1424+
#[cfg(bootstrap)]
1425+
pub fn wrapping_rem(self, rhs: Self) -> Self {
1426+
self.overflowing_rem(rhs).0
1427+
}
1428+
14051429
doc_comment! {
14061430
concat!("Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping around
14071431
at the boundary of the type.
@@ -3422,7 +3446,7 @@ $EndFeature, "
34223446
#[stable(feature = "rust1", since = "1.0.0")]
34233447
#[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
34243448
#[must_use = "this returns the result of the operation, \
3425-
without modifying the original"]
3449+
without modifying the original"]
34263450
#[inline]
34273451
pub const fn wrapping_mul(self, rhs: Self) -> Self {
34283452
intrinsics::wrapping_mul(self, rhs)
@@ -3446,11 +3470,23 @@ Basic usage:
34463470
#[must_use = "this returns the result of the operation, \
34473471
without modifying the original"]
34483472
#[inline]
3449-
pub fn wrapping_div(self, rhs: Self) -> Self {
3473+
#[rustc_const_unstable(feature = "const_int_wrapping")]
3474+
#[cfg(not(bootstrap))]
3475+
pub const fn wrapping_div(self, rhs: Self) -> Self {
34503476
self / rhs
34513477
}
34523478
}
34533479

3480+
/// No docs for bootstrap.
3481+
#[stable(feature = "num_wrapping", since = "1.2.0")]
3482+
#[must_use = "this returns the result of the operation, \
3483+
without modifying the original"]
3484+
#[inline]
3485+
#[cfg(bootstrap)]
3486+
pub fn wrapping_div(self, rhs: Self) -> Self {
3487+
self / rhs
3488+
}
3489+
34543490
doc_comment! {
34553491
concat!("Wrapping Euclidean division. Computes `self.div_euclid(rhs)`.
34563492
Wrapped division on unsigned types is just normal division.
@@ -3496,11 +3532,23 @@ Basic usage:
34963532
#[must_use = "this returns the result of the operation, \
34973533
without modifying the original"]
34983534
#[inline]
3499-
pub fn wrapping_rem(self, rhs: Self) -> Self {
3535+
#[rustc_const_unstable(feature = "const_int_wrapping")]
3536+
#[cfg(not(bootstrap))]
3537+
pub const fn wrapping_rem(self, rhs: Self) -> Self {
35003538
self % rhs
35013539
}
35023540
}
35033541

3542+
/// No docs for bootstrap.
3543+
#[stable(feature = "num_wrapping", since = "1.2.0")]
3544+
#[must_use = "this returns the result of the operation, \
3545+
without modifying the original"]
3546+
#[inline]
3547+
#[cfg(bootstrap)]
3548+
pub fn wrapping_rem(self, rhs: Self) -> Self {
3549+
self % rhs
3550+
}
3551+
35043552
doc_comment! {
35053553
concat!("Wrapping Euclidean modulo. Computes `self.rem_euclid(rhs)`.
35063554
Wrapped modulo calculation on unsigned types is

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

+25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-pass
2+
#![feature(const_int_wrapping)]
23

34
const ADD_A: u32 = 200u32.wrapping_add(55);
45
const ADD_B: u32 = 200u32.wrapping_add(u32::max_value());
@@ -22,6 +23,18 @@ const ABS_POS: i32 = 10i32.wrapping_abs();
2223
const ABS_NEG: i32 = (-10i32).wrapping_abs();
2324
const ABS_MIN: i32 = i32::min_value().wrapping_abs();
2425

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

0 commit comments

Comments
 (0)