Skip to content

Commit 1ce06d0

Browse files
committed
Remove redundant operation in derive[PartialEq]
1 parent 3238e0d commit 1ce06d0

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

src/libsyntax_ext/deriving/cmp/partial_eq.rs

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,70 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
2727
// structures are equal if all fields are equal, and non equal, if
2828
// any fields are not equal or if the enum variants are different
2929
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
30-
cs_fold(true, // use foldl
31-
|cx, span, subexpr, self_f, other_fs| {
32-
let other_f = match (other_fs.len(), other_fs.get(0)) {
33-
(1, Some(o_f)) => o_f,
34-
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
35-
};
30+
cs_fold1(true, // use foldl
31+
|cx, span, subexpr, self_f, other_fs| {
32+
let other_f = match (other_fs.len(), other_fs.get(0)) {
33+
(1, Some(o_f)) => o_f,
34+
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
35+
};
3636

37-
let eq = cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone());
37+
let eq = cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone());
3838

39-
cx.expr_binary(span, BinOpKind::And, subexpr, eq)
40-
},
41-
cx.expr_bool(span, true),
42-
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
43-
cx,
44-
span,
45-
substr)
39+
cx.expr_binary(span, BinOpKind::And, subexpr, eq)
40+
},
41+
|cx, args| {
42+
match args {
43+
Some((span, self_f, other_fs)) => {
44+
// Special-case the base case to generate cleaner code.
45+
let other_f = match (other_fs.len(), other_fs.get(0)) {
46+
(1, Some(o_f)) => o_f,
47+
_ => {
48+
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
49+
}
50+
};
51+
52+
cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone())
53+
}
54+
None => cx.expr_bool(span, true),
55+
}
56+
},
57+
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
58+
cx,
59+
span,
60+
substr)
4661
}
4762
fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
48-
cs_fold(true, // use foldl
49-
|cx, span, subexpr, self_f, other_fs| {
50-
let other_f = match (other_fs.len(), other_fs.get(0)) {
51-
(1, Some(o_f)) => o_f,
52-
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
53-
};
63+
cs_fold1(true, // use foldl
64+
|cx, span, subexpr, self_f, other_fs| {
65+
let other_f = match (other_fs.len(), other_fs.get(0)) {
66+
(1, Some(o_f)) => o_f,
67+
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
68+
};
69+
70+
let eq = cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone());
5471

55-
let eq = cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone());
72+
cx.expr_binary(span, BinOpKind::Or, subexpr, eq)
73+
},
74+
|cx, args| {
75+
match args {
76+
Some((span, self_f, other_fs)) => {
77+
// Special-case the base case to generate cleaner code.
78+
let other_f = match (other_fs.len(), other_fs.get(0)) {
79+
(1, Some(o_f)) => o_f,
80+
_ => {
81+
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
82+
}
83+
};
5684

57-
cx.expr_binary(span, BinOpKind::Or, subexpr, eq)
58-
},
59-
cx.expr_bool(span, false),
60-
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
61-
cx,
62-
span,
63-
substr)
85+
cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone())
86+
}
87+
None => cx.expr_bool(span, false),
88+
}
89+
},
90+
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
91+
cx,
92+
span,
93+
substr)
6494
}
6595

6696
macro_rules! md {

0 commit comments

Comments
 (0)