Skip to content

Commit b80f0a3

Browse files
committed
Use the method name 'unary-' for overloading negation
It's less likely to clash with something than 'neg'. Issue #1520
1 parent 7b67e2d commit b80f0a3

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

src/comp/middle/typeck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
19031903
oper_t = structurally_resolved_type(fcx, oper.span, oper_t);
19041904
if !(ty::type_is_integral(tcx, oper_t) ||
19051905
ty::type_is_fp(tcx, oper_t)) {
1906-
oper_t = check_user_unop(fcx, "-", "neg", expr, oper_t);
1906+
oper_t = check_user_unop(fcx, "-", "unary-", expr, oper_t);
19071907
}
19081908
}
19091909
}

src/comp/syntax/parse/parser.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,11 @@ fn parse_method_name(p: parser) -> ast::ident {
18291829
token::BINOP(op) { p.bump(); token::binop_to_str(op) }
18301830
token::NOT { p.bump(); "!" }
18311831
token::LBRACKET { p.bump(); expect(p, token::RBRACKET); "[]" }
1832-
_ { parse_value_ident(p) }
1832+
_ {
1833+
let id = parse_value_ident(p);
1834+
if id == "unary" && eat(p, token::BINOP(token::MINUS)) { "unary-" }
1835+
else { id }
1836+
}
18331837
}
18341838
}
18351839

src/test/run-pass/operator-overloading.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ impl point_ops for point {
77
fn -(other: point) -> point {
88
{x: self.x - other.x, y: self.y - other.y}
99
}
10-
fn neg() -> point {
10+
fn unary-() -> point {
1111
{x: -self.x, y: -self.y}
1212
}
1313
fn [](x: bool) -> int {

0 commit comments

Comments
 (0)