Skip to content

Require that both sides of a swap be lvals. #590

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 12 additions & 5 deletions src/comp/middle/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ fn visit_expr(@ctx cx, &@ast::expr ex, &scope sc, &vt[scope] v) {
check_var(*cx, ex, pt, ex.id, false, sc);
handled = false;
}
case (ast::expr_swap(?lhs, ?rhs)) {
check_lval(cx, lhs, sc, v);
check_lval(cx, rhs, sc, v);
}
case (ast::expr_move(?dest, ?src)) {
check_assign(cx, dest, src, sc, v);
}
Expand Down Expand Up @@ -376,11 +380,7 @@ fn check_var(&ctx cx, &@ast::expr ex, &ast::path p, ast::node_id id,
}
}


// FIXME does not catch assigning to immutable object fields yet
fn check_assign(&@ctx cx, &@ast::expr dest, &@ast::expr src, &scope sc,
&vt[scope] v) {
visit_expr(cx, src, sc, v);
fn check_lval(&@ctx cx, &@ast::expr dest, &scope sc, &vt[scope] v) {
alt (dest.node) {
case (ast::expr_path(?p)) {
auto dnum = ast::def_id_of_def(cx.tcx.def_map.get(dest.id))._1;
Expand Down Expand Up @@ -418,6 +418,13 @@ fn check_assign(&@ctx cx, &@ast::expr dest, &@ast::expr src, &scope sc,
}
}

fn check_assign(&@ctx cx, &@ast::expr dest, &@ast::expr src, &scope sc,
&vt[scope] v) {
visit_expr(cx, src, sc, v);
check_lval(cx, dest, sc, v);
}


fn is_immutable_alias(&@ctx cx, &scope sc, node_id dnum) -> bool {
alt (cx.local_map.find(dnum)) {
case (some(arg(ast::alias(false)))) { ret true; }
Expand Down
5 changes: 5 additions & 0 deletions src/test/compile-fail/swap-no-lval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// error-pattern: assignment to non-lvalue

fn main() {
5 <-> 3;
}