Skip to main content

rustc_builtin_macros/deriving/cmp/
partial_ord.rs

1use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind, Safety, ast};
2use rustc_expand::base::{Annotatable, ExtCtxt};
3use rustc_span::{Ident, Span, sym};
4use thin_vec::{ThinVec, 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(generic::ty::Path::new({
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [sym::cmp, sym::Ordering]))
    })path_std!(cmp::Ordering));
19    let ret_ty =
20        Path(Path::new_({
    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
            [sym::option, sym::Option]))
}pathvec_std!(option::Option), ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [Box::new(ordering_ty)]))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
45    let container_id = cx.current_expansion.id.expn_data().parent.expect_local();
46    let has_derive_ord = cx.resolver.has_derive_ord(container_id);
47    let is_simple_candidate = |params: &ThinVec<ast::GenericParam>| -> bool {
48        has_derive_ord
49            && !params.iter().any(|param| #[allow(non_exhaustive_omitted_patterns)] match param.kind {
    ast::GenericParamKind::Type { .. } => true,
    _ => false,
}matches!(param.kind, ast::GenericParamKind::Type { .. }))
50    };
51
52    let default_substructure = combine_substructure(Box::new(|cx, span, substr| {
53        cs_partial_cmp(cx, span, substr, discr_then_data)
54    }));
55    let simple_substructure = combine_substructure(Box::new(|cx, span, _| {
56        cs_partial_cmp_simple(cx, span, cx.expr_ident(span, Ident::new(sym::other, span)))
57    }));
58    let (is_simple, substructure) = match item {
59        Annotatable::Item(annitem) => match &annitem.kind {
60            // For unit structs/zero-variant enums, the default generated code is better.
61            ItemKind::Struct(.., ast::VariantData::Unit(..)) => (false, default_substructure),
62            // Also for single fieldless variant enum
63            ItemKind::Enum(.., enum_def) if enum_def.variants.is_empty() => {
64                (false, default_substructure)
65            }
66            ItemKind::Enum(.., enum_def)
67                if enum_def.variants.len() == 1
68                    && #[allow(non_exhaustive_omitted_patterns)] match enum_def.variants[0].data {
    ast::VariantData::Unit(..) => true,
    _ => false,
}matches!(enum_def.variants[0].data, ast::VariantData::Unit(..)) =>
69            {
70                (false, default_substructure)
71            }
72            ItemKind::Struct(_, ast::Generics { params, .. }, _)
73            | ItemKind::Enum(_, ast::Generics { params, .. }, _)
74                if is_simple_candidate(params) =>
75            {
76                (true, simple_substructure)
77            }
78            _ => (false, default_substructure),
79        },
80        _ => (false, default_substructure),
81    };
82
83    let partial_cmp_def = MethodDef {
84        name: sym::partial_cmp,
85        generics: Bounds::empty(),
86        explicit_self: true,
87        nonself_args: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(self_ref(), sym::other)]))vec![(self_ref(), sym::other)],
88        ret_ty,
89        attributes: {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(cx.attr_word(sym::inline, span));
    vec
}thin_vec![cx.attr_word(sym::inline, span)],
90        fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
91        combine_substructure: substructure,
92    };
93
94    let trait_def = TraitDef {
95        span,
96        path: generic::ty::Path::new({
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [sym::cmp, sym::PartialOrd]))
    })path_std!(cmp::PartialOrd),
