Skip to content

Commit 6557f4b

Browse files
committed
syntax: Rename deriving/cmp/* to match their current names
1 parent abf0548 commit 6557f4b

File tree

7 files changed

+415
-415
lines changed

7 files changed

+415
-415
lines changed

src/libsyntax/ext/deriving/cmp/eq.rs

Lines changed: 30 additions & 56 deletions
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;
@@ -24,70 +24,44 @@ pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
2424
push: F) where
2525
F: FnOnce(P<Item>),
2626
{
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)
58-
},
59-
cx.expr_bool(span, false),
60-
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
61-
cx, span, substr)
27+
fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
28+
cs_same_method(|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)
34+
},
35+
Box::new(|cx, sp, _, _| {
36+
cx.span_bug(sp, "non matching enums in derive(Eq)?") }),
37+
cx,
38+
span,
39+
substr)
6240
}
6341

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));
42+
let inline = cx.meta_word(span, InternedString::new("inline"));
43+
let hidden = cx.meta_word(span, InternedString::new("hidden"));
44+
let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden));
45+
let attrs = vec!(cx.attribute(span, inline),
46+
cx.attribute(span, doc));
47+
let trait_def = TraitDef {
48+
span: span,
49+
attributes: Vec::new(),
50+
path: path_std!(cx, core::cmp::Eq),
51+
additional_bounds: Vec::new(),
52+
generics: LifetimeBounds::empty(),
53+
methods: vec!(
6854
MethodDef {
69-
name: $name,
55+
name: "assert_receiver_is_total_eq",
7056
generics: LifetimeBounds::empty(),
7157
explicit_self: borrowed_explicit_self(),
72-
args: vec!(borrowed_self()),
73-
ret_ty: Literal(path_local!(bool)),
58+
args: vec!(),
59+
ret_ty: nil_ty(),
7460
attributes: attrs,
7561
combine_substructure: combine_substructure(Box::new(|a, b, c| {
76-
$f(a, b, c)
62+
cs_total_eq_assert(a, b, c)
7763
}))
7864
}
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)
9165
),
9266
associated_types: Vec::new(),
9367
};

src/libsyntax/ext/deriving/cmp/ord.rs

Lines changed: 44 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
pub use self::OrderingOp::*;
12-
1311
use ast;
1412
use ast::{MetaItem, Item, Expr};
1513
use codemap::Span;
@@ -20,114 +18,72 @@ use ext::deriving::generic::ty::*;
2018
use parse::token::InternedString;
2119
use ptr::P;
2220

23-
pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
24-
span: Span,
25-
mitem: &MetaItem,
26-
item: &Item,
27-
push: F) where
21+
pub fn expand_deriving_totalord<F>(cx: &mut ExtCtxt,
22+
span: Span,
23+
mitem: &MetaItem,
24+
item: &Item,
25+
push: F) where
2826
F: FnOnce(P<Item>),
2927
{
30-
macro_rules! md {
31-
($name:expr, $op:expr, $equal:expr) => { {
32-
let inline = cx.meta_word(span, InternedString::new("inline"));
33-
let attrs = vec!(cx.attribute(span, inline));
28+
let inline = cx.meta_word(span, InternedString::new("inline"));
29+
let attrs = vec!(cx.attribute(span, inline));
30+
let trait_def = TraitDef {
31+
span: span,
32+
attributes: Vec::new(),
33+
path: path_std!(cx, core::cmp::Ord),
34+
additional_bounds: Vec::new(),
35+
generics: LifetimeBounds::empty(),
36+
methods: vec!(
3437
MethodDef {
35-
name: $name,
38+
name: "cmp",
3639
generics: LifetimeBounds::empty(),
3740
explicit_self: borrowed_explicit_self(),
3841
args: vec!(borrowed_self()),
39-
ret_ty: Literal(path_local!(bool)),
42+
ret_ty: Literal(path_std!(cx, core::cmp::Ordering)),
4043
attributes: attrs,
41-
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
42-
cs_op($op, $equal, cx, span, substr)
43-
}))
44+
combine_substructure: combine_substructure(Box::new(|a, b, c| {
45+
cs_cmp(a, b, c)
46+
})),
4447
}
45-
} }
46-
}
47-
48-
let ordering_ty = Literal(path_std!(cx, core::cmp::Ordering));
49-
let ret_ty = Literal(Path::new_(pathvec_std!(cx, core::option::Option),
50-
None,
51-
vec![box ordering_ty],
52-
true));
53-
54-
let inline = cx.meta_word(span, InternedString::new("inline"));
55-
let attrs = vec!(cx.attribute(span, inline));
56-
57-
let partial_cmp_def = MethodDef {
58-
name: "partial_cmp",
59-
generics: LifetimeBounds::empty(),
60-
explicit_self: borrowed_explicit_self(),
61-
args: vec![borrowed_self()],
62-
ret_ty: ret_ty,
63-
attributes: attrs,
64-
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
65-
cs_partial_cmp(cx, span, substr)
66-
}))
67-
};
68-
69-
let trait_def = TraitDef {
70-
span: span,
71-
attributes: vec![],
72-
path: path_std!(cx, core::cmp::PartialOrd),
73-
additional_bounds: vec![],
74-
generics: LifetimeBounds::empty(),
75-
methods: vec![
76-
partial_cmp_def,
77-
md!("lt", true, false),
78-
md!("le", true, true),
79-
md!("gt", false, false),
80-
md!("ge", false, true)
81-
],
48+
),
8249
associated_types: Vec::new(),
8350
};
51+
8452
trait_def.expand(cx, mitem, item, push)
8553
}
8654

87-
#[derive(Copy, Clone)]
88-
pub enum OrderingOp {
89-
PartialCmpOp, LtOp, LeOp, GtOp, GeOp,
90-
}
9155

92-
pub fn some_ordering_collapsed(cx: &mut ExtCtxt,
93-
span: Span,
94-
op: OrderingOp,
95-
self_arg_tags: &[ast::Ident]) -> P<ast::Expr> {
56+
pub fn ordering_collapsed(cx: &mut ExtCtxt,
57+
span: Span,
58+
self_arg_tags: &[ast::Ident]) -> P<ast::Expr> {
9659
let lft = cx.expr_ident(span, self_arg_tags[0]);
9760
let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1]));
98-
let op_str = match op {
99-
PartialCmpOp => "partial_cmp",
100-
LtOp => "lt", LeOp => "le",
101-
GtOp => "gt", GeOp => "ge",
102-
};
103-
cx.expr_method_call(span, lft, cx.ident_of(op_str), vec![rgt])
61+
cx.expr_method_call(span, lft, cx.ident_of("cmp"), vec![rgt])
10462
}
10563

106-
pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
64+
pub fn cs_cmp(cx: &mut ExtCtxt, span: Span,
10765
substr: &Substructure) -> P<Expr> {
10866
let test_id = cx.ident_of("__test");
109-
let ordering = cx.path_global(span,
110-
vec!(cx.ident_of_std("core"),
111-
cx.ident_of("cmp"),
112-
cx.ident_of("Ordering"),
113-
cx.ident_of("Equal")));
114-
let ordering = cx.expr_path(ordering);
115-
let equals_expr = cx.expr_some(span, ordering);
67+
let equals_path = cx.path_global(span,
68+
vec!(cx.ident_of_std("core"),
69+
cx.ident_of("cmp"),
70+
cx.ident_of("Ordering"),
71+
cx.ident_of("Equal")));
11672

117-
let partial_cmp_path = vec![
73+
let cmp_path = vec![
11874
cx.ident_of_std("core"),
11975
cx.ident_of("cmp"),
120-
cx.ident_of("PartialOrd"),
121-
cx.ident_of("partial_cmp"),
76+
cx.ident_of("Ord"),
77+
cx.ident_of("cmp"),
12278
];
12379

12480
/*
12581
Builds:
12682
127-
let __test = ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1);
128-
if __test == ::std::option::Option::Some(::std::cmp::Ordering::Equal) {
129-
let __test = ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2);
130-
if __test == ::std::option::Option::Some(::std::cmp::Ordering::Equal) {
83+
let __test = ::std::cmp::Ord::cmp(&self_field1, &other_field1);
84+
if other == ::std::cmp::Ordering::Equal {
85+
let __test = ::std::cmp::Ord::cmp(&self_field2, &other_field2);
86+
if __test == ::std::cmp::Ordering::Equal {
13187
...
13288
} else {
13389
__test
@@ -144,7 +100,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
144100
false,
145101
|cx, span, old, self_f, other_fs| {
146102
// let __test = new;
147-
// if __test == Some(::std::cmp::Ordering::Equal) {
103+
// if __test == ::std::cmp::Ordering::Equal {
148104
// old
149105
// } else {
150106
// __test
@@ -161,77 +117,25 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
161117
cx.expr_addr_of(span, other_f.clone()),
162118
];
163119

164-
cx.expr_call_global(span, partial_cmp_path.clone(), args)
120+
cx.expr_call_global(span, cmp_path.clone(), args)
165121
};
166122

167123
let assign = cx.stmt_let(span, false, test_id, new);
168124

169125
let cond = cx.expr_binary(span, ast::BiEq,
170126
cx.expr_ident(span, test_id),
171-
equals_expr.clone());
127+
cx.expr_path(equals_path.clone()));
172128
let if_ = cx.expr_if(span,
173129
cond,
174130
old, Some(cx.expr_ident(span, test_id)));
175131
cx.expr_block(cx.block(span, vec!(assign), Some(if_)))
176132
},
177-
equals_expr.clone(),
133+
cx.expr_path(equals_path.clone()),
178134
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
179135
if self_args.len() != 2 {
180-
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
136+
cx.span_bug(span, "not exactly 2 arguments in `derives(Ord)`")
181137
} else {
182-
some_ordering_collapsed(cx, span, PartialCmpOp, tag_tuple)
183-
}
184-
}),
185-
cx, span, substr)
186-
}
187-
188-
/// Strict inequality.
189-
fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
190-
span: Span, substr: &Substructure) -> P<Expr> {
191-
let op = if less {ast::BiLt} else {ast::BiGt};
192-
cs_fold(
193-
false, // need foldr,
194-
|cx, span, subexpr, self_f, other_fs| {
195-
/*
196-
build up a series of chain ||'s and &&'s from the inside
197-
out (hence foldr) to get lexical ordering, i.e. for op ==
198-
`ast::lt`
199-
200-
```
201-
self.f1 < other.f1 || (!(other.f1 < self.f1) &&
202-
(self.f2 < other.f2 || (!(other.f2 < self.f2) &&
203-
(false)
204-
))
205-
)
206-
```
207-
208-
The optimiser should remove the redundancy. We explicitly
209-
get use the binops to avoid auto-deref dereferencing too many
210-
layers of pointers, if the type includes pointers.
211-
*/
212-
let other_f = match other_fs {
213-
[ref o_f] => o_f,
214-
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
215-
};
216-
217-
let cmp = cx.expr_binary(span, op, self_f.clone(), other_f.clone());
218-
219-
let not_cmp = cx.expr_unary(span, ast::UnNot,
220-
cx.expr_binary(span, op, other_f.clone(), self_f));
221-
222-
let and = cx.expr_binary(span, ast::BiAnd, not_cmp, subexpr);
223-
cx.expr_binary(span, ast::BiOr, cmp, and)
224-
},
225-
cx.expr_bool(span, equal),
226-
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
227-
if self_args.len() != 2 {
228-
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
229-
} else {
230-
let op = match (less, equal) {
231-
(true, true) => LeOp, (true, false) => LtOp,
232-
(false, true) => GeOp, (false, false) => GtOp,
233-
};
234-
some_ordering_collapsed(cx, span, op, tag_tuple)
138+
ordering_collapsed(cx, span, tag_tuple)
235139
}
236140
}),
237141
cx, span, substr)

0 commit comments

Comments
 (0)