rustc_builtin_macros/deriving/cmp/
ord.rs1use rustc_ast::{MetaItem, 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;
9
10pub(crate) fn expand_deriving_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 trait_def = TraitDef {
19 span,
20 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::Ord]))
})path_std!(cmp::Ord),
21 skip_path_as_bound: false,
22 needs_copy_as_bound_if_packed: true,
23 additional_bounds: SmallVec::new(),
24 supports_unions: false,
25 methods: {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(MethodDef {
name: sym::cmp,
generics: Bounds::empty(),
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::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]))
})),
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: Bounds::empty(),
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::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]))
})),
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 {
26 name: sym::cmp,
27 generics: Bounds::empty(),
28 explicit_self: true,
29 nonself_args: smallvec![(self_ref(), sym::other)],
30 ret_ty: Path(path_std!(cmp::Ordering)),
31 attributes: thin_vec![cx.attr_word(sym::inline, span)],
32 fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
33 combine_substructure: combine_substructure(cs_cmp),
34 }],
35 associated_types: SmallVec::new(),
36 is_const,
37 safety: Safety::Default,
38 document: true,
39 };
40
41 trait_def.expand(cx, mitem, item, push)
42}
43
44pub(crate) fn cs_cmp(cx: &ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
45 let test_id = Ident::new(sym::cmp, span);
46 let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
47 let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
48
49 let expr = cs_fold(
57 false,
60 cx,
61 span,
62 substr,
63 |cx, fold| match fold {
64 CsFold::Single(field) => {
65 let [other_expr] = &field.other_selflike_exprs[..] else {
66 cx.dcx().span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
67 };
68 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()];
69 cx.expr_call_global(field.span, cmp_path.clone(), args)
70 }
71 CsFold::Combine(span, expr1, expr2) => {
72 let eq_arm = cx.arm(span, cx.pat_path(span, equal_path.clone()), expr1);
73 let neq_arm =
74 cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
75 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])
76 }
77 CsFold::Fieldless => cx.expr_path(equal_path.clone()),
78 },
79 );
80 BlockOrExpr::new_expr(expr)
81}