]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
Auto merge of #107443 - cjgillot:generator-less-query, r=compiler-errors
[rust.git] / compiler / rustc_builtin_macros / src / deriving / cmp / partial_ord.rs
1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::{path_std, pathvec_std};
4 use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind};
5 use rustc_expand::base::{Annotatable, ExtCtxt};
6 use rustc_span::symbol::{sym, Ident};
7 use rustc_span::Span;
8 use thin_vec::thin_vec;
9
10 pub fn expand_deriving_partial_ord(
11     cx: &mut 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 attrs = thin_vec![cx.attr_word(sym::inline, span)];
23
24     // Order in which to perform matching
25     let tag_then_data = if let Annotatable::Item(item) = item
26         && let ItemKind::Enum(def, _) = &item.kind {
27             let dataful: Vec<bool> = def.variants.iter().map(|v| !v.data.fields().is_empty()).collect();
28             match dataful.iter().filter(|&&b| b).count() {
29                 // No data, placing the tag check first makes codegen simpler
30                 0 => true,
31                 1..=2 => false,
32                 _ => {
33                     (0..dataful.len()-1).any(|i| {
34                         if dataful[i] && let Some(idx) = dataful[i+1..].iter().position(|v| *v) {
35                             idx >= 2
36                         } else {
37                             false
38                         }
39                     })
40                 }
41             }
42         } else {
43             true
44         };
45     let partial_cmp_def = MethodDef {
46         name: sym::partial_cmp,
47         generics: Bounds::empty(),
48         explicit_self: true,
49         nonself_args: vec![(self_ref(), sym::other)],
50         ret_ty,
51         attributes: attrs,
52         fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
53         combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
54             cs_partial_cmp(cx, span, substr, tag_then_data)
55         })),
56     };
57
58     let trait_def = TraitDef {
59         span,
60         path: path_std!(cmp::PartialOrd),
61         skip_path_as_bound: false,
62         needs_copy_as_bound_if_packed: true,
63         additional_bounds: vec![],
64         supports_unions: false,
65         methods: vec![partial_cmp_def],
66         associated_types: Vec::new(),
67         is_const,
68     };
69     trait_def.expand(cx, mitem, item, push)
70 }
71
72 fn cs_partial_cmp(
73     cx: &mut ExtCtxt<'_>,
74     span: Span,
75     substr: &Substructure<'_>,
76     tag_then_data: bool,
77 ) -> BlockOrExpr {
78     let test_id = Ident::new(sym::cmp, span);
79     let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
80     let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
81
82     // Builds:
83     //
84     // match ::core::cmp::PartialOrd::partial_cmp(&self.x, &other.x) {
85     //     ::core::option::Option::Some(::core::cmp::Ordering::Equal) =>
86     //         ::core::cmp::PartialOrd::partial_cmp(&self.y, &other.y),
87     //     cmp => cmp,
88     // }
89     let expr = cs_fold(
90         // foldr nests the if-elses correctly, leaving the first field
91         // as the outermost one, and the last as the innermost.
92         false,
93         cx,
94         span,
95         substr,
96         |cx, fold| match fold {
97             CsFold::Single(field) => {
98                 let [other_expr] = &field.other_selflike_exprs[..] else {
99                         cx.span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
100                     };
101                 let args = vec![field.self_expr.clone(), other_expr.clone()];
102                 cx.expr_call_global(field.span, partial_cmp_path.clone(), args)
103             }
104             CsFold::Combine(span, mut expr1, expr2) => {
105                 // When the item is an enum, this expands to
106                 // ```
107                 // match (expr2) {
108                 //     Some(Ordering::Equal) => expr1,
109                 //     cmp => cmp
110                 // }
111                 // ```
112                 // where `expr2` is `partial_cmp(self_tag, other_tag)`, and `expr1` is a `match`
113                 //  against the enum variants. This means that we begin by comparing the enum tags,
114                 // before either inspecting their contents (if they match), or returning
115                 // the `cmp::Ordering` of comparing the enum tags.
116                 // ```
117                 // match partial_cmp(self_tag, other_tag) {
118                 //     Some(Ordering::Equal) => match (self, other)  {
119                 //         (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
120                 //         (Self::B(self_0), Self::B(other_0)) => partial_cmp(self_0, other_0),
121                 //         _ => Some(Ordering::Equal)
122                 //     }
123                 //     cmp => cmp
124                 // }
125                 // ```
126                 // If we have any certain enum layouts, flipping this results in better codegen
127                 // ```
128                 // match (self, other) {
129                 //     (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
130                 //     _ => partial_cmp(self_tag, other_tag)
131                 // }
132                 // ```
133                 // Reference: https://github.com/rust-lang/rust/pull/103659#issuecomment-1328126354
134
135                 if !tag_then_data
136                     && let ExprKind::Match(_, arms) = &mut expr1.kind
137                     && let Some(last) = arms.last_mut()
138                     && let PatKind::Wild = last.pat.kind {
139                         last.body = expr2;
140                         expr1
141                 } else {
142                     let eq_arm =
143                         cx.arm(span, cx.pat_some(span, cx.pat_path(span, equal_path.clone())), expr1);
144                     let neq_arm =
145                         cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
146                     cx.expr_match(span, expr2, vec![eq_arm, neq_arm])
147                 }
148             }
149             CsFold::Fieldless => cx.expr_some(span, cx.expr_path(equal_path.clone())),
150         },
151     );
152     BlockOrExpr::new_expr(expr)
153 }