Skip to content

Commit 3c442b9

Browse files
author
Jorge Aparicio
committed
syntax: Use UFCS in the expansion of #[deriving(PartialOrd)]
1 parent 0a3cbf8 commit 3c442b9

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

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

+25-4
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(Ord)`"),
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,

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)