Skip to content

Commit de4b383

Browse files
committed
Properly typecheck unary minus
Closes #813
1 parent e8d170b commit de4b383

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Diff for: src/comp/middle/typeck.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,15 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr,
18421842
ty_to_str(tcx, oper_t)));
18431843
}
18441844
}
1845-
_ { oper_t = do_autoderef(fcx, expr.span, oper_t); }
1845+
ast::neg. {
1846+
oper_t = structurally_resolved_type
1847+
(fcx, oper.span, do_autoderef(fcx, expr.span, oper_t));
1848+
if !(ty::type_is_integral(tcx, oper_t) ||
1849+
ty::type_is_fp(tcx, oper_t)) {
1850+
tcx.sess.span_fatal(expr.span, "applying unary minus to \
1851+
non-numeric type " + ty_to_str(tcx, oper_t));
1852+
}
1853+
}
18461854
}
18471855
write::ty_only_fixup(fcx, id, oper_t);
18481856
}

Diff for: src/test/compile-fail/minus-string.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:applying unary minus to non-numeric type str
2+
3+
fn main() {
4+
-"foo";
5+
}

0 commit comments

Comments
 (0)