rustc_builtin_macros/deriving/cmp/
eq.rs1use rustc_ast::{self as ast, MetaItem, Safety};
2use rustc_data_structures::fx::FxHashSet;
3use rustc_expand::base::{Annotatable, ExtCtxt};
4use rustc_span::{Span, sym};
5use thin_vec::{ThinVec, thin_vec};
6
7use crate::deriving::generic::ty::*;
8use crate::deriving::generic::*;
9use crate::deriving::path_std;
10
11pub(crate) fn expand_deriving_eq(
12 cx: &ExtCtxt<'_>,
13 span: Span,
14 mitem: &MetaItem,
15 item: &Annotatable,
16 push: &mut dyn FnMut(Annotatable),
17 is_const: bool,
18) {
19 let span = cx.with_def_site_ctxt(span);
20
21 let trait_def = TraitDef {
22 span,
23 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::Eq]))
})path_std!(cmp::Eq),
24 skip_path_as_bound: false,
25 needs_copy_as_bound_if_packed: true,
26 additional_bounds: Vec::new(),
27 supports_unions: true,
28 methods: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[MethodDef {
name: sym::assert_fields_are_eq,
generics: Bounds::empty(),
explicit_self: true,
nonself_args: ::alloc::vec::Vec::new(),
ret_ty: Unit,
attributes: {
let len = [(), (), ()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.attr_word(sym::inline, span));
vec.push(cx.attr_nested_word(sym::doc, sym::hidden, span));
vec.push(cx.attr_nested_word(sym::coverage, sym::off,
span));
vec
},
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(Box::new(|a, b,
c| { cs_total_eq_assert(a, b, c) })),
}]))vec![MethodDef {
29 name: sym::assert_fields_are_eq,
30 generics: Bounds::empty(),
31 explicit_self: true,
32 nonself_args: vec![],
33 ret_ty: Unit,
34 attributes: thin_vec![
35 cx.attr_word(sym::inline, span),
38 cx.attr_nested_word(sym::doc, sym::hidden, span),
39 cx.attr_nested_word(sym::coverage, sym::off, span),
40 ],
41 fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
42 combine_substructure: combine_substructure(Box::new(|a, b, c| {
43 cs_total_eq_assert(a, b, c)
44 })),
45 }],
46 associated_types: Vec::new(),
47 is_const,
48 is_staged_api_crate: cx.ecfg.features.staged_api(),
49 safety: Safety::Default,
50 document: true,
51 };
52 trait_def.expand_ext(cx, mitem, item, push, true)
53}
54
55fn cs_total_eq_assert(
56 cx: &ExtCtxt<'_>,
57 trait_span: Span,
58 substr: &Substructure<'_>,
59) -> BlockOrExpr {
60 let mut stmts = ThinVec::new();
61 let mut seen_type_names = FxHashSet::default();
62 let mut process_variant = |variant: &ast::VariantData| {
63 for field in variant.fields() {
64 if let Some(name) = field.ty.kind.is_simple_path()
68 && !seen_type_names.insert(name)
69 {
70 } else {
72 super::assert_ty_bounds(
74 cx,
75 &mut stmts,
76 field.ty.clone(),
77 field.span,
78 &[sym::cmp, sym::AssertParamIsEq],
79 );
80 }
81 }
82 };
83
84 match *substr.fields {
85 StaticStruct(vdata, ..) => {
86 process_variant(vdata);
87 }
88 StaticEnum(enum_def, ..) => {
89 for variant in &enum_def.variants {
90 process_variant(&variant.data);
91 }
92 }
93 _ => cx.dcx().span_bug(trait_span, "unexpected substructure in `derive(Eq)`"),
94 }
95 BlockOrExpr::new_stmts(stmts)
96}