]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
Allow more deriving on packed structs.
[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;
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     is_const: bool,
19 ) {
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)
25     ];
26     let trait_def = TraitDef {
27         span,
28         path: path_std!(cmp::Eq),
29         skip_path_as_bound: false,
30         needs_copy_as_bound_if_packed: true,
31         additional_bounds: Vec::new(),
32         supports_unions: true,
33         methods: vec![MethodDef {
34             name: sym::assert_receiver_is_total_eq,
35             generics: Bounds::empty(),
36             explicit_self: true,
37             nonself_args: vec![],
38             ret_ty: Unit,
39             attributes: attrs,
40             fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
41             combine_substructure: combine_substructure(Box::new(|a, b, c| {
42                 cs_total_eq_assert(a, b, c)
43             })),
44         }],
45         associated_types: Vec::new(),
46         is_const,
47     };
48
49     super::inject_impl_of_structural_trait(cx, span, item, path_std!(marker::StructuralEq), push);
50
51     trait_def.expand_ext(cx, mitem, item, push, true)
52 }
53
54 fn cs_total_eq_assert(
55     cx: &mut ExtCtxt<'_>,
56     trait_span: Span,
57     substr: &Substructure<'_>,
58 ) -> BlockOrExpr {
59     let mut stmts = Vec::new();
60     let mut seen_type_names = FxHashSet::default();
61     let mut process_variant = |variant: &ast::VariantData| {
62         for field in variant.fields() {
63             // This basic redundancy checking only prevents duplication of
64             // assertions like `AssertParamIsEq<Foo>` where the type is a
65             // simple name. That's enough to get a lot of cases, though.
66             if let Some(name) = field.ty.kind.is_simple_path() && !seen_type_names.insert(name) {
67                 // Already produced an assertion for this type.
68             } else {
69                 // let _: AssertParamIsEq<FieldTy>;
70                 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.span_bug(trait_span, "unexpected substructure in `derive(Eq)`"),
91     }
92     BlockOrExpr::new_stmts(stmts)
93 }