rustc_builtin_macros/deriving/cmp/
partial_ord.rs

1use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind, Safety};
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        is_staged_api_crate: cx.ecfg.features.staged_api(),
68        safety: Safety::Default,
69        document: true,
70    };
71    trait_def.expand(cx, mitem, item, push)
72}
73
74fn cs_partial_cmp(
75    cx: &ExtCtxt<'_>,
76    span: Span,
77    substr: &Substructure<'_>,
78    discr_then_data: bool,
79) -> BlockOrExpr {
80    let test_id = Ident::new(sym::cmp, span);
81    let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
82    let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
83
84    // Builds:
85    //
86    // match ::core::cmp::PartialOrd::partial_cmp(&self.x, &other.x) {
87    //     ::core::option::Option::Some(::core::cmp::Ordering::Equal) =>
88    //         ::core::cmp::PartialOrd::partial_cmp(&self.y, &other.y),
89    //     cmp => cmp,
90    // }
91    let expr = cs_fold(
92        // foldr nests the if-elses correctly, leaving the first field
93        // as the outermost one, and the last as the innermost.
94        false,
95        cx,
96        span,
97        substr,
98        |cx, fold| match fold {
99            CsFold::Single(field) => {
100                let [other_expr] = &field.other_selflike_exprs[..] else {
101                    cx.dcx().span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
102                };
103                let args = thin_vec![field.self_expr.clone(), other_expr.clone()];
104                cx.expr_call_global(field.span, partial_cmp_path.clone(), args)
105            }
106            CsFold::Combine(span, mut expr1, expr2) => {
107                // When the item is an enum, this expands to
108                // ```
109                // match (expr2) {
110                //     Some(Ordering::Equal) => expr1,
111                //     cmp => cmp
112                // }
113                // ```
114                // where `expr2` is `partial_cmp(self_discr, other_discr)`, and `expr1` is a `match`
115                // against the enum variants. This means that we begin by comparing the enum discriminants,
116                // before either inspecting their contents (if they match), or returning
117                // the `cmp::Ordering` of comparing the enum discriminants.
118                // ```
119                // match partial_cmp(self_discr, other_discr) {
120                //     Some(Ordering::Equal) => match (self, other)  {
121                //         (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
122                //         (Self::B(self_0), Self::B(other_0)) => partial_cmp(self_0, other_0),
123                //         _ => Some(Ordering::Equal)
124                //     }
125                //     cmp => cmp
126                // }
127                // ```
128                // If we have any certain enum layouts, flipping this results in better codegen
129                // ```
130                // match (self, other) {
131                //     (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
132                //     _ => partial_cmp(self_discr, other_discr)
133                // }
134                // ```
135                // Reference: https://github.com/rust-lang/rust/pull/103659#issuecomment-1328126354
136
137                if !discr_then_data
138                    && let ExprKind::Match(_, arms, _) = &mut expr1.kind
139                    && let Some(last) = arms.last_mut()
140                    && let PatKind::Wild = last.pat.kind
141                {
142                    last.body = Some(expr2);
143                    expr1
144                } else {
145                    let eq_arm = cx.arm(
146                        span,
147                        cx.pat_some(span, cx.pat_path(span, equal_path.clone())),
148                        expr1,
149                    );
150                    let neq_arm =
151                        cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
152                    cx.expr_match(span, expr2, thin_vec![eq_arm, neq_arm])
153                }
154            }
155            CsFold::Fieldless => cx.expr_some(span, cx.expr_path(equal_path.clone())),
156        },
157    );
158    BlockOrExpr::new_expr(expr)
159}