8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use ast:: { MetaItem , Item , Expr } ;
11
+ use ast:: { MetaItem , Item , Expr , mod } ;
12
12
use codemap:: Span ;
13
13
use ext:: base:: ExtCtxt ;
14
14
use ext:: build:: AstBuilder ;
@@ -25,12 +25,38 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
25
25
// structures are equal if all fields are equal, and non equal, if
26
26
// any fields are not equal or if the enum variants are different
27
27
fn cs_eq ( cx : & mut ExtCtxt , span : Span , substr : & Substructure ) -> P < Expr > {
28
- cs_and ( |cx, span, _, _| cx. expr_bool ( span, false ) ,
29
- cx, span, substr)
28
+ cs_fold (
29
+ true , // use foldl
30
+ |cx, span, subexpr, self_f, other_fs| {
31
+ let other_f = match other_fs {
32
+ [ ref o_f] => o_f,
33
+ _ => cx. span_bug ( span, "not exactly 2 arguments in `deriving(Eq)`" )
34
+ } ;
35
+
36
+ let eq = cx. expr_binary ( span, ast:: BiEq , self_f, other_f. clone ( ) ) ;
37
+
38
+ cx. expr_binary ( span, ast:: BiAnd , subexpr, eq)
39
+ } ,
40
+ cx. expr_bool ( span, true ) ,
41
+ |cx, span, _, _| cx. expr_bool ( span, false ) ,
42
+ cx, span, substr)
30
43
}
31
44
fn cs_ne ( cx : & mut ExtCtxt , span : Span , substr : & Substructure ) -> P < Expr > {
32
- cs_or ( |cx, span, _, _| cx. expr_bool ( span, true ) ,
33
- cx, span, substr)
45
+ cs_fold (
46
+ true , // use foldl
47
+ |cx, span, subexpr, self_f, other_fs| {
48
+ let other_f = match other_fs {
49
+ [ ref o_f] => o_f,
50
+ _ => cx. span_bug ( span, "not exactly 2 arguments in `deriving(Eq)`" )
51
+ } ;
52
+
53
+ let eq = cx. expr_binary ( span, ast:: BiNe , self_f, other_f. clone ( ) ) ;
54
+
55
+ cx. expr_binary ( span, ast:: BiOr , subexpr, eq)
56
+ } ,
57
+ cx. expr_bool ( span, false ) ,
58
+ |cx, span, _, _| cx. expr_bool ( span, true ) ,
59
+ cx, span, substr)
34
60
}
35
61
36
62
macro_rules! md (
0 commit comments