Skip to content

Commit 2509a3d

Browse files
committed
Resolve loop collection expressions in the right scope
Closes #745
1 parent 41d27dd commit 2509a3d

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/comp/middle/resolve.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,19 @@ fn visit_arm_with_scope(&ast::arm a, &scopes sc, &vt[scopes] v) {
372372
}
373373

374374
fn visit_expr_with_scope(&@ast::expr x, &scopes sc, &vt[scopes] v) {
375-
auto new_sc =
376-
alt (x.node) {
377-
ast::expr_for(?d, _, _) | ast::expr_for_each(?d, _, _) {
378-
cons[scope](scope_loop(d), @sc)
379-
}
380-
ast::expr_fn(?f) { cons(scope_fn(f.decl, ~[]), @sc) }
381-
_ { sc }
382-
};
383-
visit::visit_expr(x, new_sc, v);
375+
alt (x.node) {
376+
ast::expr_for(?decl, ?coll, ?blk) |
377+
ast::expr_for_each(?decl, ?coll, ?blk) {
378+
auto new_sc = cons[scope](scope_loop(decl), @sc);
379+
v.visit_expr(coll, sc, v);
380+
v.visit_local(decl, new_sc, v);
381+
v.visit_block(blk, new_sc, v);
382+
}
383+
ast::expr_fn(?f) {
384+
visit::visit_expr(x, cons(scope_fn(f.decl, ~[]), @sc), v);
385+
}
386+
_ { visit::visit_expr(x, sc, v); }
387+
};
384388
}
385389

386390
fn follow_import(&env e, &scopes sc, &ident[] path, &span sp)
@@ -1380,7 +1384,7 @@ fn check_expr(&@env e, &@ast::expr ex, &() x, &vt[()] v) {
13801384
alt ex.node {
13811385
ast::expr_rec(?fields, _) {
13821386
fn field_name(&ast::field f) -> ident { ret f.node.ident; }
1383-
ensure_unique(*e, ex.span, fields, field_name, "field name");
1387+
ensure_unique(*e, ex.span, fields, field_name, "field");
13841388
}
13851389
_ {}
13861390
}
@@ -1391,7 +1395,7 @@ fn check_ty(&@env e, &@ast::ty ty, &() x, &vt[()] v) {
13911395
alt ty.node {
13921396
ast::ty_rec(?fields) {
13931397
fn field_name(&ast::ty_field f) -> ident { ret f.node.ident; }
1394-
ensure_unique(*e, ty.span, fields, field_name, "field name");
1398+
ensure_unique(*e, ty.span, fields, field_name, "field");
13951399
}
13961400
_ {}
13971401
}

src/comp/syntax/visit.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,7 @@ fn visit_expr[E](&@expr ex, &E e, &vt[E] v) {
322322
v.visit_expr(x, e, v);
323323
v.visit_block(b, e, v);
324324
}
325-
case (expr_for(?dcl, ?x, ?b)) {
326-
v.visit_local(dcl, e, v);
327-
v.visit_expr(x, e, v);
328-
v.visit_block(b, e, v);
329-
}
330-
case (expr_for_each(?dcl, ?x, ?b)) {
325+
expr_for(?dcl, ?x, ?b) | expr_for_each(?dcl, ?x, ?b) {
331326
v.visit_local(dcl, e, v);
332327
v.visit_expr(x, e, v);
333328
v.visit_block(b, e, v);

src/test/run-pass/loop-scope.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
auto x = ~[10, 20, 30];
3+
auto sum = 0;
4+
for (auto x in x) { sum += x; }
5+
assert sum == 60;
6+
}

0 commit comments

Comments
 (0)