From 6b0d44e92a529962792d3a5f1e7b44b6e3c6ed05 Mon Sep 17 00:00:00 2001 From: CDirkx Date: Wed, 12 Aug 2020 22:10:32 +0200 Subject: [PATCH 1/4] Make some Ordering methods const Constify the following methods of `core::cmp::Ordering`: - `reverse` - `then` Possible because of #49146 (Allow `if` and `match` in constants). --- library/core/src/cmp.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index e775ded60f593..b66a6409b9cf8 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -357,7 +357,7 @@ impl Ordering { #[inline] #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - pub fn reverse(self) -> Ordering { + pub const fn reverse(self) -> Ordering { match self { Less => Greater, Equal => Equal, @@ -395,7 +395,7 @@ impl Ordering { #[inline] #[must_use] #[stable(feature = "ordering_chaining", since = "1.17.0")] - pub fn then(self, other: Ordering) -> Ordering { + pub const fn then(self, other: Ordering) -> Ordering { match self { Equal => other, _ => self, From 5fac991bf6bc4b07df9b4b4eb3fcb0c5487973c4 Mon Sep 17 00:00:00 2001 From: CDirkx Date: Sun, 30 Aug 2020 19:40:00 +0200 Subject: [PATCH 2/4] Add unstable `const_ordering` feature, and some tests. --- library/core/src/cmp.rs | 2 ++ library/core/src/lib.rs | 1 + src/test/ui/consts/const-ordering.rs | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 src/test/ui/consts/const-ordering.rs diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index b66a6409b9cf8..ddc9f7b8cbad7 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -356,6 +356,7 @@ impl Ordering { /// ``` #[inline] #[must_use] + #[rustc_const_unstable(feature = "const_ordering", issue = "76113")] #[stable(feature = "rust1", since = "1.0.0")] pub const fn reverse(self) -> Ordering { match self { @@ -394,6 +395,7 @@ impl Ordering { /// ``` #[inline] #[must_use] + #[rustc_const_unstable(feature = "const_ordering", issue = "76113")] #[stable(feature = "ordering_chaining", since = "1.17.0")] pub const fn then(self, other: Ordering) -> Ordering { match self { diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index fcf5454308b47..745c46e086841 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -86,6 +86,7 @@ #![feature(const_ptr_offset_from)] #![feature(const_raw_ptr_comparison)] #![feature(const_result)] +#![feature(const_ordering)] #![feature(const_slice_from_raw_parts)] #![feature(const_slice_ptr_len)] #![feature(const_size_of_val)] diff --git a/src/test/ui/consts/const-ordering.rs b/src/test/ui/consts/const-ordering.rs new file mode 100644 index 0000000000000..9feb2b27dadb6 --- /dev/null +++ b/src/test/ui/consts/const-ordering.rs @@ -0,0 +1,17 @@ +// run-pass + +#![feature(const_ordering)] + +use std::cmp::Ordering; + +// the following methods of core::cmp::Ordering are const: +// - reverse +// - then + +fn main() { + const REVERSE : Ordering = Ordering::Greater.reverse(); + assert_eq!(REVERSE, Ordering::Less); + + const THEN : Ordering = Ordering::Equal.then(REVERSE); + assert_eq!(THEN, Ordering::Less); +} From 89fc3fb93b0befd5515917289564a297c49bba6e Mon Sep 17 00:00:00 2001 From: CDirkx Date: Sun, 30 Aug 2020 23:48:54 +0200 Subject: [PATCH 3/4] Stabilize feature `const_ordering` --- library/core/src/cmp.rs | 4 ++-- library/core/src/lib.rs | 1 - src/test/ui/consts/const-ordering.rs | 2 -- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index ddc9f7b8cbad7..1060fc04071c2 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -356,7 +356,7 @@ impl Ordering { /// ``` #[inline] #[must_use] - #[rustc_const_unstable(feature = "const_ordering", issue = "76113")] + #[rustc_const_stable(feature = "const_ordering", since = "1.47.0")] #[stable(feature = "rust1", since = "1.0.0")] pub const fn reverse(self) -> Ordering { match self { @@ -395,7 +395,7 @@ impl Ordering { /// ``` #[inline] #[must_use] - #[rustc_const_unstable(feature = "const_ordering", issue = "76113")] + #[rustc_const_stable(feature = "const_ordering", since = "1.47.0")] #[stable(feature = "ordering_chaining", since = "1.17.0")] pub const fn then(self, other: Ordering) -> Ordering { match self { diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 745c46e086841..fcf5454308b47 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -86,7 +86,6 @@ #![feature(const_ptr_offset_from)] #![feature(const_raw_ptr_comparison)] #![feature(const_result)] -#![feature(const_ordering)] #![feature(const_slice_from_raw_parts)] #![feature(const_slice_ptr_len)] #![feature(const_size_of_val)] diff --git a/src/test/ui/consts/const-ordering.rs b/src/test/ui/consts/const-ordering.rs index 9feb2b27dadb6..454f2da00df9e 100644 --- a/src/test/ui/consts/const-ordering.rs +++ b/src/test/ui/consts/const-ordering.rs @@ -1,7 +1,5 @@ // run-pass -#![feature(const_ordering)] - use std::cmp::Ordering; // the following methods of core::cmp::Ordering are const: From 12f4624f71004692eae4e089276ce91db0d668d3 Mon Sep 17 00:00:00 2001 From: CDirkx Date: Sun, 30 Aug 2020 23:59:37 +0200 Subject: [PATCH 4/4] Update `since` to correct release `const_ordering` will stabilize in version 1.48.0 --- library/core/src/cmp.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 1060fc04071c2..e11c2a08d0ebf 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -356,7 +356,7 @@ impl Ordering { /// ``` #[inline] #[must_use] - #[rustc_const_stable(feature = "const_ordering", since = "1.47.0")] + #[rustc_const_stable(feature = "const_ordering", since = "1.48.0")] #[stable(feature = "rust1", since = "1.0.0")] pub const fn reverse(self) -> Ordering { match self { @@ -395,7 +395,7 @@ impl Ordering { /// ``` #[inline] #[must_use] - #[rustc_const_stable(feature = "const_ordering", since = "1.47.0")] + #[rustc_const_stable(feature = "const_ordering", since = "1.48.0")] #[stable(feature = "ordering_chaining", since = "1.17.0")] pub const fn then(self, other: Ordering) -> Ordering { match self {