Skip to content

Commit a1a17f5

Browse files
committed
Rollup merge of rust-lang#58044 - Lokathor:lokathor, r=alexcrichton
Make overflowing and wrapping negation const Remember that the signed and unsigned versions are slightly different here, so there's four functions made const instead of just two.
2 parents 8ca56e1 + e06302f commit a1a17f5

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/libcore/num/mod.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ $EndFeature, "
12151215
```"),
12161216
#[stable(feature = "num_wrapping", since = "1.2.0")]
12171217
#[inline]
1218-
pub fn wrapping_neg(self) -> Self {
1218+
pub const fn wrapping_neg(self) -> Self {
12191219
self.overflowing_neg().0
12201220
}
12211221
}
@@ -1569,12 +1569,8 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
15691569
```"),
15701570
#[inline]
15711571
#[stable(feature = "wrapping", since = "1.7.0")]
1572-
pub fn overflowing_neg(self) -> (Self, bool) {
1573-
if self == Self::min_value() {
1574-
(Self::min_value(), true)
1575-
} else {
1576-
(-self, false)
1577-
}
1572+
pub const fn overflowing_neg(self) -> (Self, bool) {
1573+
((!self).wrapping_add(1), self == Self::min_value())
15781574
}
15791575
}
15801576

@@ -3092,7 +3088,7 @@ assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);
30923088
/// ```
30933089
#[stable(feature = "num_wrapping", since = "1.2.0")]
30943090
#[inline]
3095-
pub fn wrapping_neg(self) -> Self {
3091+
pub const fn wrapping_neg(self) -> Self {
30963092
self.overflowing_neg().0
30973093
}
30983094

@@ -3397,7 +3393,7 @@ assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2i32 as ", stringify!(
33973393
```"),
33983394
#[inline]
33993395
#[stable(feature = "wrapping", since = "1.7.0")]
3400-
pub fn overflowing_neg(self) -> (Self, bool) {
3396+
pub const fn overflowing_neg(self) -> (Self, bool) {
34013397
((!self).wrapping_add(1), self != 0)
34023398
}
34033399
}

src/test/run-pass/const-int-overflowing.rs

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const SHL_B: (u32, bool) = 0x1u32.overflowing_shl(132);
1313
const SHR_A: (u32, bool) = 0x10u32.overflowing_shr(4);
1414
const SHR_B: (u32, bool) = 0x10u32.overflowing_shr(132);
1515

16+
const NEG_A: (u32, bool) = 0u32.overflowing_neg();
17+
const NEG_B: (u32, bool) = core::u32::MAX.overflowing_neg();
18+
1619
fn ident<T>(ident: T) -> T {
1720
ident
1821
}
@@ -32,4 +35,7 @@ fn main() {
3235

3336
assert_eq!(SHR_A, ident((0x1, false)));
3437
assert_eq!(SHR_B, ident((0x1, true)));
38+
39+
assert_eq!(NEG_A, ident((0, false)));
40+
assert_eq!(NEG_B, ident((1, true)));
3541
}

src/test/run-pass/const-int-wrapping.rs

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const SHL_B: u32 = 1u32.wrapping_shl(128);
1313
const SHR_A: u32 = 128u32.wrapping_shr(7);
1414
const SHR_B: u32 = 128u32.wrapping_shr(128);
1515

16+
const NEG_A: u32 = 5u32.wrapping_neg();
17+
const NEG_B: u32 = 1234567890u32.wrapping_neg();
18+
1619
fn ident<T>(ident: T) -> T {
1720
ident
1821
}
@@ -32,4 +35,7 @@ fn main() {
3235

3336
assert_eq!(SHR_A, ident(1));
3437
assert_eq!(SHR_B, ident(128));
38+
39+
assert_eq!(NEG_A, ident(4294967291));
40+
assert_eq!(NEG_B, ident(3060399406));
3541
}

0 commit comments

Comments
 (0)