]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/deriving/cmp/eq.rs
hygiene: `ExpnInfo` -> `ExpnData`
[rust.git] / src / libsyntax_ext / deriving / cmp / eq.rs
1 use crate::deriving::path_std;
2 use crate::deriving::generic::*;
3 use crate::deriving::generic::ty::*;
4
5 use syntax::ast::{self, Expr, MetaItem, GenericArg};
6 use syntax::ext::base::{Annotatable, ExtCtxt, SpecialDerives};
7 use syntax::ptr::P;
8 use syntax::symbol::{sym, Symbol};
9 use syntax_pos::Span;
10
11 pub fn expand_deriving_eq(cx: &mut ExtCtxt<'_>,
12                           span: Span,
13                           mitem: &MetaItem,
14                           item: &Annotatable,
15                           push: &mut dyn FnMut(Annotatable)) {
16     cx.resolver.add_derives(cx.current_expansion.id.expn_data().parent, SpecialDerives::EQ);
17
18     let inline = cx.meta_word(span, sym::inline);
19     let hidden = cx.meta_list_item_word(span, sym::hidden);
20     let doc = cx.meta_list(span, sym::doc, vec![hidden]);
21     let attrs = vec![cx.attribute(inline), cx.attribute(doc)];
22     let trait_def = TraitDef {
23         span,
24         attributes: Vec::new(),
25         path: path_std!(cx, cmp::Eq),
26         additional_bounds: Vec::new(),
27         generics: LifetimeBounds::empty(),
28         is_unsafe: false,
29         supports_unions: true,
30         methods: vec![MethodDef {
31                           name: "assert_receiver_is_total_eq",
32                           generics: LifetimeBounds::empty(),
33                           explicit_self: borrowed_explicit_self(),
34                           args: vec![],
35                           ret_ty: nil_ty(),
36                           attributes: attrs,
37                           is_unsafe: false,
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     trait_def.expand_ext(cx, mitem, item, push, true)
46 }
47
48 fn cs_total_eq_assert(cx: &mut ExtCtxt<'_>,
49                       trait_span: Span,
50                       substr: &Substructure<'_>)
51                       -> P<Expr> {
52     fn assert_ty_bounds(cx: &mut ExtCtxt<'_>, stmts: &mut Vec<ast::Stmt>,
53                         ty: P<ast::Ty>, span: Span, helper_name: &str) {
54         // Generate statement `let _: helper_name<ty>;`,
55         // set the expn ID so we can use the unstable struct.
56         let span = span.with_ctxt(cx.backtrace());
57         let assert_path = cx.path_all(span, true,
58                                         cx.std_path(&[sym::cmp, Symbol::intern(helper_name)]),
59                                         vec![GenericArg::Type(ty)], vec![]);
60         stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
61     }
62     fn process_variant(cx: &mut ExtCtxt<'_>,
63                        stmts: &mut Vec<ast::Stmt>,
64                        variant: &ast::VariantData) {
65         for field in variant.fields() {
66             // let _: AssertParamIsEq<FieldTy>;
67             assert_ty_bounds(cx, stmts, field.ty.clone(), field.span, "AssertParamIsEq");
68         }
69     }
70
71     let mut stmts = Vec::new();
72     match *substr.fields {
73         StaticStruct(vdata, ..) => {
74             process_variant(cx, &mut stmts, vdata);
75         }
76         StaticEnum(enum_def, ..) => {
77             for variant in &enum_def.variants {
78                 process_variant(cx, &mut stmts, &variant.data);
79             }
80         }
81         _ => cx.span_bug(trait_span, "unexpected substructure in `derive(Eq)`")
82     }
83     cx.expr_block(cx.block(trait_span, stmts))
84 }