From b80f0a3c9fe6a0e6d88a95b0697cb64411152530 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Fri, 27 Jan 2012 10:06:53 +0100 Subject: [PATCH] Use the method name 'unary-' for overloading negation It's less likely to clash with something than 'neg'. Issue #1520 --- src/comp/middle/typeck.rs | 2 +- src/comp/syntax/parse/parser.rs | 6 +++++- src/test/run-pass/operator-overloading.rs | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 743bc190796..1d54ec2befc 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -1903,7 +1903,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier, oper_t = structurally_resolved_type(fcx, oper.span, oper_t); if !(ty::type_is_integral(tcx, oper_t) || ty::type_is_fp(tcx, oper_t)) { - oper_t = check_user_unop(fcx, "-", "neg", expr, oper_t); + oper_t = check_user_unop(fcx, "-", "unary-", expr, oper_t); } } } diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 8b2d6cf36a7..a7bbddfee39 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -1829,7 +1829,11 @@ fn parse_method_name(p: parser) -> ast::ident { token::BINOP(op) { p.bump(); token::binop_to_str(op) } token::NOT { p.bump(); "!" } token::LBRACKET { p.bump(); expect(p, token::RBRACKET); "[]" } - _ { parse_value_ident(p) } + _ { + let id = parse_value_ident(p); + if id == "unary" && eat(p, token::BINOP(token::MINUS)) { "unary-" } + else { id } + } } } diff --git a/src/test/run-pass/operator-overloading.rs b/src/test/run-pass/operator-overloading.rs index 412e1fd538c..42933578b3b 100644 --- a/src/test/run-pass/operator-overloading.rs +++ b/src/test/run-pass/operator-overloading.rs @@ -7,7 +7,7 @@ impl point_ops for point { fn -(other: point) -> point { {x: self.x - other.x, y: self.y - other.y} } - fn neg() -> point { + fn unary-() -> point { {x: -self.x, y: -self.y} } fn [](x: bool) -> int {