rustc_builtin_macros/deriving/cmp/
ord.rs

1use rustc_ast::MetaItem;
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;
9
10pub(crate) fn expand_deriving_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 trait_def = TraitDef {
19        span,
20        path: path_std!(cmp::Ord),
21        skip_path_as_bound: false,
22        needs_copy_as_bound_if_packed: true,
23        additional_bounds: Vec::new(),
24        supports_unions: false,
25        methods: vec![MethodDef {
26            name: sym::cmp,
27            generics: Bounds::empty(),
28            explicit_self: true,
29            nonself_args: vec![(self_ref(), sym::other)],
30            ret_ty: Path(path_std!(cmp::Ordering)),
31            attributes: thin_vec![cx.attr_word(sym::inline, span)],
32            fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
33            combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
34        }],
35        associated_types: Vec::new(),
36        is_const,
37        is_staged_api_crate: cx.ecfg.features.staged_api(),
38    };
39
40    trait_def.expand(cx, mitem, item, push)
41}
42
43pub(crate) fn cs_cmp(cx: &ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
44    let test_id = Ident::new(sym::cmp, span);
45    let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
46    let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
47
48    // Builds:
49    //
50    // match ::core::cmp::Ord::cmp(&self.x, &other.x) {
51    //     ::std::cmp::Ordering::Equal =>
52    //         ::core::cmp::Ord::cmp(&self.y, &other.y),
53    //     cmp => cmp,
54    // }
55    let expr = cs_fold(
56        // foldr nests the if-elses correctly, leaving the first field
57        // as the outermost one, and the last as the innermost.
58        false,
59        cx,
60        span,
61        substr,
62        |cx, fold| match fold {
63            CsFold::Single(field) => {
64                let [other_expr] = &field.other_selflike_exprs[..] else {
65                    cx.dcx().span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
66                };
67                let args = thin_vec![field.self_expr.clone(), other_expr.clone()];
68                cx.expr_call_global(field.span, cmp_path.clone(), args)
69            }
70            CsFold::Combine(span, expr1, expr2) => {
71                let eq_arm = cx.arm(span, cx.pat_path(span, equal_path.clone()), expr1);
72                let neq_arm =
73                    cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
74                cx.expr_match(span, expr2, thin_vec![eq_arm, neq_arm])
75            }
76            CsFold::Fieldless => cx.expr_path(equal_path.clone()),
77        },
78    );
79    BlockOrExpr::new_expr(expr)
80}