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_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_nested_word(sym::doc, sym::hidden, span),
36 cx.attr_nested_word(sym::coverage, sym::off, span),
37 ],
38 fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
39 combine_substructure: combine_substructure(Box::new(|a, b, c| {
40 cs_total_eq_assert(a, b, c)
41 })),
42 }],
43 associated_types: Vec::new(),
44 is_const,
45 is_staged_api_crate: cx.ecfg.features.staged_api(),
46 safety: Safety::Default,
47 document: true,
48 };
49 trait_def.expand_ext(cx, mitem, item, push, true)
50}
51
52fn cs_total_eq_assert(
53 cx: &ExtCtxt<'_>,
54 trait_span: Span,
55 substr: &Substructure<'_>,
56) -> BlockOrExpr {
57 let mut stmts = ThinVec::new();
58 let mut seen_type_names = FxHashSet::default();
59 let mut process_variant = |variant: &ast::VariantData| {
60 for field in variant.fields() {
61 if let Some(name) = field.ty.kind.is_simple_path()
65 && !seen_type_names.insert(name)
66 {
67 } else {
69 super::assert_ty_bounds(
71 cx,
72 &mut stmts,
73 field.ty.clone(),
74 field.span,
75 &[sym::cmp, sym::AssertParamIsEq],
76 );
77 }
78 }
79 };
80
81 match *substr.fields {
82 StaticStruct(vdata, ..) => {
83 process_variant(vdata);
84 }
85 StaticEnum(enum_def, ..) => {
86 for variant in &enum_def.variants {
87 process_variant(&variant.data);
88 }
89 }
90 _ => cx.dcx().span_bug(trait_span, "unexpected substructure in `derive(Eq)`"),
91 }
92 BlockOrExpr::new_stmts(stmts)
93}