Skip to content

Commit d436194

Browse files
committed
Avoid regression in derive(PartialOrd) for unit structs
1 parent 802938c commit d436194

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind, Safety, ast};
22
use rustc_expand::base::{Annotatable, ExtCtxt};
33
use rustc_span::{Ident, Span, sym};
4-
use thin_vec::thin_vec;
4+
use thin_vec::{ThinVec, thin_vec};
55

66
use crate::deriving::generic::ty::*;
77
use crate::deriving::generic::*;
@@ -41,33 +41,33 @@ pub(crate) fn expand_deriving_partial_ord(
4141
} else {
4242
true
4343
};
44-
let substructure = combine_substructure(Box::new(|cx, span, substr| {
44+
45+
let container_id = cx.current_expansion.id.expn_data().parent.expect_local();
46+
let has_derive_ord = cx.resolver.has_derive_ord(container_id);
47+
let is_simple_candidate = |params: &ThinVec<ast::GenericParam>| -> bool {
48+
has_derive_ord
49+
&& !params.iter().any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))
50+
};
51+
52+
let default_substructure = combine_substructure(Box::new(|cx, span, substr| {
4553
cs_partial_cmp(cx, span, substr, discr_then_data)
4654
}));
55+
let simple_substructure = combine_substructure(Box::new(|cx, span, _| {
56+
cs_partial_cmp_simple(cx, span, cx.expr_ident(span, Ident::new(sym::other, span)))
57+
}));
4758
let (is_simple, substructure) = match item {
4859
Annotatable::Item(annitem) => match &annitem.kind {
60+
// For unit structs, the default generated code is better.
61+
ItemKind::Struct(.., ast::VariantData::Unit(..)) => (false, default_substructure),
4962
ItemKind::Struct(_, ast::Generics { params, .. }, _)
5063
| ItemKind::Enum(_, ast::Generics { params, .. }, _)
51-
if let container_id = cx.current_expansion.id.expn_data().parent.expect_local()
52-
&& cx.resolver.has_derive_ord(container_id)
53-
&& !params
54-
.iter()
55-
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. })) =>
64+
if is_simple_candidate(params) =>
5665
{
57-
(
58-
true,
59-
combine_substructure(Box::new(|cx, span, _| {
60-
cs_partial_cmp_simple(
61-
cx,
62-
span,
63-
cx.expr_ident(span, Ident::new(sym::other, span)),
64-
)
65-
})),
66-
)
66+
(true, simple_substructure)
6767
}
68-
_ => (false, substructure),
68+
_ => (false, default_substructure),
6969
},
70-
_ => (false, substructure),
70+
_ => (false, default_substructure),
7171
};
7272

7373
let partial_cmp_def = MethodDef {

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

Lines changed: 2 additions & 2 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(::core::cmp::Ord::cmp(self, other))
75+
::core::option::Option::Some(::core::cmp::Ordering::Equal)
7676
}
7777
}
7878
#[automatically_derived]
@@ -1842,7 +1842,7 @@ impl ::core::cmp::PartialOrd for UnitStruct {
18421842
#[inline]
18431843
fn partial_cmp(&self, other: &UnitStruct)
18441844
-> ::core::option::Option<::core::cmp::Ordering> {
1845-
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
1845+
::core::option::Option::Some(::core::cmp::Ordering::Equal)
18461846
}
18471847
}
18481848
#[automatically_derived]

0 commit comments

Comments
 (0)