rustc_builtin_macros/deriving/
eq.rs1use rustc_ast::{self as ast, Safety};
2use rustc_data_structures::fx::FxHashSet;
3use rustc_expand::base::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 item: &ast::Item,
15 push: &mut dyn FnMut(Box<ast::Item>),
16 is_const: bool,
17) {
18 let span = cx.with_def_site_ctxt(span);
19
20 let trait_def = TraitDef {
21 span,
22 path: generic::ty::new_path(cx, span, { &[sym::cmp, sym::Eq] }, &[])path_std!(cx, span, cmp::Eq),
23 skip_path_as_bound: false,
24 needs_copy_as_bound_if_packed: true,
25 additional_bounds: SmallVec::new(),
26 supports_unions: true,
27 methods: {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(MethodDef {
name: sym::assert_fields_are_eq,
generics: cx.empty_generics(span),
explicit_self: true,
nonself_args: ::smallvec::SmallVec::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(cs_total_eq_assert),
});
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::assert_fields_are_eq,
generics: cx.empty_generics(span),
explicit_self: true,
nonself_args: ::smallvec::SmallVec::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(cs_total_eq_assert),
}])))
}
}smallvec![MethodDef {
28 name: sym::assert_fields_are_eq,
29 generics: cx.empty_generics(span),
30 explicit_self: true,
31 nonself_args: smallvec![],
32 ret_ty: Unit,
33 attributes: thin_vec![
34 cx.attr_word(sym::inline, span),
37 cx.attr_nested_word(sym::doc, sym::hidden, span),
38 cx.attr_nested_word(sym::coverage, sym::off, span),
39 ],
40 fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
41 combine_substructure: combine_substructure(cs_total_eq_assert),
42 }],
43 associated_types: SmallVec::new(),
44 is_const,
45 safety: Safety::Default,
46 document: true,
47 };
48 trait_def.expand_ext(cx, item, push, true)
49}
50
51fn cs_total_eq_assert(cx: &ExtCtxt<'_>, trait_span: Span, substr: Substructure<'_>) -> BlockOrExpr {
52 let mut stmts = ThinVec::new();
53 let mut seen_type_names = FxHashSet::default();
54 let mut process_variant = |variant: &ast::VariantData| {
55 for field in variant.fields() {
56 if let Some(name) = field.ty.kind.is_simple_path()
60 && !seen_type_names.insert(name)
61 {
62 } else {
64 super::assert_ty_bounds(
66 cx,
67 &mut stmts,
68 field.ty.clone(),
69 field.span,
70 &[sym::cmp, sym::AssertParamIsEq],
71 );
72 }
73 }
74 };
75
76 match substr {
77 StaticStruct(vdata, ..) => {
78 process_variant(vdata);
79 }
80 StaticEnum(enum_def, ..) => {
81 for variant in &enum_def.variants {
82 process_variant(&variant.data);
83 }
84 }
85 _ => cx.dcx().span_bug(trait_span, "unexpected substructure in `derive(Eq)`"),
86 }
87 BlockOrExpr::new_stmts(stmts)
88}