97        skip_path_as_bound: false,
98        needs_copy_as_bound_if_packed: true,
99        additional_bounds: ::alloc::vec::Vec::new()vec![],
100        supports_unions: false,
101        methods: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [partial_cmp_def]))vec![partial_cmp_def],
102        associated_types: Vec::new(),
103        is_const,
104        is_staged_api_crate: cx.ecfg.features.staged_api(),
105        safety: Safety::Default,
106        document: true,
107    };
108    trait_def.expand_ext(cx, mitem, item, push, is_simple)
109}
110
111// Special case for the type deriving both `PartialOrd` and `Ord`. Builds:
112// ```
113// Some(::core::cmp::Ord::cmp(self, other))
114// ```
115fn cs_partial_cmp_simple(cx: &ExtCtxt<'_>, span: Span, other_expr: Box<ast::Expr>) -> BlockOrExpr {
116    let ord_cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
117    let cmp_expr =
118        cx.expr_call_global(span, ord_cmp_path, {
    let len = [(), ()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(cx.expr_self(span));
    vec.push(other_expr);
    vec
}thin_vec![cx.expr_self(span), other_expr]);
119    BlockOrExpr::new_expr(cx.expr_some(span, cmp_expr))
120}
121
122fn cs_partial_cmp(
123    cx: &ExtCtxt<'_>,
124    span: Span,
125    substr: &Substructure<'_>,
126    discr_then_data: bool,
127) -> BlockOrExpr {
128    let test_id = Ident::new(sym::cmp, span);
129    let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
130    let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
131
132    // Builds:
133    //
134    // match ::core::cmp::PartialOrd::partial_cmp(&self.x, &other.x) {
135    //     ::core::option::Option::Some(::core::cmp::Ordering::Equal) =>
136    //         ::core::cmp::PartialOrd::partial_cmp(&self.y, &other.y),
137    //     cmp => cmp,
138    // }
139    let expr = cs_fold(
140        // foldr nests the if-elses correctly, leaving the first field
141        // as the outermost one, and the last as the innermost.
142        false,
143        cx,
144        span,
145        substr,
146        |cx, fold| match fold {
147            CsFold::Single(field) => {
148                let [other_expr] = &field.other_selflike_exprs[..] else {
149                    cx.dcx()
150                        .span_bug(field.span, "not exactly 2 arguments in `derive(PartialOrd)`");
151                };
152                let args = {
    let len = [(), ()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(field.self_expr.clone());
    vec.push(other_expr.clone());
    vec
}thin_vec![field.self_expr.clone(), other_expr.clone()];
153                cx.expr_call_global(field.span, partial_cmp_path.clone(), args)
154            }
155            CsFold::Combine(span, mut expr1, expr2) => {
156                // When the item is an enum, this expands to
157                // ```
158                // match (expr2) {
159                //     Some(Ordering::Equal) => expr1,
160                //     cmp => cmp
161                // }
162                // ```
163                // where `expr2` is `partial_cmp(self_discr, other_discr)`, and `expr1` is a `match`
164                // against the enum variants. This means that we begin by comparing the enum discriminants,
165                // before either inspecting their contents (if they match), or returning
166                // the `cmp::Ordering` of comparing the enum discriminants.
167                // ```
168                // match partial_cmp(self_discr, other_discr) {
169                //     Some(Ordering::Equal) => match (self, other)  {
170                //         (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
171                //         (Self::B(self_0), Self::B(other_0)) => partial_cmp(self_0, other_0),
172                //         _ => Some(Ordering::Equal)
173                //     }
174                //     cmp => cmp
175                // }
176                // ```
177                // If we have any certain enum layouts, flipping this results in better codegen
178                // ```
179                // match (self, other) {
180                //     (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
181                //     _ => partial_cmp(self_discr, other_discr)
182                // }
183                // ```
184                // Reference: https://github.com/rust-lang/rust/pull/103659#issuecomment-1328126354
185
186                if !discr_then_data
187                    && let ExprKind::Match(_, arms, _) = &mut expr1.kind
188                    && let Some(last) = arms.last_mut()
189                    && let PatKind::Wild = last.pat.kind
190                {
191                    last.body = Some(expr2);
192                    expr1
193                } else {
194                    let eq_arm = cx.arm(
195                        span,
196                        cx.pat_some(span, cx.pat_path(span, equal_path.clone())),
197                        expr1,
198                    );
199                    let neq_arm =
200                        cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
201                    cx.expr_match(span, expr2, {
    let len = [(), ()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(eq_arm);
    vec.push(neq_arm);
    vec
}thin_vec![eq_arm, neq_arm])
202                }
203            }
204            CsFold::Fieldless => cx.expr_some(span, cx.expr_path(equal_path.clone())),
205        },
206    );
207    BlockOrExpr::new_expr(expr)
208}