|
1 | 1 | use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind, Safety, ast}; |
2 | 2 | use rustc_expand::base::{Annotatable, ExtCtxt}; |
3 | 3 | use rustc_span::{Ident, Span, sym}; |
4 | | -use thin_vec::thin_vec; |
| 4 | +use thin_vec::{ThinVec, thin_vec}; |
5 | 5 |
|
6 | 6 | use crate::deriving::generic::ty::*; |
7 | 7 | use crate::deriving::generic::*; |
@@ -41,33 +41,33 @@ pub(crate) fn expand_deriving_partial_ord( |
41 | 41 | } else { |
42 | 42 | true |
43 | 43 | }; |
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| { |
45 | 53 | cs_partial_cmp(cx, span, substr, discr_then_data) |
46 | 54 | })); |
| 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 | + })); |
47 | 58 | let (is_simple, substructure) = match item { |
48 | 59 | 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), |
49 | 62 | ItemKind::Struct(_, ast::Generics { params, .. }, _) |
50 | 63 | | 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) => |
56 | 65 | { |
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) |
67 | 67 | } |
68 | | - _ => (false, substructure), |
| 68 | + _ => (false, default_substructure), |
69 | 69 | }, |
70 | | - _ => (false, substructure), |
| 70 | + _ => (false, default_substructure), |
71 | 71 | }; |
72 | 72 |
|
73 | 73 | let partial_cmp_def = MethodDef { |
|
0 commit comments