Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: Use UFCS in the expansion of #[deriving(PartialOrd)] #18755

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libsyntax/ext/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
|cx, span, subexpr, self_f, other_fs| {
let other_f = match other_fs {
[ref o_f] => o_f,
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Eq)`")
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialEq)`")
};

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

let eq = cx.expr_binary(span, ast::BiNe, self_f, other_f.clone());
Expand Down
35 changes: 28 additions & 7 deletions src/libsyntax/ext/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,19 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
let ordering = cx.expr_path(ordering);
let equals_expr = cx.expr_some(span, ordering);

let partial_cmp_path = vec![
cx.ident_of("std"),
cx.ident_of("cmp"),
cx.ident_of("PartialOrd"),
cx.ident_of("partial_cmp"),
];

/*
Builds:

let __test = self_field1.partial_cmp(&other_field2);
let __test = ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1);
if __test == ::std::option::Some(::std::cmp::Equal) {
let __test = self_field2.partial_cmp(&other_field2);
let __test = ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2);
if __test == ::std::option::Some(::std::cmp::Equal) {
...
} else {
Expand All @@ -124,18 +131,32 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,

FIXME #6449: These `if`s could/should be `match`es.
*/
cs_same_method_fold(
cs_fold(
// foldr nests the if-elses correctly, leaving the first field
// as the outermost one, and the last as the innermost.
false,
|cx, span, old, new| {
|cx, span, old, self_f, other_fs| {
// let __test = new;
// if __test == Some(::std::cmp::Equal) {
// old
// } else {
// __test
// }

let new = {
let other_f = match other_fs {
[ref o_f] => o_f,
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialOrd)`"),
};

let args = vec![
cx.expr_addr_of(span, self_f),
cx.expr_addr_of(span, other_f.clone()),
];

cx.expr_call_global(span, partial_cmp_path.clone(), args)
};

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

let cond = cx.expr_binary(span, ast::BiEq,
Expand All @@ -149,7 +170,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
equals_expr.clone(),
|cx, span, (self_args, tag_tuple), _non_self_args| {
if self_args.len() != 2 {
cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialOrd)`")
} else {
some_ordering_collapsed(cx, span, PartialCmpOp, tag_tuple)
}
Expand Down Expand Up @@ -183,7 +204,7 @@ fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
*/
let other_f = match other_fs {
[ref o_f] => o_f,
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialOrd)`")
};

let cmp = cx.expr_binary(span, op, self_f.clone(), other_f.clone());
Expand All @@ -197,7 +218,7 @@ fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
cx.expr_bool(span, equal),
|cx, span, (self_args, tag_tuple), _non_self_args| {
if self_args.len() != 2 {
cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialOrd)`")
} else {
let op = match (less, equal) {
(true, true) => LeOp, (true, false) => LtOp,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/deriving/cmp/totalord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt, span: Span,
cx.expr_path(equals_path.clone()),
|cx, span, (self_args, tag_tuple), _non_self_args| {
if self_args.len() != 2 {
cx.span_bug(span, "not exactly 2 arguments in `deriving(TotalOrd)`")
cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
} else {
ordering_collapsed(cx, span, tag_tuple)
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/run-pass/issue-18738.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[deriving(PartialEq, PartialOrd)]
enum Test<'a> {
Int(&'a int),
Slice(&'a [u8]),
}

#[deriving(PartialEq, PartialOrd)]
struct Version {
vendor_info: &'static str
}

#[deriving(PartialEq, PartialOrd)]
struct Foo(&'static str);

fn main() {}