Skip to main content

rustc_builtin_macros/deriving/
ord.rs

1use rustc_ast::Safety;
2use rustc_expand::base::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;
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 trait_def = TraitDef {
18        span,
19        path: generic::ty::new_path(cx, span, { &[sym::cmp, sym::Ord] }, &[])path_std!(cx, span, cmp::Ord),
20        skip_path_as_bound: false,
21        needs_copy_as_bound_if_packed: true,
22        additional_bounds: SmallVec::new(),
23        supports_unions: false,
24        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((self_ref(), 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(),
                                    [(self_ref(), sym::other)])))
                    }
                },
                ret_ty: Path(generic::ty::new_path(cx, span,
                        { &[sym::cmp, sym::Ordering] }, &[])),
                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(cs_cmp),
            });
        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((self_ref(), 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(),
                                                    [(self_ref(), sym::other)])))
                                    }
                                },
                                ret_ty: Path(generic::ty::new_path(cx, span,
                                        { &[sym::cmp, sym::Ordering] }, &[])),
                                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(cs_cmp),
                            }])))
    }
}smallvec![MethodDef {
25            name: sym::cmp,
26            generics: cx.empty_generics(span),
27            explicit_self: true,
28            nonself_args: smallvec![(self_ref(), sym::other)],
29            ret_ty: Path(path_std!(cx, span, cmp::Ordering)),
30            attributes: thin_vec![cx.attr_word(sym::inline, span)],
31            fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
32            combine_substructure: combine_substructure(cs_cmp),
33        }],
34        associated_types: SmallVec::new(),
35        is_const,
36        safety: Safety::Default,
37        document: true,
38    };
39
40    trait_def.expand(cx, item, push)
41}
42
43pub(crate) fn cs_cmp(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) -> BlockOrExpr {
44    let test_id = Ident::new(sym::cmp, span);
45    let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
46    let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
47
48    // Builds:
49    //
50    // match ::core::cmp::Ord::cmp(&self.x, &other.x) {
51    //     ::std::cmp::Ordering::Equal =>
52    //         ::core::cmp::Ord::cmp(&self.y, &other.y),
53    //     cmp => cmp,
54    // }
55    let expr = cs_foldr(
56        cx,
57        span,
58        substr,
59        |field| {
60            let other_expr =
61                field.other_selflike_expr.expect("not exactly 2 arguments in `derive(Ord)`");
62            let args = {
    let len = [(), ()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(field.self_expr);
    vec.push(other_expr);
    vec
}thin_vec![field.self_expr, other_expr];
63            cx.expr_call_global(field.span, cmp_path.clone(), args)
64        },
65        |span, expr1, expr2| {
66            let eq_arm = cx.arm(span, cx.pat_path(span, equal_path.clone()), expr1);
67            let neq_arm = cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
68            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])
69        },
70        || cx.expr_path(equal_path.clone()),
71    );
72    BlockOrExpr::new_expr(expr)
73}