diff options
| author | Jorge Aparicio <japaricious@gmail.com> | 2014-11-07 17:16:59 -0500 |
|---|---|---|
| committer | Jorge Aparicio <japaricious@gmail.com> | 2014-11-07 18:26:44 -0500 |
| commit | 3c442b92ae2dc684e02f9668eb8c54aad55f1d9d (patch) | |
| tree | 6e993d1cb3606d0ed1ecdc5d97efd6cf96844435 /src/libsyntax | |
| parent | 0a3cbf8cf44e41072c11277363a5100cf3a8a161 (diff) | |
| download | rust-3c442b92ae2dc684e02f9668eb8c54aad55f1d9d.tar.gz rust-3c442b92ae2dc684e02f9668eb8c54aad55f1d9d.zip | |
syntax: Use UFCS in the expansion of `#[deriving(PartialOrd)]`
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/deriving/cmp/ord.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs index 7cb61d295c0..8f3b1edb59f 100644 --- a/src/libsyntax/ext/deriving/cmp/ord.rs +++ b/src/libsyntax/ext/deriving/cmp/ord.rs @@ -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 { @@ -124,11 +131,11 @@ 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 @@ -136,6 +143,20 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, // __test // } + let new = { + let other_f = match other_fs { + [ref o_f] => o_f, + _ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`"), + }; + + 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, |
