Skip to main content

rustc_builtin_macros/deriving/
ord.rs

1use rustc_ast::Safety;
2use rustc_expand::base::ExtCtxt;
3use rustc_span::{Span, sym};
4use thin_vec::thin_vec;
5
6use crate::deriving::generic::*;
7use crate::deriving::partial_ord::{OrdlikeDerive, cmp_body, discr_data_order};
8use crate::deriving::path_std;
9
10pub(crate) fn expand_deriving_ord(
11    cx: &ExtCtxt<'_>,
12    span: Span,
13    item: &ast::Item,
14    push: &mut dyn FnMut(Box<ast::Item>),
15    is_const: bool,
16) {
17    let discr_then_data = discr_data_order(item);
18
19    let trait_def = TraitDef {
20        span,
21        path: new_path(cx, span, { &[sym::cmp, sym::Ord] }, ::alloc::vec::Vec::new())path_std!(cx, span, cmp::Ord),
22        skip_path_as_bound: false,
23        needs_copy_as_bound_if_packed: true,
24        additional_bounds: SmallVec::new(),
25        supports_unions: false,
26        methods: {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(MethodDef {
                name: sym::cmp,
                generics: cx.empty_generics(span),
                explicit_self: true,
                nonself_args: {
                    let count = 0usize + 1usize;
                    let mut vec = ::smallvec::SmallVec::new();
                    if count <= vec.inline_size() {
                        vec.push((cx.ty_self_ref(span), sym::other));
                        vec
                    } else {
                        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                                    [(cx.ty_self_ref(span), sym::other)])))
                    }
                },
                has_other_selflike_arg: true,
                ret_ty: cx.ty_path(new_path(cx, span,
                        { &[sym::cmp, sym::Ordering] }, ::alloc::vec::Vec::new())),
                attributes: {
                    let len = [()].len();
                    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
                    vec.push(cx.attr_word(sym::inline, span));
                    vec
                },
                fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
                combine_substructure: combine_substructure(|cx, span, substr|
                        cs_cmp(cx, span, substr, discr_then_data)),
            });
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [MethodDef {
                                name: sym::cmp,
                                generics: cx.empty_generics(span),
                                explicit_self: true,
                                nonself_args: {
                                    let count = 0usize + 1usize;
                                    let mut vec = ::smallvec::SmallVec::new();
                                    if count <= vec.inline_size() {
                                        vec.push((cx.ty_self_ref(span), sym::other));
                                        vec
                                    } else {
                                        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                                                    [(cx.ty_self_ref(span), sym::other)])))
                                    }
                                },
                                has_other_selflike_arg: true,
                                ret_ty: cx.ty_path(new_path(cx, span,
                                        { &[sym::cmp, sym::Ordering] }, ::alloc::vec::Vec::new())),
                                attributes: {
                                    let len = [()].len();
                                    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
                                    vec.push(cx.attr_word(sym::inline, span));
                                    vec
                                },
                                fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
                                combine_substructure: combine_substructure(|cx, span,
                                        substr| cs_cmp(cx, span, substr, discr_then_data)),
                            }])))
    }
}smallvec![MethodDef {
27            name: sym::cmp,
28            generics: cx.empty_generics(span),
29            explicit_self: true,
30            nonself_args: smallvec![(cx.ty_self_ref(span), sym::other)],
31            has_other_selflike_arg: true,
32            ret_ty: cx.ty_path(path_std!(cx, span, cmp::Ordering)),
33            attributes: thin_vec![cx.attr_word(sym::inline, span)],
34            fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
35            combine_substructure: combine_substructure(|cx, span, substr| cs_cmp(
36                cx,
37                span,
38                substr,
39                discr_then_data
40            )),
41        }],
42        is_const,
43        safety: Safety::Default,
44        document: true,
45    };
46
47    trait_def.expand(cx, item, push)
48}
49
50pub(crate) fn cs_cmp(
51    cx: &ExtCtxt<'_>,
52    span: Span,
53    substr: Substructure<'_>,
54    discr_then_data: bool,
55) -> BlockOrExpr {
56    // Builds:
57    //
58    // match ::core::cmp::Ord::cmp(&self.x, &other.x) {
59    //     ::std::cmp::Ordering::Equal =>
60    //         ::core::cmp::Ord::cmp(&self.y, &other.y),
61    //     cmp => cmp,
62    // }
63    let expr = cmp_body(cx, span, substr, discr_then_data, OrdlikeDerive::Ord);
64    BlockOrExpr::new_expr(expr)
65}