Skip to content

Commit

Permalink
Resolve loop collection expressions in the right scope
Browse files Browse the repository at this point in the history
Closes #745
  • Loading branch information
marijnh committed Jul 26, 2011
1 parent 41d27dd commit 2509a3d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
26 changes: 15 additions & 11 deletions src/comp/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,19 @@ fn visit_arm_with_scope(&ast::arm a, &scopes sc, &vt[scopes] v) {
}

fn visit_expr_with_scope(&@ast::expr x, &scopes sc, &vt[scopes] v) {
auto new_sc =
alt (x.node) {
ast::expr_for(?d, _, _) | ast::expr_for_each(?d, _, _) {
cons[scope](scope_loop(d), @sc)
}
ast::expr_fn(?f) { cons(scope_fn(f.decl, ~[]), @sc) }
_ { sc }
};
visit::visit_expr(x, new_sc, v);
alt (x.node) {
ast::expr_for(?decl, ?coll, ?blk) |
ast::expr_for_each(?decl, ?coll, ?blk) {
auto new_sc = cons[scope](scope_loop(decl), @sc);
v.visit_expr(coll, sc, v);
v.visit_local(decl, new_sc, v);
v.visit_block(blk, new_sc, v);
}
ast::expr_fn(?f) {
visit::visit_expr(x, cons(scope_fn(f.decl, ~[]), @sc), v);
}
_ { visit::visit_expr(x, sc, v); }
};
}

fn follow_import(&env e, &scopes sc, &ident[] path, &span sp)
Expand Down Expand Up @@ -1380,7 +1384,7 @@ fn check_expr(&@env e, &@ast::expr ex, &() x, &vt[()] v) {
alt ex.node {
ast::expr_rec(?fields, _) {
fn field_name(&ast::field f) -> ident { ret f.node.ident; }
ensure_unique(*e, ex.span, fields, field_name, "field name");
ensure_unique(*e, ex.span, fields, field_name, "field");
}
_ {}
}
Expand All @@ -1391,7 +1395,7 @@ fn check_ty(&@env e, &@ast::ty ty, &() x, &vt[()] v) {
alt ty.node {
ast::ty_rec(?fields) {
fn field_name(&ast::ty_field f) -> ident { ret f.node.ident; }
ensure_unique(*e, ty.span, fields, field_name, "field name");
ensure_unique(*e, ty.span, fields, field_name, "field");
}
_ {}
}
Expand Down
7 changes: 1 addition & 6 deletions src/comp/syntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,7 @@ fn visit_expr[E](&@expr ex, &E e, &vt[E] v) {
v.visit_expr(x, e, v);
v.visit_block(b, e, v);
}
case (expr_for(?dcl, ?x, ?b)) {
v.visit_local(dcl, e, v);
v.visit_expr(x, e, v);
v.visit_block(b, e, v);
}
case (expr_for_each(?dcl, ?x, ?b)) {
expr_for(?dcl, ?x, ?b) | expr_for_each(?dcl, ?x, ?b) {
v.visit_local(dcl, e, v);
v.visit_expr(x, e, v);
v.visit_block(b, e, v);
Expand Down
6 changes: 6 additions & 0 deletions src/test/run-pass/loop-scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
auto x = ~[10, 20, 30];
auto sum = 0;
for (auto x in x) { sum += x; }
assert sum == 60;
}

0 comments on commit 2509a3d

Please sign in to comment.