Skip to content

Commit 7907099

Browse files
committed
Auto merge of #23985 - erickt:derive-cleanup, r=huonw
This extracts some of the minor cleanup patches from #23905.
2 parents cf51e55 + 02c5ff7 commit 7907099

18 files changed

+503
-525
lines changed

Diff for: src/libsyntax/ext/deriving/bounds.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,20 @@ use ext::deriving::generic::*;
1515
use ext::deriving::generic::ty::*;
1616
use ptr::P;
1717

18-
pub fn expand_deriving_unsafe_bound<F>(cx: &mut ExtCtxt,
19-
span: Span,
20-
_: &MetaItem,
21-
_: &Item,
22-
_: F) where
23-
F: FnOnce(P<Item>),
18+
pub fn expand_deriving_unsafe_bound(cx: &mut ExtCtxt,
19+
span: Span,
20+
_: &MetaItem,
21+
_: &Item,
22+
_: &mut FnMut(P<Item>))
2423
{
2524
cx.span_err(span, "this unsafe trait should be implemented explicitly");
2625
}
2726

28-
pub fn expand_deriving_copy<F>(cx: &mut ExtCtxt,
29-
span: Span,
30-
mitem: &MetaItem,
31-
item: &Item,
32-
push: F) where
33-
F: FnOnce(P<Item>),
27+
pub fn expand_deriving_copy(cx: &mut ExtCtxt,
28+
span: Span,
29+
mitem: &MetaItem,
30+
item: &Item,
31+
push: &mut FnMut(P<Item>))
3432
{
3533
let path = Path::new(vec![
3634
if cx.use_std { "std" } else { "core" },
@@ -48,5 +46,5 @@ pub fn expand_deriving_copy<F>(cx: &mut ExtCtxt,
4846
associated_types: Vec::new(),
4947
};
5048

51-
trait_def.expand(cx, mitem, item, push)
49+
trait_def.expand(cx, mitem, item, push);
5250
}

Diff for: src/libsyntax/ext/deriving/clone.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ use ext::deriving::generic::ty::*;
1717
use parse::token::InternedString;
1818
use ptr::P;
1919

20-
pub fn expand_deriving_clone<F>(cx: &mut ExtCtxt,
21-
span: Span,
22-
mitem: &MetaItem,
23-
item: &Item,
24-
push: F) where
25-
F: FnOnce(P<Item>),
20+
pub fn expand_deriving_clone(cx: &mut ExtCtxt,
21+
span: Span,
22+
mitem: &MetaItem,
23+
item: &Item,
24+
push: &mut FnMut(P<Item>))
2625
{
2726
let inline = cx.meta_word(span, InternedString::new("inline"));
2827
let attrs = vec!(cx.attribute(span, inline));

Diff for: src/libsyntax/ext/deriving/cmp/eq.rs

+36-61
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{MetaItem, Item, Expr, self};
11+
use ast::{MetaItem, Item, Expr};
1212
use codemap::Span;
1313
use ext::base::ExtCtxt;
1414
use ext::build::AstBuilder;
@@ -17,77 +17,52 @@ use ext::deriving::generic::ty::*;
1717
use parse::token::InternedString;
1818
use ptr::P;
1919

20-
pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
21-
span: Span,
22-
mitem: &MetaItem,
23-
item: &Item,
24-
push: F) where
25-
F: FnOnce(P<Item>),
20+
pub fn expand_deriving_eq(cx: &mut ExtCtxt,
21+
span: Span,
22+
mitem: &MetaItem,
23+
item: &Item,
24+
push: &mut FnMut(P<Item>))
2625
{
27-
// structures are equal if all fields are equal, and non equal, if
28-
// any fields are not equal or if the enum variants are different
29-
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
30-
cs_fold(
31-
true, // use foldl
32-
|cx, span, subexpr, self_f, other_fs| {
33-
let other_f = match other_fs {
34-
[ref o_f] => o_f,
35-
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
36-
};
37-
38-
let eq = cx.expr_binary(span, ast::BiEq, self_f, other_f.clone());
39-
40-
cx.expr_binary(span, ast::BiAnd, subexpr, eq)
41-
},
42-
cx.expr_bool(span, true),
43-
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
44-
cx, span, substr)
45-
}
46-
fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
47-
cs_fold(
48-
true, // use foldl
49-
|cx, span, subexpr, self_f, other_fs| {
50-
let other_f = match other_fs {
51-
[ref o_f] => o_f,
52-
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
53-
};
54-
55-
let eq = cx.expr_binary(span, ast::BiNe, self_f, other_f.clone());
56-
57-
cx.expr_binary(span, ast::BiOr, subexpr, eq)
26+
fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
27+
cs_same_method(
28+
|cx, span, exprs| {
29+
// create `a.<method>(); b.<method>(); c.<method>(); ...`
30+
// (where method is `assert_receiver_is_total_eq`)
31+
let stmts = exprs.into_iter().map(|e| cx.stmt_expr(e)).collect();
32+
let block = cx.block(span, stmts, None);
33+
cx.expr_block(block)
5834
},
59-
cx.expr_bool(span, false),
60-
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
61-
cx, span, substr)
35+
Box::new(|cx, sp, _, _| {
36+
cx.span_bug(sp, "non matching enums in derive(Eq)?") }),
37+
cx,
38+
span,
39+
substr
40+
)
6241
}
6342

64-
macro_rules! md {
65-
($name:expr, $f:ident) => { {
66-
let inline = cx.meta_word(span, InternedString::new("inline"));
67-
let attrs = vec!(cx.attribute(span, inline));
43+
let inline = cx.meta_word(span, InternedString::new("inline"));
44+
let hidden = cx.meta_word(span, InternedString::new("hidden"));
45+
let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden));
46+
let attrs = vec!(cx.attribute(span, inline),
47+
cx.attribute(span, doc));
48+
let trait_def = TraitDef {
49+
span: span,
50+
attributes: Vec::new(),
51+
path: path_std!(cx, core::cmp::Eq),
52+
additional_bounds: Vec::new(),
53+
generics: LifetimeBounds::empty(),
54+
methods: vec!(
6855
MethodDef {
69-
name: $name,
56+
name: "assert_receiver_is_total_eq",
7057
generics: LifetimeBounds::empty(),
7158
explicit_self: borrowed_explicit_self(),
72-
args: vec!(borrowed_self()),
73-
ret_ty: Literal(path_local!(bool)),
59+
args: vec!(),
60+
ret_ty: nil_ty(),
7461
attributes: attrs,
7562
combine_substructure: combine_substructure(Box::new(|a, b, c| {
76-
$f(a, b, c)
63+
cs_total_eq_assert(a, b, c)
7764
}))
7865
}
79-
} }
80-
}
81-
82-
let trait_def = TraitDef {
83-
span: span,
84-
attributes: Vec::new(),
85-
path: path_std!(cx, core::cmp::PartialEq),
86-
additional_bounds: Vec::new(),
87-
generics: LifetimeBounds::empty(),
88-
methods: vec!(
89-
md!("eq", cs_eq),
90-
md!("ne", cs_ne)
9166
),
9267
associated_types: Vec::new(),
9368
};

0 commit comments

Comments
 (0)