From d02cab1e18b870de3ba378579ec464d4a2381af3 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 31 Mar 2023 13:16:58 +0000 Subject: [PATCH 1/3] Revert "Constify cmp_min_max_by" This reverts commit 2e7a201d2e9e73477f1e69240711bfe09aefdc18. --- library/core/src/cmp.rs | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 5b5f55d0e65b0..85aa1de100aef 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -1184,12 +1184,7 @@ pub const fn min(v1: T, v2: T) -> T { #[inline] #[must_use] #[stable(feature = "cmp_min_max_by", since = "1.53.0")] -#[rustc_const_unstable(feature = "const_cmp", issue = "92391")] -pub const fn min_by Ordering>(v1: T, v2: T, compare: F) -> T -where - T: ~const Destruct, - F: ~const Destruct, -{ +pub fn min_by Ordering>(v1: T, v2: T, compare: F) -> T { match compare(&v1, &v2) { Ordering::Less | Ordering::Equal => v1, Ordering::Greater => v2, @@ -1211,14 +1206,8 @@ where #[inline] #[must_use] #[stable(feature = "cmp_min_max_by", since = "1.53.0")] -#[rustc_const_unstable(feature = "const_cmp", issue = "92391")] -pub const fn min_by_key K, K: ~const Ord>(v1: T, v2: T, mut f: F) -> T -where - T: ~const Destruct, - F: ~const Destruct, - K: ~const Destruct, -{ - min_by(v1, v2, const |v1, v2| f(v1).cmp(&f(v2))) +pub fn min_by_key K, K: Ord>(v1: T, v2: T, mut f: F) -> T { + min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2))) } /// Compares and returns the maximum of two values. @@ -1259,12 +1248,7 @@ pub const fn max(v1: T, v2: T) -> T { #[inline] #[must_use] #[stable(feature = "cmp_min_max_by", since = "1.53.0")] -#[rustc_const_unstable(feature = "const_cmp", issue = "92391")] -pub const fn max_by Ordering>(v1: T, v2: T, compare: F) -> T -where - T: ~const Destruct, - F: ~const Destruct, -{ +pub fn max_by Ordering>(v1: T, v2: T, compare: F) -> T { match compare(&v1, &v2) { Ordering::Less | Ordering::Equal => v2, Ordering::Greater => v1, @@ -1286,14 +1270,8 @@ where #[inline] #[must_use] #[stable(feature = "cmp_min_max_by", since = "1.53.0")] -#[rustc_const_unstable(feature = "const_cmp", issue = "92391")] -pub const fn max_by_key K, K: ~const Ord>(v1: T, v2: T, mut f: F) -> T -where - T: ~const Destruct, - F: ~const Destruct, - K: ~const Destruct, -{ - max_by(v1, v2, const |v1, v2| f(v1).cmp(&f(v2))) +pub fn max_by_key K, K: Ord>(v1: T, v2: T, mut f: F) -> T { + max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2))) } // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types From de08218f24998eb977170723dc0e334798494d85 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 31 Mar 2023 13:51:17 +0000 Subject: [PATCH 2/3] Partially revert 5b08c9f39754039ef9c6cbde157ac9eb8c252a58 --- library/core/src/cmp.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 85aa1de100aef..068637d1a0356 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -800,7 +800,10 @@ pub trait Ord: Eq + PartialOrd { Self: Sized, Self: ~const Destruct, { - max_by(self, other, Ord::cmp) + match self.cmp(&other) { + Ordering::Less | Ordering::Equal => other, + Ordering::Greater => self, + } } /// Compares and returns the minimum of two values. @@ -821,7 +824,10 @@ pub trait Ord: Eq + PartialOrd { Self: Sized, Self: ~const Destruct, { - min_by(self, other, Ord::cmp) + match self.cmp(&other) { + Ordering::Less | Ordering::Equal => self, + Ordering::Greater => other, + } } /// Restrict a value to a certain interval. From 7d891166f9f3f3bf084209b4e8fd5caedd2c33a3 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 31 Mar 2023 14:01:48 +0000 Subject: [PATCH 3/3] Add regression test --- .../function-pointer-does-not-require-const.rs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/ui/rfc-2632-const-trait-impl/function-pointer-does-not-require-const.rs diff --git a/tests/ui/rfc-2632-const-trait-impl/function-pointer-does-not-require-const.rs b/tests/ui/rfc-2632-const-trait-impl/function-pointer-does-not-require-const.rs new file mode 100644 index 0000000000000..1726cf82e2b26 --- /dev/null +++ b/tests/ui/rfc-2632-const-trait-impl/function-pointer-does-not-require-const.rs @@ -0,0 +1,8 @@ +// check-pass + +type I32Cmp = fn(&i32, &i32) -> core::cmp::Ordering; +pub const fn min_by_i32() -> fn(i32, i32, I32Cmp) -> i32 { + core::cmp::min_by +} + +fn main() {}