rustc_builtin_macros/deriving/cmp/
partial_ord.rs
1use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind};
2use rustc_expand::base::{Annotatable, ExtCtxt};
3use rustc_span::{Ident, Span, sym};
4use thin_vec::thin_vec;
5
6use crate::deriving::generic::ty::*;
7use crate::deriving::generic::*;
8use crate::deriving::{path_std, pathvec_std};
9
10pub(crate) fn expand_deriving_partial_ord(
11 cx: &ExtCtxt<'_>,
12 span: Span,
13 mitem: &MetaItem,
14 item: &Annotatable,
15 push: &mut dyn FnMut(Annotatable),
16 is_const: bool,
17) {
18 let ordering_ty = Path(path_std!(cmp::Ordering));
19 let ret_ty =
20 Path(Path::new_(pathvec_std!(option::Option), vec![Box::new(ordering_ty)], PathKind::Std));
21
22 let discr_then_data = if let Annotatable::Item(item) = item
24 && let ItemKind::Enum(def, _) = &item.kind
25 {
26 let dataful: Vec<bool> = def.variants.iter().map(|v| !v.data.fields().is_empty()).collect();
27 match dataful.iter().filter(|&&b| b).count() {
28 0 => true,
30 1..=2 => false,
31 _ => (0..dataful.len() - 1).any(|i| {
32 if dataful[i]
33 && let Some(idx) = dataful[i + 1..].iter().position(|v| *v)
34 {
35 idx >= 2
36 } else {
37 false
38 }
39 }),
40 }
41 } else {
42 true
43 };
44 let partial_cmp_def = MethodDef {
45 name: sym::partial_cmp,
46 generics: Bounds::empty(),
47 explicit_self: true,
48 nonself_args: vec![(self_ref(), sym::other)],
49 ret_ty,
50 attributes: thin_vec![cx.attr_word(sym::inline, span)],
51 fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
52 combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
53 cs_partial_cmp(cx, span, substr, discr_then_data)
54 })),
55 };
56
57 let trait_def = TraitDef {
58 span,
59 path: path_std!(cmp::PartialOrd),
60 skip_path_as_bound: false,
61 needs_copy_as_bound_if_packed: true,
62 additional_bounds: vec![],
63 supports_unions: false,
64 methods: vec![partial_cmp_def],
65 associated_types: Vec::new(),
66 is_const,
67 };
68 trait_def.expand(cx, mitem, item, push)
69}
70
71fn cs_partial_cmp(
72 cx: &ExtCtxt<'_>,
73 span: Span,
74 substr: &Substructure<'_>,
75 discr_then_data: bool,
76) -> BlockOrExpr {
77 let test_id = Ident::new(sym::cmp, span);
78 let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
79 let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
80
81 let expr = cs_fold(
89 false,
92 cx,
93 span,
94 substr,
95 |cx, fold| match fold {
96 CsFold::Single(field) => {
97 let [other_expr] = &field.other_selflike_exprs[..] else {
98 cx.dcx().span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
99 };
100 let args = thin_vec![field.self_expr.clone(), other_expr.clone()];
101 cx.expr_call_global(field.span, partial_cmp_path.clone(), args)
102 }
103 CsFold::Combine(span, mut expr1, expr2) => {
104 if !discr_then_data
135 && let ExprKind::Match(_, arms, _) = &mut expr1.kind
136 && let Some(last) = arms.last_mut()
137 && let PatKind::Wild = last.pat.kind
138 {
139 last.body = Some(expr2);
140 expr1
141 } else {
142 let eq_arm = cx.arm(
143 span,
144 cx.pat_some(span, cx.pat_path(span, equal_path.clone())),
145 expr1,
146 );
147 let neq_arm =
148 cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
149 cx.expr_match(span, expr2, thin_vec![eq_arm, neq_arm])
150 }
151 }
152 CsFold::Fieldless => cx.expr_some(span, cx.expr_path(equal_path.clone())),
153 },
154 );
155 BlockOrExpr::new_expr(expr)
156}