Skip to content

Commit af27a43

Browse files
authored
Rollup merge of rust-lang#91439 - ecstatic-morse:const-cmp-trait-default-methods, r=oli-obk
Mark defaulted `PartialEq`/`PartialOrd` methods as const WIthout it, `const` impls of these traits are unpleasant to write. I think this kind of change is allowed now. although it looks like it might require some Miri tweaks. Let's find out. r? `@fee1-dead`
2 parents 404c847 + dc18d50 commit af27a43

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

library/core/src/cmp.rs

+5
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ pub trait PartialEq<Rhs: ?Sized = Self> {
215215
#[inline]
216216
#[must_use]
217217
#[stable(feature = "rust1", since = "1.0.0")]
218+
#[default_method_body_is_const]
218219
fn ne(&self, other: &Rhs) -> bool {
219220
!self.eq(other)
220221
}
@@ -1031,6 +1032,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
10311032
#[inline]
10321033
#[must_use]
10331034
#[stable(feature = "rust1", since = "1.0.0")]
1035+
#[default_method_body_is_const]
10341036
fn lt(&self, other: &Rhs) -> bool {
10351037
matches!(self.partial_cmp(other), Some(Less))
10361038
}
@@ -1050,6 +1052,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
10501052
#[inline]
10511053
#[must_use]
10521054
#[stable(feature = "rust1", since = "1.0.0")]
1055+
#[default_method_body_is_const]
10531056
fn le(&self, other: &Rhs) -> bool {
10541057
// Pattern `Some(Less | Eq)` optimizes worse than negating `None | Some(Greater)`.
10551058
// FIXME: The root cause was fixed upstream in LLVM with:
@@ -1072,6 +1075,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
10721075
#[inline]
10731076
#[must_use]
10741077
#[stable(feature = "rust1", since = "1.0.0")]
1078+
#[default_method_body_is_const]
10751079
fn gt(&self, other: &Rhs) -> bool {
10761080
matches!(self.partial_cmp(other), Some(Greater))
10771081
}
@@ -1091,6 +1095,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
10911095
#[inline]
10921096
#[must_use]
10931097
#[stable(feature = "rust1", since = "1.0.0")]
1098+
#[default_method_body_is_const]
10941099
fn ge(&self, other: &Rhs) -> bool {
10951100
matches!(self.partial_cmp(other), Some(Greater | Equal))
10961101
}

library/core/tests/cmp.rs

+28
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,31 @@ fn cmp_default() {
203203
assert!(Fool(false) != Fool(false));
204204
assert_eq!(Fool(false), Fool(true));
205205
}
206+
207+
struct S(i32);
208+
209+
impl const PartialEq for S {
210+
fn eq(&self, other: &Self) -> bool {
211+
self.0 == other.0
212+
}
213+
}
214+
215+
impl const PartialOrd for S {
216+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
217+
let ret = match (self.0, other.0) {
218+
(a, b) if a > b => Ordering::Greater,
219+
(a, b) if a < b => Ordering::Less,
220+
_ => Ordering::Equal,
221+
};
222+
223+
Some(ret)
224+
}
225+
}
226+
227+
const _: () = assert!(S(1) == S(1));
228+
const _: () = assert!(S(0) != S(1));
229+
230+
const _: () = assert!(S(1) <= S(1));
231+
const _: () = assert!(S(1) >= S(1));
232+
const _: () = assert!(S(0) < S(1));
233+
const _: () = assert!(S(1) > S(0));

0 commit comments

Comments
 (0)