Skip to content

Check LHS expression of assignment #10123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,11 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
let result_t = fcx.expr_ty(expr);
demand::suptype(fcx, expr.span, result_t, lhs_t);

let tcx = fcx.tcx();
if !ty::expr_is_lval(tcx, fcx.ccx.method_map, lhs) {
tcx.sess.span_err(lhs.span, "illegal left-hand side expression");
}

// Overwrite result of check_binop...this preserves existing behavior
// but seems quite dubious with regard to user-defined methods
// and so forth. - Niko
Expand Down Expand Up @@ -2538,6 +2543,12 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
}
ast::ExprAssign(lhs, rhs) => {
check_assignment(fcx, lhs, rhs, id);

let tcx = fcx.tcx();
if !ty::expr_is_lval(tcx, fcx.ccx.method_map, lhs) {
tcx.sess.span_err(lhs.span, "illegal left-hand side expression");
}

let lhs_ty = fcx.expr_ty(lhs);
let rhs_ty = fcx.expr_ty(rhs);
if ty::type_is_error(lhs_ty) || ty::type_is_error(rhs_ty) {
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/bad-expr-lhs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
1 = 2; //~ ERROR illegal left-hand side expression
1 += 2; //~ ERROR illegal left-hand side expression
(1, 2) = (3, 4); //~ ERROR illegal left-hand side expression

let (a, b) = (1, 2);
(a, b) = (3, 4); //~ ERROR illegal left-hand side expression

None = Some(3); //~ ERROR illegal left-hand side expression
}
1 change: 0 additions & 1 deletion src/test/compile-fail/borrowck-assign-to-constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ static foo: int = 5;

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