about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorJorge Aparicio <japaricious@gmail.com>2014-10-29 18:31:41 -0500
committerJorge Aparicio <japaricious@gmail.com>2014-11-05 20:11:14 -0500
commit88ed2d1c41a0a80c98cb84be076e6447eeed3f36 (patch)
treef81aed46e5aa4d432ecac723f5ec4fe8042e09af /src/libsyntax
parent63c4f22f2bf9f1c070311cdc08c6ceb279434733 (diff)
downloadrust-88ed2d1c41a0a80c98cb84be076e6447eeed3f36.tar.gz
rust-88ed2d1c41a0a80c98cb84be076e6447eeed3f36.zip
Use operator sugar in the expansion of `#[deriving(PartialEq)]`
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ext/deriving/cmp/eq.rs36
1 files changed, 31 insertions, 5 deletions
diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs
index a27016fde61..c3b1a52925d 100644
--- a/src/libsyntax/ext/deriving/cmp/eq.rs
+++ b/src/libsyntax/ext/deriving/cmp/eq.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use ast::{MetaItem, Item, Expr};
+use ast::{MetaItem, Item, Expr, mod};
 use codemap::Span;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -25,12 +25,38 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
     // structures are equal if all fields are equal, and non equal, if
     // any fields are not equal or if the enum variants are different
     fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
-        cs_and(|cx, span, _, _| cx.expr_bool(span, false),
-                                 cx, span, substr)
+        cs_fold(
+            true,  // use foldl
+            |cx, span, subexpr, self_f, other_fs| {
+                let other_f = match other_fs {
+                    [ref o_f] => o_f,
+                    _ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Eq)`")
+                };
+
+                let eq = cx.expr_binary(span, ast::BiEq, self_f, other_f.clone());
+
+                cx.expr_binary(span, ast::BiAnd, subexpr, eq)
+            },
+            cx.expr_bool(span, true),
+            |cx, span, _, _| cx.expr_bool(span, false),
+            cx, span, substr)
     }
     fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
-        cs_or(|cx, span, _, _| cx.expr_bool(span, true),
-              cx, span, substr)
+        cs_fold(
+            true,  // use foldl
+            |cx, span, subexpr, self_f, other_fs| {
+                let other_f = match other_fs {
+                    [ref o_f] => o_f,
+                    _ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Eq)`")
+                };
+
+                let eq = cx.expr_binary(span, ast::BiNe, self_f, other_f.clone());
+
+                cx.expr_binary(span, ast::BiOr, subexpr, eq)
+            },
+            cx.expr_bool(span, false),
+            |cx, span, _, _| cx.expr_bool(span, true),
+            cx, span, substr)
     }
 
     macro_rules! md (