Skip to content

Commit 47abdbd

Browse files
committed
rustc: Check LHS expression of assignment
Fixes #7507 Fixes #7508
1 parent bee40a9 commit 47abdbd

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/librustc/middle/typeck/check/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,11 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
23662366
let result_t = fcx.expr_ty(expr);
23672367
demand::suptype(fcx, expr.span, result_t, lhs_t);
23682368

2369+
let tcx = fcx.tcx();
2370+
if !ty::expr_is_lval(tcx, fcx.ccx.method_map, lhs) {
2371+
tcx.sess.span_err(lhs.span, "illegal left-hand side expression");
2372+
}
2373+
23692374
// Overwrite result of check_binop...this preserves existing behavior
23702375
// but seems quite dubious with regard to user-defined methods
23712376
// and so forth. - Niko
@@ -2538,6 +2543,12 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
25382543
}
25392544
ast::ExprAssign(lhs, rhs) => {
25402545
check_assignment(fcx, lhs, rhs, id);
2546+
2547+
let tcx = fcx.tcx();
2548+
if !ty::expr_is_lval(tcx, fcx.ccx.method_map, lhs) {
2549+
tcx.sess.span_err(lhs.span, "illegal left-hand side expression");
2550+
}
2551+
25412552
let lhs_ty = fcx.expr_ty(lhs);
25422553
let rhs_ty = fcx.expr_ty(rhs);
25432554
if ty::type_is_error(lhs_ty) || ty::type_is_error(rhs_ty) {

src/test/compile-fail/borrowck-assign-to-constants.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ static foo: int = 5;
1212

1313
fn main() {
1414
// assigning to various global constants
15-
None = Some(3); //~ ERROR cannot assign to immutable static item
1615
foo = 6; //~ ERROR cannot assign to immutable static item
1716
}

0 commit comments

Comments
 (0)