Skip to content

Commit b944d8d

Browse files
msullivangraydon
authored andcommitted
Require that both sides of a swap be lvals.
1 parent d79330d commit b944d8d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/comp/middle/alias.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ fn visit_expr(@ctx cx, &@ast::expr ex, &scope sc, &vt[scope] v) {
111111
check_var(*cx, ex, pt, ex.id, false, sc);
112112
handled = false;
113113
}
114+
case (ast::expr_swap(?lhs, ?rhs)) {
115+
check_lval(cx, lhs, sc, v);
116+
check_lval(cx, rhs, sc, v);
117+
}
114118
case (ast::expr_move(?dest, ?src)) {
115119
check_assign(cx, dest, src, sc, v);
116120
}
@@ -376,11 +380,7 @@ fn check_var(&ctx cx, &@ast::expr ex, &ast::path p, ast::node_id id,
376380
}
377381
}
378382

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

421+
fn check_assign(&@ctx cx, &@ast::expr dest, &@ast::expr src, &scope sc,
422+
&vt[scope] v) {
423+
visit_expr(cx, src, sc, v);
424+
check_lval(cx, dest, sc, v);
425+
}
426+
427+
421428
fn is_immutable_alias(&@ctx cx, &scope sc, node_id dnum) -> bool {
422429
alt (cx.local_map.find(dnum)) {
423430
case (some(arg(ast::alias(false)))) { ret true; }

src/test/compile-fail/swap-no-lval.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern: assignment to non-lvalue
2+
3+
fn main() {
4+
5 <-> 3;
5+
}

0 commit comments

Comments
 (0)