Skip to content

Commit 12745ac

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 7abab1e + 2049287 commit 12745ac

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-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

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

0 commit comments

Comments
 (0)