1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::path_std;
5 use rustc_ast::{self as ast, MetaItem};
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_expand::base::{Annotatable, ExtCtxt};
8 use rustc_span::symbol::sym;
10 use thin_vec::thin_vec;
12 pub fn expand_deriving_eq(
17 push: &mut dyn FnMut(Annotatable),
20 let span = cx.with_def_site_ctxt(span);
21 let attrs = thin_vec![
22 cx.attr_word(sym::inline, span),
23 cx.attr_nested_word(sym::doc, sym::hidden, span),
24 cx.attr_word(sym::no_coverage, span)
26 let trait_def = TraitDef {
28 path: path_std!(cmp::Eq),
29 skip_path_as_bound: false,
30 additional_bounds: Vec::new(),
31 supports_unions: true,
32 methods: vec![MethodDef {
33 name: sym::assert_receiver_is_total_eq,
34 generics: Bounds::empty(),
39 fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
40 combine_substructure: combine_substructure(Box::new(|a, b, c| {
41 cs_total_eq_assert(a, b, c)
44 associated_types: Vec::new(),
48 super::inject_impl_of_structural_trait(cx, span, item, path_std!(marker::StructuralEq), push);
50 trait_def.expand_ext(cx, mitem, item, push, true)
53 fn cs_total_eq_assert(
56 substr: &Substructure<'_>,
58 let mut stmts = Vec::new();
59 let mut seen_type_names = FxHashSet::default();
60 let mut process_variant = |variant: &ast::VariantData| {
61 for field in variant.fields() {
62 // This basic redundancy checking only prevents duplication of
63 // assertions like `AssertParamIsEq<Foo>` where the type is a
64 // simple name. That's enough to get a lot of cases, though.
65 if let Some(name) = field.ty.kind.is_simple_path() && !seen_type_names.insert(name) {
66 // Already produced an assertion for this type.
68 // let _: AssertParamIsEq<FieldTy>;
69 super::assert_ty_bounds(
74 &[sym::cmp, sym::AssertParamIsEq],
80 match *substr.fields {
81 StaticStruct(vdata, ..) => {
82 process_variant(vdata);
84 StaticEnum(enum_def, ..) => {
85 for variant in &enum_def.variants {
86 process_variant(&variant.data);
89 _ => cx.span_bug(trait_span, "unexpected substructure in `derive(Eq)`"),
91 BlockOrExpr::new_stmts(stmts)