Skip to content

Commit 623daf6

Browse files
committed
auto merge of #10123 : klutzy/rust/fix-7507, r=alexcrichton
This patch fixes rustc to emit explicit error if LHS of assignment is not allowed. Fixes #7507 Fixes #7508
2 parents a0e6e0e + cab0045 commit 623daf6

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,11 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
23732373
let result_t = fcx.expr_ty(expr);
23742374
demand::suptype(fcx, expr.span, result_t, lhs_t);
23752375

2376+
let tcx = fcx.tcx();
2377+
if !ty::expr_is_lval(tcx, fcx.ccx.method_map, lhs) {
2378+
tcx.sess.span_err(lhs.span, "illegal left-hand side expression");
2379+
}
2380+
23762381
// Overwrite result of check_binop...this preserves existing behavior
23772382
// but seems quite dubious with regard to user-defined methods
23782383
// and so forth. - Niko
@@ -2545,6 +2550,12 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
25452550
}
25462551
ast::ExprAssign(lhs, rhs) => {
25472552
check_assignment(fcx, lhs, rhs, id);
2553+
2554+
let tcx = fcx.tcx();
2555+
if !ty::expr_is_lval(tcx, fcx.ccx.method_map, lhs) {
2556+
tcx.sess.span_err(lhs.span, "illegal left-hand side expression");
2557+
}
2558+
25482559
let lhs_ty = fcx.expr_ty(lhs);
25492560
let rhs_ty = fcx.expr_ty(rhs);
25502561
if ty::type_is_error(lhs_ty) || ty::type_is_error(rhs_ty) {

src/test/compile-fail/bad-expr-lhs.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
1 = 2; //~ ERROR illegal left-hand side expression
13+
1 += 2; //~ ERROR illegal left-hand side expression
14+
(1, 2) = (3, 4); //~ ERROR illegal left-hand side expression
15+
16+
let (a, b) = (1, 2);
17+
(a, b) = (3, 4); //~ ERROR illegal left-hand side expression
18+
19+
None = Some(3); //~ ERROR illegal left-hand side expression
20+
}

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

-1
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)