Skip to content

Commit bdb99e4

Browse files
committed
Fix hygeine issue in #[derive(PartialOrd)]
Currently defining a constant named `__cmp` breaks `#[derive(PartialOrd)]`. This is ... not great.
1 parent d5a304e commit bdb99e4

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/libsyntax_ext/deriving/cmp/partial_ord.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use deriving::generic::*;
1515
use deriving::generic::ty::*;
1616

1717
use syntax::ast::{self, BinOpKind, Expr, MetaItem};
18+
use syntax::codemap::respan;
1819
use syntax::ext::base::{Annotatable, ExtCtxt};
1920
use syntax::ext::build::AstBuilder;
2021
use syntax::ptr::P;
@@ -123,7 +124,7 @@ pub fn some_ordering_collapsed(cx: &mut ExtCtxt,
123124
}
124125

125126
pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
126-
let test_id = cx.ident_of("__cmp");
127+
let test_id = cx.ident_of("cmp");
127128
let ordering = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"]));
128129
let ordering_expr = cx.expr_path(ordering.clone());
129130
let equals_expr = cx.expr_some(span, ordering_expr);
@@ -138,18 +139,19 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<
138139
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) => {
139140
// ...
140141
// }
141-
// __cmp => __cmp
142+
// cmp @ _ => cmp
142143
// },
143-
// __cmp => __cmp
144+
// cmp @ _ => cmp
144145
// }
145146
//
147+
// We use the `cmp @ _` form so it will not conflict with constants named `cmp`
146148
cs_fold(// foldr nests the if-elses correctly, leaving the first field
147149
// as the outermost one, and the last as the innermost.
148150
false,
149151
|cx, span, old, self_f, other_fs| {
150152
// match new {
151153
// Some(::std::cmp::Ordering::Equal) => old,
152-
// __cmp => __cmp
154+
// cmp @ _ => cmp
153155
// }
154156

155157
let new = {
@@ -169,8 +171,11 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<
169171
let eq_arm = cx.arm(span,
170172
vec![cx.pat_some(span, cx.pat_path(span, ordering.clone()))],
171173
old);
174+
// cmp @ _
175+
let kind = ast::PatKind::Ident(ast::BindingMode::ByValue(ast::Mutability::Immutable),
176+
respan(span, test_id), Some(cx.pat_wild(span)));
172177
let neq_arm = cx.arm(span,
173-
vec![cx.pat_ident(span, test_id)],
178+
vec![cx.pat(span, kind)],
174179
cx.expr_ident(span, test_id));
175180

176181
cx.expr_match(span, new, vec![eq_arm, neq_arm])

0 commit comments

Comments
 (0)