From 802938ca79a3442ed85e6a01f80db6b718d0bfcf Mon Sep 17 00:00:00 2001 From: Makai Date: Wed, 22 Apr 2026 14:14:09 +0800 Subject: [PATCH] Use UFCS for `Ord::cmp` --- .../src/deriving/cmp/partial_ord.rs | 11 +- tests/ui/derives/deriving-all-codegen.rs | 18 +++ tests/ui/derives/deriving-all-codegen.stdout | 112 ++++++++++++++++-- tests/ui/stats/macro-stats.stderr | 2 +- 4 files changed, 122 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs index 57387e2407f4..fcb6cb605e15 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs @@ -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) -> 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)) } diff --git a/tests/ui/derives/deriving-all-codegen.rs b/tests/ui/derives/deriving-all-codegen.rs index 9f2183196049..343d4095da47 100644 --- a/tests/ui/derives/deriving-all-codegen.rs +++ b/tests/ui/derives/deriving-all-codegen.rs @@ -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; diff --git a/tests/ui/derives/deriving-all-codegen.stdout b/tests/ui/derives/deriving-all-codegen.stdout index 6b65aff93625..7221f83ce32f 100644 --- a/tests/ui/derives/deriving-all-codegen.stdout +++ b/tests/ui/derives/deriving-all-codegen.stdout @@ -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 + } +} diff --git a/tests/ui/stats/macro-stats.stderr b/tests/ui/stats/macro-stats.stderr index ba2f840f7259..c70895067eb5 100644 --- a/tests/ui/stats/macro-stats.stderr +++ b/tests/ui/stats/macro-stats.stderr @@ -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