Skip to content

Commit a965f49

Browse files
committed
syntax: correct the modifications to deriving(Ord) so that it works.
1 parent ebf7281 commit a965f49

File tree

1 file changed

+13
-30
lines changed
  • src/libsyntax/ext/deriving/cmp

1 file changed

+13
-30
lines changed

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

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ pub fn expand_deriving_ord(cx: @ExtCtxt,
2222
mitem: @meta_item,
2323
in_items: ~[@item]) -> ~[@item] {
2424
macro_rules! md (
25-
($name:expr, $func:expr, $op:expr) => {
25+
($name:expr, $op:expr, $equal:expr) => {
2626
MethodDef {
2727
name: $name,
2828
generics: LifetimeBounds::empty(),
2929
explicit_self: borrowed_explicit_self(),
3030
args: ~[borrowed_self()],
3131
ret_ty: Literal(Path::new(~["bool"])),
3232
const_nonmatching: false,
33-
combine_substructure: |cx, span, substr| $func($op, cx, span, substr)
33+
combine_substructure: |cx, span, substr| cs_op($op, $equal, cx, span, substr)
3434
}
3535
}
3636
);
@@ -40,17 +40,17 @@ pub fn expand_deriving_ord(cx: @ExtCtxt,
4040
additional_bounds: ~[],
4141
generics: LifetimeBounds::empty(),
4242
methods: ~[
43-
md!("lt", cs_strict, true),
44-
md!("le", cs_nonstrict, true), // inverse operation
45-
md!("gt", cs_strict, false),
46-
md!("ge", cs_nonstrict, false)
43+
md!("lt", true, false),
44+
md!("le", true, true),
45+
md!("gt", false, false),
46+
md!("ge", false, true)
4747
]
4848
};
4949
trait_def.expand(cx, span, mitem, in_items)
5050
}
5151

5252
/// Strict inequality.
53-
fn cs_strict(less: bool, cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr {
53+
fn cs_op(less: bool, equal: bool, cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr {
5454
let op = if less {ast::lt} else {ast::gt};
5555
cs_fold(
5656
false, // need foldr,
@@ -81,16 +81,15 @@ fn cs_strict(less: bool, cx: @ExtCtxt, span: span, substr: &Substructure) -> @ex
8181
cx.expr_deref(span, self_f),
8282
cx.expr_deref(span, other_f));
8383

84-
let not_cmp = cx.expr_binary(span, op,
85-
cx.expr_deref(span, other_f),
86-
cx.expr_deref(span, self_f));
87-
let not_cmp = cx.expr_unary(span, ast::not, not_cmp);
84+
let not_cmp = cx.expr_unary(span, ast::not,
85+
cx.expr_binary(span, op,
86+
cx.expr_deref(span, other_f),
87+
cx.expr_deref(span, self_f)));
8888

89-
let and = cx.expr_binary(span, ast::and,
90-
not_cmp, subexpr);
89+
let and = cx.expr_binary(span, ast::and, not_cmp, subexpr);
9190
cx.expr_binary(span, ast::or, cmp, and)
9291
},
93-
cx.expr_bool(span, false),
92+
cx.expr_bool(span, equal),
9493
|cx, span, args, _| {
9594
// nonmatching enums, order by the order the variants are
9695
// written
@@ -108,19 +107,3 @@ fn cs_strict(less: bool, cx: @ExtCtxt, span: span, substr: &Substructure) -> @ex
108107
},
109108
cx, span, substr)
110109
}
111-
112-
fn cs_nonstrict(less: bool, cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr {
113-
// Example: ge becomes !(*self < *other), le becomes !(*self > *other)
114-
115-
let inverse_op = if less {ast::gt} else {ast::lt};
116-
match substr.self_args {
117-
[self_, other] => {
118-
let inverse_cmp = cx.expr_binary(span, inverse_op,
119-
cx.expr_deref(span, self_),
120-
cx.expr_deref(span, other));
121-
122-
cx.expr_unary(span, ast::not, inverse_cmp)
123-
}
124-
_ => cx.span_bug(span, "Not exactly 2 arguments in `deriving(Ord)`")
125-
}
126-
}

0 commit comments

Comments
 (0)