]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/deriving/cmp/eq.rs
Rollup merge of #34436 - jseyfried:no_block_expr, r=eddyb
[rust.git] / src / libsyntax_ext / deriving / cmp / eq.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use deriving::generic::*;
12 use deriving::generic::ty::*;
13
14 use syntax::ast::{MetaItem, Expr};
15 use syntax::ext::base::{ExtCtxt, Annotatable};
16 use syntax::ext::build::AstBuilder;
17 use syntax::parse::token::InternedString;
18 use syntax::ptr::P;
19 use syntax_pos::Span;
20
21 pub fn expand_deriving_eq(cx: &mut ExtCtxt,
22                           span: Span,
23                           mitem: &MetaItem,
24                           item: &Annotatable,
25                           push: &mut FnMut(Annotatable))
26 {
27     fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
28         cs_same_method(
29             |cx, span, exprs| {
30                 // create `a.<method>(); b.<method>(); c.<method>(); ...`
31                 // (where method is `assert_receiver_is_total_eq`)
32                 let stmts = exprs.into_iter().map(|e| cx.stmt_expr(e)).collect();
33                 let block = cx.block(span, stmts);
34                 cx.expr_block(block)
35             },
36             Box::new(|cx, sp, _, _| {
37                 cx.span_bug(sp, "non matching enums in derive(Eq)?") }),
38             cx,
39             span,
40             substr
41         )
42     }
43
44     let inline = cx.meta_word(span, InternedString::new("inline"));
45     let hidden = cx.meta_word(span, InternedString::new("hidden"));
46     let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden));
47     let attrs = vec!(cx.attribute(span, inline),
48                      cx.attribute(span, doc));
49     let trait_def = TraitDef {
50         span: span,
51         attributes: Vec::new(),
52         path: path_std!(cx, core::cmp::Eq),
53         additional_bounds: Vec::new(),
54         generics: LifetimeBounds::empty(),
55         is_unsafe: false,
56         methods: vec!(
57             MethodDef {
58                 name: "assert_receiver_is_total_eq",
59                 generics: LifetimeBounds::empty(),
60                 explicit_self: borrowed_explicit_self(),
61                 args: vec!(),
62                 ret_ty: nil_ty(),
63                 attributes: attrs,
64                 is_unsafe: false,
65                 unify_fieldless_variants: true,
66                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
67                     cs_total_eq_assert(a, b, c)
68                 }))
69             }
70         ),
71         associated_types: Vec::new(),
72     };
73     trait_def.expand(cx, mitem, item, push)
74 }