Skip to content

Commit 802938c

Browse files
committed
Use UFCS for Ord::cmp
1 parent c9a560b commit 802938c

4 files changed

Lines changed: 122 additions & 21 deletions

File tree

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,12 @@ pub(crate) fn expand_deriving_partial_ord(
100100

101101
// Special case for the type deriving both `PartialOrd` and `Ord`. Builds:
102102
// ```
103-
// Some(self.cmp(other))
103+
// Some(::core::cmp::Ord::cmp(self, other))
104104
// ```
105105
fn cs_partial_cmp_simple(cx: &ExtCtxt<'_>, span: Span, other_expr: Box<ast::Expr>) -> BlockOrExpr {
106-
let cmp_expr = cx.expr_method_call(
107-
span,
108-
cx.expr_self(span),
109-
Ident::new(sym::cmp, span),
110-
thin_vec![other_expr],
111-
);
106+
let ord_cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
107+
let cmp_expr =
108+
cx.expr_call_global(span, ord_cmp_path, thin_vec![cx.expr_self(span), other_expr]);
112109
BlockOrExpr::new_expr(cx.expr_some(span, cmp_expr))
113110
}
114111

tests/ui/derives/deriving-all-codegen.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,21 @@ struct FooCopyAndClone(i32);
235235
#[derive(Clone)]
236236
#[derive(Copy)]
237237
struct FooCloneAndCopy(i32);
238+
239+
#[derive(PartialOrd, Ord)]
240+
struct FooPartialOrdOrd(i32);
241+
242+
#[derive(Ord, PartialOrd)]
243+
struct FooOrdPartialOrd(i32);
244+
245+
#[derive(Ord)]
246+
#[derive(PartialOrd)]
247+
struct FooOrdBeforePartialOrd(i32);
248+
249+
// FIXME: this case should also have a trivial `PartialOrd` impl.
250+
#[derive(PartialOrd)]
251+
#[derive(Ord)]
252+
struct FooPartialOrdBeforeOrd(i32);
253+
254+
#[derive(PartialOrd, Ord)]
255+
struct UnitStruct;

tests/ui/derives/deriving-all-codegen.stdout

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl ::core::cmp::PartialOrd for Empty {
7272
#[inline]
7373
fn partial_cmp(&self, other: &Empty)
7474
-> ::core::option::Option<::core::cmp::Ordering> {
75-
::core::option::Option::Some(self.cmp(other))
75+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
7676
}
7777
}
7878
#[automatically_derived]
@@ -151,7 +151,7 @@ impl ::core::cmp::PartialOrd for Point {
151151
#[inline]
152152
fn partial_cmp(&self, other: &Point)
153153
-> ::core::option::Option<::core::cmp::Ordering> {
154-
::core::option::Option::Some(self.cmp(other))
154+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
155155
}
156156
}
157157
#[automatically_derived]
@@ -235,7 +235,7 @@ impl ::core::cmp::PartialOrd for PackedPoint {
235235
#[inline]
236236
fn partial_cmp(&self, other: &PackedPoint)
237237
-> ::core::option::Option<::core::cmp::Ordering> {
238-
::core::option::Option::Some(self.cmp(other))
238+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
239239
}
240240
}
241241
#[automatically_derived]
@@ -312,7 +312,7 @@ impl ::core::cmp::PartialOrd for TupleSingleField {
312312
#[inline]
313313
fn partial_cmp(&self, other: &TupleSingleField)
314314
-> ::core::option::Option<::core::cmp::Ordering> {
315-
::core::option::Option::Some(self.cmp(other))
315+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
316316
}
317317
}
318318
#[automatically_derived]
@@ -387,7 +387,7 @@ impl ::core::cmp::PartialOrd for SingleField {
387387
#[inline]
388388
fn partial_cmp(&self, other: &SingleField)
389389
-> ::core::option::Option<::core::cmp::Ordering> {
390-
::core::option::Option::Some(self.cmp(other))
390+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
391391
}
392392
}
393393
#[automatically_derived]
@@ -492,7 +492,7 @@ impl ::core::cmp::PartialOrd for Big {
492492
#[inline]
493493
fn partial_cmp(&self, other: &Big)
494494
-> ::core::option::Option<::core::cmp::Ordering> {
495-
::core::option::Option::Some(self.cmp(other))
495+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
496496
}
497497
}
498498
#[automatically_derived]
@@ -716,7 +716,7 @@ impl ::core::cmp::PartialOrd for Unsized {
716716
#[inline]
717717
fn partial_cmp(&self, other: &Unsized)
718718
-> ::core::option::Option<::core::cmp::Ordering> {
719-
::core::option::Option::Some(self.cmp(other))
719+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
720720
}
721721
}
722722
#[automatically_derived]
@@ -1016,7 +1016,7 @@ impl ::core::cmp::PartialOrd for Enum0 {
10161016
#[inline]
10171017
fn partial_cmp(&self, other: &Enum0)
10181018
-> ::core::option::Option<::core::cmp::Ordering> {
1019-
::core::option::Option::Some(self.cmp(other))
1019+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
10201020
}
10211021
}
10221022
#[automatically_derived]
@@ -1088,7 +1088,7 @@ impl ::core::cmp::PartialOrd for Enum1 {
10881088
#[inline]
10891089
fn partial_cmp(&self, other: &Enum1)
10901090
-> ::core::option::Option<::core::cmp::Ordering> {
1091-
::core::option::Option::Some(self.cmp(other))
1091+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
10921092
}
10931093
}
10941094
#[automatically_derived]
@@ -1149,7 +1149,7 @@ impl ::core::cmp::PartialOrd for Fieldless1 {
11491149
#[inline]
11501150
fn partial_cmp(&self, other: &Fieldless1)
11511151
-> ::core::option::Option<::core::cmp::Ordering> {
1152-
::core::option::Option::Some(self.cmp(other))
1152+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
11531153
}
11541154
}
11551155
#[automatically_derived]
@@ -1226,7 +1226,7 @@ impl ::core::cmp::PartialOrd for Fieldless {
12261226
#[inline]
12271227
fn partial_cmp(&self, other: &Fieldless)
12281228
-> ::core::option::Option<::core::cmp::Ordering> {
1229-
::core::option::Option::Some(self.cmp(other))
1229+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
12301230
}
12311231
}
12321232
#[automatically_derived]
@@ -1338,7 +1338,7 @@ impl ::core::cmp::PartialOrd for Mixed {
13381338
#[inline]
13391339
fn partial_cmp(&self, other: &Mixed)
13401340
-> ::core::option::Option<::core::cmp::Ordering> {
1341-
::core::option::Option::Some(self.cmp(other))
1341+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
13421342
}
13431343
}
13441344
#[automatically_derived]
@@ -1520,7 +1520,7 @@ impl ::core::cmp::PartialOrd for Fielded {
15201520
#[inline]
15211521
fn partial_cmp(&self, other: &Fielded)
15221522
-> ::core::option::Option<::core::cmp::Ordering> {
1523-
::core::option::Option::Some(self.cmp(other))
1523+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
15241524
}
15251525
}
15261526
#[automatically_derived]
@@ -1766,3 +1766,89 @@ impl ::core::clone::Clone for FooCloneAndCopy {
17661766
FooCloneAndCopy(::core::clone::Clone::clone(&self.0))
17671767
}
17681768
}
1769+
1770+
struct FooPartialOrdOrd(i32);
1771+
#[automatically_derived]
1772+
impl ::core::cmp::PartialOrd for FooPartialOrdOrd {
1773+
#[inline]
1774+
fn partial_cmp(&self, other: &FooPartialOrdOrd)
1775+
-> ::core::option::Option<::core::cmp::Ordering> {
1776+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
1777+
}
1778+
}
1779+
#[automatically_derived]
1780+
impl ::core::cmp::Ord for FooPartialOrdOrd {
1781+
#[inline]
1782+
fn cmp(&self, other: &FooPartialOrdOrd) -> ::core::cmp::Ordering {
1783+
::core::cmp::Ord::cmp(&self.0, &other.0)
1784+
}
1785+
}
1786+
1787+
struct FooOrdPartialOrd(i32);
1788+
#[automatically_derived]
1789+
impl ::core::cmp::Ord for FooOrdPartialOrd {
1790+
#[inline]
1791+
fn cmp(&self, other: &FooOrdPartialOrd) -> ::core::cmp::Ordering {
1792+
::core::cmp::Ord::cmp(&self.0, &other.0)
1793+
}
1794+
}
1795+
#[automatically_derived]
1796+
impl ::core::cmp::PartialOrd for FooOrdPartialOrd {
1797+
#[inline]
1798+
fn partial_cmp(&self, other: &FooOrdPartialOrd)
1799+
-> ::core::option::Option<::core::cmp::Ordering> {
1800+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
1801+
}
1802+
}
1803+
1804+
struct FooOrdBeforePartialOrd(i32);
1805+
#[automatically_derived]
1806+
impl ::core::cmp::PartialOrd for FooOrdBeforePartialOrd {
1807+
#[inline]
1808+
fn partial_cmp(&self, other: &FooOrdBeforePartialOrd)
1809+
-> ::core::option::Option<::core::cmp::Ordering> {
1810+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
1811+
}
1812+
}
1813+
#[automatically_derived]
1814+
impl ::core::cmp::Ord for FooOrdBeforePartialOrd {
1815+
#[inline]
1816+
fn cmp(&self, other: &FooOrdBeforePartialOrd) -> ::core::cmp::Ordering {
1817+
::core::cmp::Ord::cmp(&self.0, &other.0)
1818+
}
1819+
}
1820+
1821+
// FIXME: this case should also have a trivial `PartialOrd` impl.
1822+
struct FooPartialOrdBeforeOrd(i32);
1823+
#[automatically_derived]
1824+
impl ::core::cmp::Ord for FooPartialOrdBeforeOrd {
1825+
#[inline]
1826+
fn cmp(&self, other: &FooPartialOrdBeforeOrd) -> ::core::cmp::Ordering {
1827+
::core::cmp::Ord::cmp(&self.0, &other.0)
1828+
}
1829+
}
1830+
#[automatically_derived]
1831+
impl ::core::cmp::PartialOrd for FooPartialOrdBeforeOrd {
1832+
#[inline]
1833+
fn partial_cmp(&self, other: &FooPartialOrdBeforeOrd)
1834+
-> ::core::option::Option<::core::cmp::Ordering> {
1835+
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
1836+
}
1837+
}
1838+
1839+
struct UnitStruct;
1840+
#[automatically_derived]
1841+
impl ::core::cmp::PartialOrd for UnitStruct {
1842+
#[inline]
1843+
fn partial_cmp(&self, other: &UnitStruct)
1844+
-> ::core::option::Option<::core::cmp::Ordering> {
1845+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
1846+
}
1847+
}
1848+
#[automatically_derived]
1849+
impl ::core::cmp::Ord for UnitStruct {
1850+
#[inline]
1851+
fn cmp(&self, other: &UnitStruct) -> ::core::cmp::Ordering {
1852+
::core::cmp::Ordering::Equal
1853+
}
1854+
}

tests/ui/stats/macro-stats.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ macro-stats #[derive(Default)] 2 16 8.0
1010
macro-stats #[derive(Eq)] 1 11 11.0 312 312.0
1111
macro-stats #[derive(Debug)] 1 8 8.0 277 277.0
1212
macro-stats #[derive(PartialEq)] 1 9 9.0 267 267.0
13-
macro-stats #[derive(PartialOrd)] 1 8 8.0 235 235.0
13+
macro-stats #[derive(PartialOrd)] 1 8 8.0 254 254.0
1414
macro-stats #[derive(Copy)] 1 2 2.0 61 61.0
1515
macro-stats p! 1 3 3.0 32 32.0
1616
macro-stats trait_impl_tys! 1 2 2.0 28 28.0

0 commit comments

Comments
 (0)