Use UFCS for Ord::cmp

This commit is contained in:
Makai
2026-04-22 14:14:09 +08:00
parent c9a560b30d
commit 802938ca79
4 changed files with 122 additions and 21 deletions
@@ -100,15 +100,12 @@ pub(crate) fn expand_deriving_partial_ord(
// Special case for the type deriving both `PartialOrd` and `Ord`. Builds:
// ```
// Some(self.cmp(other))
// Some(::core::cmp::Ord::cmp(self, other))
// ```
fn cs_partial_cmp_simple(cx: &ExtCtxt<'_>, span: Span, other_expr: Box<ast::Expr>) -> BlockOrExpr {
let cmp_expr = cx.expr_method_call(
span,
cx.expr_self(span),
Ident::new(sym::cmp, span),
thin_vec![other_expr],
);
let ord_cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
let cmp_expr =
cx.expr_call_global(span, ord_cmp_path, thin_vec![cx.expr_self(span), other_expr]);
BlockOrExpr::new_expr(cx.expr_some(span, cmp_expr))
}
+18
View File
@@ -235,3 +235,21 @@ pub union Union {
#[derive(Clone)]
#[derive(Copy)]
struct FooCloneAndCopy(i32);
#[derive(PartialOrd, Ord)]
struct FooPartialOrdOrd(i32);
#[derive(Ord, PartialOrd)]
struct FooOrdPartialOrd(i32);
#[derive(Ord)]
#[derive(PartialOrd)]
struct FooOrdBeforePartialOrd(i32);
// FIXME: this case should also have a trivial `PartialOrd` impl.
#[derive(PartialOrd)]
#[derive(Ord)]
struct FooPartialOrdBeforeOrd(i32);
#[derive(PartialOrd, Ord)]
struct UnitStruct;
+99 -13
View File
@@ -72,7 +72,7 @@ impl ::core::cmp::PartialOrd for Empty {
#[inline]
fn partial_cmp(&self, other: &Empty)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -151,7 +151,7 @@ impl ::core::cmp::PartialOrd for Point {
#[inline]
fn partial_cmp(&self, other: &Point)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -235,7 +235,7 @@ impl ::core::cmp::PartialOrd for PackedPoint {
#[inline]
fn partial_cmp(&self, other: &PackedPoint)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -312,7 +312,7 @@ impl ::core::cmp::PartialOrd for TupleSingleField {
#[inline]
fn partial_cmp(&self, other: &TupleSingleField)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -387,7 +387,7 @@ impl ::core::cmp::PartialOrd for SingleField {
#[inline]
fn partial_cmp(&self, other: &SingleField)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -492,7 +492,7 @@ impl ::core::cmp::PartialOrd for Big {
#[inline]
fn partial_cmp(&self, other: &Big)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -716,7 +716,7 @@ impl ::core::cmp::PartialOrd for Unsized {
#[inline]
fn partial_cmp(&self, other: &Unsized)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -1016,7 +1016,7 @@ impl ::core::cmp::PartialOrd for Enum0 {
#[inline]
fn partial_cmp(&self, other: &Enum0)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -1088,7 +1088,7 @@ impl ::core::cmp::PartialOrd for Enum1 {
#[inline]
fn partial_cmp(&self, other: &Enum1)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -1149,7 +1149,7 @@ impl ::core::cmp::PartialOrd for Fieldless1 {
#[inline]
fn partial_cmp(&self, other: &Fieldless1)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -1226,7 +1226,7 @@ impl ::core::cmp::PartialOrd for Fieldless {
#[inline]
fn partial_cmp(&self, other: &Fieldless)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -1338,7 +1338,7 @@ impl ::core::cmp::PartialOrd for Mixed {
#[inline]
fn partial_cmp(&self, other: &Mixed)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -1520,7 +1520,7 @@ impl ::core::cmp::PartialOrd for Fielded {
#[inline]
fn partial_cmp(&self, other: &Fielded)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(self.cmp(other))
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
@@ -1766,3 +1766,89 @@ impl ::core::clone::Clone for FooCloneAndCopy {
FooCloneAndCopy(::core::clone::Clone::clone(&self.0))
}
}
struct FooPartialOrdOrd(i32);
#[automatically_derived]
impl ::core::cmp::PartialOrd for FooPartialOrdOrd {
#[inline]
fn partial_cmp(&self, other: &FooPartialOrdOrd)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
impl ::core::cmp::Ord for FooPartialOrdOrd {
#[inline]
fn cmp(&self, other: &FooPartialOrdOrd) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}
struct FooOrdPartialOrd(i32);
#[automatically_derived]
impl ::core::cmp::Ord for FooOrdPartialOrd {
#[inline]
fn cmp(&self, other: &FooOrdPartialOrd) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}
#[automatically_derived]
impl ::core::cmp::PartialOrd for FooOrdPartialOrd {
#[inline]
fn partial_cmp(&self, other: &FooOrdPartialOrd)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
struct FooOrdBeforePartialOrd(i32);
#[automatically_derived]
impl ::core::cmp::PartialOrd for FooOrdBeforePartialOrd {
#[inline]
fn partial_cmp(&self, other: &FooOrdBeforePartialOrd)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
impl ::core::cmp::Ord for FooOrdBeforePartialOrd {
#[inline]
fn cmp(&self, other: &FooOrdBeforePartialOrd) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}
// FIXME: this case should also have a trivial `PartialOrd` impl.
struct FooPartialOrdBeforeOrd(i32);
#[automatically_derived]
impl ::core::cmp::Ord for FooPartialOrdBeforeOrd {
#[inline]
fn cmp(&self, other: &FooPartialOrdBeforeOrd) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}
#[automatically_derived]
impl ::core::cmp::PartialOrd for FooPartialOrdBeforeOrd {
#[inline]
fn partial_cmp(&self, other: &FooPartialOrdBeforeOrd)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}
struct UnitStruct;
#[automatically_derived]
impl ::core::cmp::PartialOrd for UnitStruct {
#[inline]
fn partial_cmp(&self, other: &UnitStruct)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}
#[automatically_derived]
impl ::core::cmp::Ord for UnitStruct {
#[inline]
fn cmp(&self, other: &UnitStruct) -> ::core::cmp::Ordering {
::core::cmp::Ordering::Equal
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ macro-stats #[derive(Default)] 2 16 8.0
macro-stats #[derive(Eq)] 1 11 11.0 312 312.0
macro-stats #[derive(Debug)] 1 8 8.0 277 277.0
macro-stats #[derive(PartialEq)] 1 9 9.0 267 267.0
macro-stats #[derive(PartialOrd)] 1 8 8.0 235 235.0
macro-stats #[derive(PartialOrd)] 1 8 8.0 254 254.0
macro-stats #[derive(Copy)] 1 2 2.0 61 61.0
macro-stats p! 1 3 3.0 32 32.0
macro-stats trait_impl_tys! 1 2 2.0 28 28.0