From ea5dc0909ea1ed4135f1bdecbaa3423051be578d Mon Sep 17 00:00:00 2001 From: CDirkx Date: Tue, 1 Sep 2020 15:44:00 +0200 Subject: [PATCH 1/2] Make some Ordering methods const Constify the following methods of `core::cmp::Ordering`: - `reverse` - `then` Stabilizes these methods as const under the `const_ordering` feature. Also adds a test for these methods in a const context. Possible because of #49146 (Allow `if` and `match` in constants). --- library/core/src/cmp.rs | 6 ++++-- src/test/ui/consts/const-ordering.rs | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) 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 3953c73319fe4..dde442aa7b52d 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -356,8 +356,9 @@ impl Ordering { /// ``` #[inline] #[must_use] + #[rustc_const_stable(feature = "const_ordering", since = "1.48.0")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn reverse(self) -> Ordering { + pub const fn reverse(self) -> Ordering { match self { Less => Greater, Equal => Equal, @@ -394,8 +395,9 @@ impl Ordering { /// ``` #[inline] #[must_use] + #[rustc_const_stable(feature = "const_ordering", since = "1.48.0")] #[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, diff --git a/src/test/ui/consts/const-ordering.rs b/src/test/ui/consts/const-ordering.rs new file mode 100644 index 0000000000000..454f2da00df9e --- /dev/null +++ b/src/test/ui/consts/const-ordering.rs @@ -0,0 +1,15 @@ +// run-pass + +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 79d563c819483eaf6e67b6aaaef9d0ca6030337d Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Fri, 4 Sep 2020 00:40:11 +0200 Subject: [PATCH 2/2] Move const tests for `Ordering` to `library\core` Part of #76268 --- library/core/tests/cmp.rs | 18 +++++++++++++++++- src/test/ui/consts/const-ordering.rs | 15 --------------- 2 files changed, 17 insertions(+), 16 deletions(-) delete mode 100644 src/test/ui/consts/const-ordering.rs diff --git a/library/core/tests/cmp.rs b/library/core/tests/cmp.rs index 4086917780fa4..835289daf715a 100644 --- a/library/core/tests/cmp.rs +++ b/library/core/tests/cmp.rs @@ -1,4 +1,7 @@ -use core::cmp::{self, Ordering::*}; +use core::cmp::{ + self, + Ordering::{self, *}, +}; #[test] fn test_int_totalord() { @@ -116,3 +119,16 @@ fn test_user_defined_eq() { assert!(SketchyNum { num: 37 } == SketchyNum { num: 34 }); assert!(SketchyNum { num: 25 } != SketchyNum { num: 57 }); } + +#[test] +fn ordering_const() { + // test that the methods of `Ordering` are usable in a const context + + const ORDERING: Ordering = Greater; + + const REVERSE: Ordering = ORDERING.reverse(); + assert_eq!(REVERSE, Less); + + const THEN: Ordering = Equal.then(ORDERING); + assert_eq!(THEN, Greater); +} diff --git a/src/test/ui/consts/const-ordering.rs b/src/test/ui/consts/const-ordering.rs deleted file mode 100644 index 454f2da00df9e..0000000000000 --- a/src/test/ui/consts/const-ordering.rs +++ /dev/null @@ -1,15 +0,0 @@ -// run-pass - -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); -}