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    // Order in which to perform matching
23    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            // No data, placing the discriminant check first makes codegen simpler
29            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    // Builds:
82    //
83    // match ::core::cmp::PartialOrd::partial_cmp(&self.x, &other.x) {
84    //     ::core::option::Option::Some(::core::cmp::Ordering::Equal) =>
85    //         ::core::cmp::PartialOrd::partial_cmp(&self.y, &other.y),
86    //     cmp => cmp,
87    // }
88    let expr = cs_fold(
89        // foldr nests the if-elses correctly, leaving the first field
90        // as the outermost one, and the last as the innermost.
91        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                // When the item is an enum, this expands to
105                // ```
106                // match (expr2) {
107                //     Some(Ordering::Equal) => expr1,
108                //     cmp => cmp
109                // }
110                // ```
111                // where `expr2` is `partial_cmp(self_discr, other_discr)`, and `expr1` is a `match`
112                // against the enum variants. This means that we begin by comparing the enum discriminants,
113                // before either inspecting their contents (if they match), or returning
114                // the `cmp::Ordering` of comparing the enum discriminants.
115                // ```
116                // match partial_cmp(self_discr, other_discr) {
117                //     Some(Ordering::Equal) => match (self, other)  {
118                //         (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
119                //         (Self::B(self_0), Self::B(other_0)) => partial_cmp(self_0, other_0),
120                //         _ => Some(Ordering::Equal)
121                //     }
122                //     cmp => cmp
123                // }
124                // ```
125                // If we have any certain enum layouts, flipping this results in better codegen
126                // ```
127                // match (self, other) {
128                //     (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
129                //     _ => partial_cmp(self_discr, other_discr)
130                // }
131                // ```
132                // Reference: https://github.com/rust-lang/rust/pull/103659#issuecomment-1328126354
133
134                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}