]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/deriving/cmp/eq.rs
Replace AstBuilder with inherent methods
[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};
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     let inline = cx.meta_word(span, sym::inline);
17     let hidden = cx.meta_list_item_word(span, sym::hidden);
18     let doc = cx.meta_list(span, sym::doc, vec![hidden]);
19     let attrs = vec![cx.attribute(inline), cx.attribute(doc)];
20     let trait_def = TraitDef {
21         span,
22         attributes: Vec::new(),
23         path: path_std!(cx, cmp::Eq),
24         additional_bounds: Vec::new(),
25         generics: LifetimeBounds::empty(),
26         is_unsafe: false,
27         supports_unions: true,
28         methods: vec![MethodDef {
29                           name: "assert_receiver_is_total_eq",
30                           generics: LifetimeBounds::empty(),
31                           explicit_self: borrowed_explicit_self(),
32                           args: vec![],
33                           ret_ty: nil_ty(),
34                           attributes: attrs,
35                           is_unsafe: false,
36                           unify_fieldless_variants: true,
37                           combine_substructure: combine_substructure(Box::new(|a, b, c| {
38                               cs_total_eq_assert(a, b, c)
39                           })),
40                       }],
41         associated_types: Vec::new(),
42     };
43     trait_def.expand_ext(cx, mitem, item, push, true)
44 }
45
46 fn cs_total_eq_assert(cx: &mut ExtCtxt<'_>,
47                       trait_span: Span,
48                       substr: &Substructure<'_>)
49                       -> P<Expr> {
50     fn assert_ty_bounds(cx: &mut ExtCtxt<'_>, stmts: &mut Vec<ast::Stmt>,
51                         ty: P<ast::Ty>, span: Span, helper_name: &str) {
52         // Generate statement `let _: helper_name<ty>;`,
53         // set the expn ID so we can use the unstable struct.
54         let span = span.with_ctxt(cx.backtrace());
55         let assert_path = cx.path_all(span, true,
56                                         cx.std_path(&[sym::cmp, Symbol::intern(helper_name)]),
57                                         vec![GenericArg::Type(ty)], vec![]);
58         stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
59     }
60     fn process_variant(cx: &mut ExtCtxt<'_>,
61                        stmts: &mut Vec<ast::Stmt>,
62                        variant: &ast::VariantData) {
63         for field in variant.fields() {
64             // let _: AssertParamIsEq<FieldTy>;
65             assert_ty_bounds(cx, stmts, field.ty.clone(), field.span, "AssertParamIsEq");
66         }
67     }
68
69     let mut stmts = Vec::new();
70     match *substr.fields {
71         StaticStruct(vdata, ..) => {
72             process_variant(cx, &mut stmts, vdata);
73         }
74         StaticEnum(enum_def, ..) => {
75             for variant in &enum_def.variants {
76                 process_variant(cx, &mut stmts, &variant.node.data);
77             }
78         }
79         _ => cx.span_bug(trait_span, "unexpected substructure in `derive(Eq)`")
80     }
81     cx.expr_block(cx.block(trait_span, stmts))
82 }