Skip to content

Commit 946225d

Browse files
committed
auto merge of #18755 : japaric/rust/ord, r=alexcrichton
Closes #18738 cc #15689 r? @alexcrichton cc @cmr
2 parents a6b7091 + 54068ee commit 946225d

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
3030
|cx, span, subexpr, self_f, other_fs| {
3131
let other_f = match other_fs {
3232
[ref o_f] => o_f,
33-
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Eq)`")
33+
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialEq)`")
3434
};
3535

3636
let eq = cx.expr_binary(span, ast::BiEq, self_f, other_f.clone());
@@ -47,7 +47,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
4747
|cx, span, subexpr, self_f, other_fs| {
4848
let other_f = match other_fs {
4949
[ref o_f] => o_f,
50-
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Eq)`")
50+
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialEq)`")
5151
};
5252

5353
let eq = cx.expr_binary(span, ast::BiNe, self_f, other_f.clone());

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

+28-7
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,19 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
107107
let ordering = cx.expr_path(ordering);
108108
let equals_expr = cx.expr_some(span, ordering);
109109

110+
let partial_cmp_path = vec![
111+
cx.ident_of("std"),
112+
cx.ident_of("cmp"),
113+
cx.ident_of("PartialOrd"),
114+
cx.ident_of("partial_cmp"),
115+
];
116+
110117
/*
111118
Builds:
112119
113-
let __test = self_field1.partial_cmp(&other_field2);
120+
let __test = ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1);
114121
if __test == ::std::option::Some(::std::cmp::Equal) {
115-
let __test = self_field2.partial_cmp(&other_field2);
122+
let __test = ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2);
116123
if __test == ::std::option::Some(::std::cmp::Equal) {
117124
...
118125
} else {
@@ -124,18 +131,32 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
124131
125132
FIXME #6449: These `if`s could/should be `match`es.
126133
*/
127-
cs_same_method_fold(
134+
cs_fold(
128135
// foldr nests the if-elses correctly, leaving the first field
129136
// as the outermost one, and the last as the innermost.
130137
false,
131-
|cx, span, old, new| {
138+
|cx, span, old, self_f, other_fs| {
132139
// let __test = new;
133140
// if __test == Some(::std::cmp::Equal) {
134141
// old
135142
// } else {
136143
// __test
137144
// }
138145

146+
let new = {
147+
let other_f = match other_fs {
148+
[ref o_f] => o_f,
149+
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialOrd)`"),
150+
};
151+
152+
let args = vec![
153+
cx.expr_addr_of(span, self_f),
154+
cx.expr_addr_of(span, other_f.clone()),
155+
];
156+
157+
cx.expr_call_global(span, partial_cmp_path.clone(), args)
158+
};
159+
139160
let assign = cx.stmt_let(span, false, test_id, new);
140161

141162
let cond = cx.expr_binary(span, ast::BiEq,
@@ -149,7 +170,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
149170
equals_expr.clone(),
150171
|cx, span, (self_args, tag_tuple), _non_self_args| {
151172
if self_args.len() != 2 {
152-
cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
173+
cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialOrd)`")
153174
} else {
154175
some_ordering_collapsed(cx, span, PartialCmpOp, tag_tuple)
155176
}
@@ -183,7 +204,7 @@ fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
183204
*/
184205
let other_f = match other_fs {
185206
[ref o_f] => o_f,
186-
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
207+
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialOrd)`")
187208
};
188209

189210
let cmp = cx.expr_binary(span, op, self_f.clone(), other_f.clone());
@@ -197,7 +218,7 @@ fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
197218
cx.expr_bool(span, equal),
198219
|cx, span, (self_args, tag_tuple), _non_self_args| {
199220
if self_args.len() != 2 {
200-
cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
221+
cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialOrd)`")
201222
} else {
202223
let op = match (less, equal) {
203224
(true, true) => LeOp, (true, false) => LtOp,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt, span: Span,
108108
cx.expr_path(equals_path.clone()),
109109
|cx, span, (self_args, tag_tuple), _non_self_args| {
110110
if self_args.len() != 2 {
111-
cx.span_bug(span, "not exactly 2 arguments in `deriving(TotalOrd)`")
111+
cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
112112
} else {
113113
ordering_collapsed(cx, span, tag_tuple)
114114
}

src/test/run-pass/issue-18738.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[deriving(PartialEq, PartialOrd)]
12+
enum Test<'a> {
13+
Int(&'a int),
14+
Slice(&'a [u8]),
15+
}
16+
17+
#[deriving(PartialEq, PartialOrd)]
18+
struct Version {
19+
vendor_info: &'static str
20+
}
21+
22+
#[deriving(PartialEq, PartialOrd)]
23+
struct Foo(&'static str);
24+
25+
fn main() {}

0 commit comments

Comments
 (0)