]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
Rollup merge of #101864 - notriddle:notriddle/h1-h2-h3-h4, r=GuillaumeGomez
[rust.git] / compiler / rustc_builtin_macros / src / deriving / cmp / eq.rs
1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::path_std;
4
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, Ident};
9 use rustc_span::Span;
10 use thin_vec::thin_vec;
11
12 pub fn expand_deriving_eq(
13     cx: &mut ExtCtxt<'_>,
14     span: Span,
15     mitem: &MetaItem,
16     item: &Annotatable,
17     push: &mut dyn FnMut(Annotatable),
18 ) {
19     let span = cx.with_def_site_ctxt(span);
20     let inline = cx.meta_word(span, sym::inline);
21     let hidden = rustc_ast::attr::mk_nested_word_item(Ident::new(sym::hidden, span));
22     let doc = rustc_ast::attr::mk_list_item(Ident::new(sym::doc, span), vec![hidden]);
23     let no_coverage = cx.meta_word(span, sym::no_coverage);
24     let attrs = thin_vec![cx.attribute(inline), cx.attribute(doc), cx.attribute(no_coverage)];
25     let trait_def = TraitDef {
26         span,
27         path: path_std!(cmp::Eq),
28         additional_bounds: Vec::new(),
29         generics: Bounds::empty(),
30         supports_unions: true,
31         methods: vec![MethodDef {
32             name: sym::assert_receiver_is_total_eq,
33             generics: Bounds::empty(),
34             explicit_self: true,
35             nonself_args: vec![],
36             ret_ty: Unit,
37             attributes: attrs,
38             unify_fieldless_variants: true,
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     };
45
46     super::inject_impl_of_structural_trait(cx, span, item, path_std!(marker::StructuralEq), push);
47
48     trait_def.expand_ext(cx, mitem, item, push, true)
49 }
50
51 fn cs_total_eq_assert(
52     cx: &mut ExtCtxt<'_>,
53     trait_span: Span,
54     substr: &Substructure<'_>,
55 ) -> BlockOrExpr {
56     let mut stmts = Vec::new();
57     let mut seen_type_names = FxHashSet::default();
58     let mut process_variant = |variant: &ast::VariantData| {
59         for field in variant.fields() {
60             // This basic redundancy checking only prevents duplication of
61             // assertions like `AssertParamIsEq<Foo>` where the type is a
62             // simple name. That's enough to get a lot of cases, though.
63             if let Some(name) = field.ty.kind.is_simple_path() && !seen_type_names.insert(name) {
64                 // Already produced an assertion for this type.
65             } else {
66                 // let _: AssertParamIsEq<FieldTy>;
67                 super::assert_ty_bounds(
68                     cx,
69                     &mut stmts,
70                     field.ty.clone(),
71                     field.span,
72                     &[sym::cmp, sym::AssertParamIsEq],
73                 );
74             }
75         }
76     };
77
78     match *substr.fields {
79         StaticStruct(vdata, ..) => {
80             process_variant(vdata);
81         }
82         StaticEnum(enum_def, ..) => {
83             for variant in &enum_def.variants {
84                 process_variant(&variant.data);
85             }
86         }
87         _ => cx.span_bug(trait_span, "unexpected substructure in `derive(Eq)`"),
88     }
89     BlockOrExpr::new_stmts(stmts)
90 }