From 2f5d2455a43203bce98a808b8687798199f4f815 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Fri, 1 Feb 2019 01:31:52 -0700 Subject: [PATCH 1/6] 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. --- src/libcore/num/mod.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index f80f839282781..81aec346c93fc 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1175,7 +1175,7 @@ $EndFeature, " ```"), #[stable(feature = "num_wrapping", since = "1.2.0")] #[inline] - pub fn wrapping_neg(self) -> Self { + pub const fn wrapping_neg(self) -> Self { self.overflowing_neg().0 } } @@ -1529,12 +1529,8 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self ```"), #[inline] #[stable(feature = "wrapping", since = "1.7.0")] - pub fn overflowing_neg(self) -> (Self, bool) { - if self == Self::min_value() { - (Self::min_value(), true) - } else { - (-self, false) - } + pub const fn overflowing_neg(self) -> (Self, bool) { + ((self ^ -1).wrapping_add(1), s == $SelfT::min_value()) } } @@ -3017,7 +3013,7 @@ assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0); /// ``` #[stable(feature = "num_wrapping", since = "1.2.0")] #[inline] - pub fn wrapping_neg(self) -> Self { + pub const fn wrapping_neg(self) -> Self { self.overflowing_neg().0 } @@ -3322,7 +3318,7 @@ assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2i32 as ", stringify!( ```"), #[inline] #[stable(feature = "wrapping", since = "1.7.0")] - pub fn overflowing_neg(self) -> (Self, bool) { + pub const fn overflowing_neg(self) -> (Self, bool) { ((!self).wrapping_add(1), self != 0) } } From 26a354065c94c107a8fb98f1a48d730aa0502dbb Mon Sep 17 00:00:00 2001 From: Lokathor Date: Fri, 1 Feb 2019 01:50:11 -0700 Subject: [PATCH 2/6] Don't know why I wasn't using `self` properly there --- src/libcore/num/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 81aec346c93fc..16364faa8000d 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1530,7 +1530,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self #[inline] #[stable(feature = "wrapping", since = "1.7.0")] pub const fn overflowing_neg(self) -> (Self, bool) { - ((self ^ -1).wrapping_add(1), s == $SelfT::min_value()) + ((self ^ -1).wrapping_add(1), self == Self::min_value()) } } From 481b354c9719a538b21547fcf06b8f65a43da604 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Fri, 1 Feb 2019 18:43:32 -0700 Subject: [PATCH 3/6] Simplify the overflowing_neg expression --- src/libcore/num/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 16364faa8000d..2fbc5f7aa76cc 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1530,7 +1530,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self #[inline] #[stable(feature = "wrapping", since = "1.7.0")] pub const fn overflowing_neg(self) -> (Self, bool) { - ((self ^ -1).wrapping_add(1), self == Self::min_value()) + ((!self).wrapping_add(1), self == Self::min_value()) } } From 5256efb80002bfcdbef2c6f007e43935c7d11432 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Fri, 1 Feb 2019 18:50:21 -0700 Subject: [PATCH 4/6] const-int-overflowing.rs += overflowing_neg --- src/test/run-pass/const-int-overflowing.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/run-pass/const-int-overflowing.rs b/src/test/run-pass/const-int-overflowing.rs index 289b1236cf1e2..e8fd022e68296 100644 --- a/src/test/run-pass/const-int-overflowing.rs +++ b/src/test/run-pass/const-int-overflowing.rs @@ -13,6 +13,9 @@ const SHL_B: (u32, bool) = 0x1u32.overflowing_shl(132); const SHR_A: (u32, bool) = 0x10u32.overflowing_shr(4); const SHR_B: (u32, bool) = 0x10u32.overflowing_shr(132); +const NEG_A: (u32, bool) = 0.overflowing_neg(); +const NEG_B: (u32, bool) = core::u32::MAX.overflowing_neg(); + fn ident(ident: T) -> T { ident } @@ -32,4 +35,7 @@ fn main() { assert_eq!(SHR_A, ident((0x1, false))); assert_eq!(SHR_B, ident((0x1, true))); + + assert_eq!(NEG_A, ident((0, false))); + assert_eq!(NEG_B, ident((1, true))); } From 31bf7e1b51ebb932036dd632396cd98c1e115a6b Mon Sep 17 00:00:00 2001 From: Lokathor Date: Fri, 1 Feb 2019 18:55:38 -0700 Subject: [PATCH 5/6] const-int-wrapping.rs += wrapping_neg --- src/test/run-pass/const-int-wrapping.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/run-pass/const-int-wrapping.rs b/src/test/run-pass/const-int-wrapping.rs index 5ab712015dfc3..fe001b625817a 100644 --- a/src/test/run-pass/const-int-wrapping.rs +++ b/src/test/run-pass/const-int-wrapping.rs @@ -13,6 +13,9 @@ const SHL_B: u32 = 1u32.wrapping_shl(128); const SHR_A: u32 = 128u32.wrapping_shr(7); const SHR_B: u32 = 128u32.wrapping_shr(128); +const NEG_A: u32 = 5u32.wrapping_neg(); +const NEG_B: u32 = 1234567890u32.wrapping_neg(); + fn ident(ident: T) -> T { ident } @@ -30,6 +33,6 @@ fn main() { assert_eq!(SHL_A, ident(128)); assert_eq!(SHL_B, ident(1)); - assert_eq!(SHR_A, ident(1)); - assert_eq!(SHR_B, ident(128)); + assert_eq!(SHR_A, ident(4294967291)); + assert_eq!(SHR_B, ident(3060399406)); } From e06302fda90dedd814c15f0517b98416b11cc204 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Fri, 1 Feb 2019 20:07:26 -0700 Subject: [PATCH 6/6] fix the build errors --- src/test/run-pass/const-int-overflowing.rs | 2 +- src/test/run-pass/const-int-wrapping.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/run-pass/const-int-overflowing.rs b/src/test/run-pass/const-int-overflowing.rs index e8fd022e68296..82057868b73bb 100644 --- a/src/test/run-pass/const-int-overflowing.rs +++ b/src/test/run-pass/const-int-overflowing.rs @@ -13,7 +13,7 @@ const SHL_B: (u32, bool) = 0x1u32.overflowing_shl(132); const SHR_A: (u32, bool) = 0x10u32.overflowing_shr(4); const SHR_B: (u32, bool) = 0x10u32.overflowing_shr(132); -const NEG_A: (u32, bool) = 0.overflowing_neg(); +const NEG_A: (u32, bool) = 0u32.overflowing_neg(); const NEG_B: (u32, bool) = core::u32::MAX.overflowing_neg(); fn ident(ident: T) -> T { diff --git a/src/test/run-pass/const-int-wrapping.rs b/src/test/run-pass/const-int-wrapping.rs index fe001b625817a..140fd57ecb802 100644 --- a/src/test/run-pass/const-int-wrapping.rs +++ b/src/test/run-pass/const-int-wrapping.rs @@ -33,6 +33,9 @@ fn main() { assert_eq!(SHL_A, ident(128)); assert_eq!(SHL_B, ident(1)); - assert_eq!(SHR_A, ident(4294967291)); - assert_eq!(SHR_B, ident(3060399406)); + assert_eq!(SHR_A, ident(1)); + assert_eq!(SHR_B, ident(128)); + + assert_eq!(NEG_A, ident(4294967291)); + assert_eq!(NEG_B, ident(3060399406)); }