Skip to content

Commit a3301f7

Browse files
committed
Generalize collect_upvars to work over any type of ast node.
1 parent 20e94de commit a3301f7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/comp/middle/trans.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,10 +4073,13 @@ fn trans_for(&@block_ctxt cx, &@ast::local local, &@ast::expr seq,
40734073

40744074
// Iterator translation
40754075

4076-
// Searches through a block for all references to locals or upvars in this
4077-
// frame and returns the list of definition IDs thus found.
4078-
fn collect_upvars(&@block_ctxt cx, &ast::block bloc,
4079-
ast::node_id initial_decl) -> ast::node_id[] {
4076+
// Searches through part of the AST for all references to locals or
4077+
// upvars in this frame and returns the list of definition IDs thus found.
4078+
// Since we want to be able to collect upvars in some arbitrary piece
4079+
// of the AST, we take a walker function that we invoke with a visitor
4080+
// in order to start the search.
4081+
fn collect_upvars(&@block_ctxt cx, &fn (&walk::ast_visitor) walker,
4082+
ast::node_id[] initial_decls) -> ast::node_id[] {
40804083
type env =
40814084
@rec(mutable ast::node_id[] refs,
40824085
hashmap[ast::node_id, ()] decls,
@@ -4112,7 +4115,8 @@ fn collect_upvars(&@block_ctxt cx, &ast::block bloc,
41124115
}
41134116
}
41144117
let hashmap[ast::node_id, ()] decls = new_int_hash[()]();
4115-
decls.insert(initial_decl, ());
4118+
for (ast::node_id decl in initial_decls) { decls.insert(decl, ()); }
4119+
41164120
let env e =
41174121
@rec(mutable refs=~[],
41184122
decls=decls,
@@ -4123,7 +4127,7 @@ fn collect_upvars(&@block_ctxt cx, &ast::block bloc,
41234127
visit_expr_pre=bind walk_expr(e, _),
41244128
visit_pat_pre=bind walk_pat(e, _)
41254129
with walk::default_visitor());
4126-
walk::walk_block(*visitor, bloc);
4130+
walker(*visitor);
41274131
// Calculate (refs - decls). This is the set of captured upvars.
41284132

41294133
let ast::node_id[] result = ~[];
@@ -4310,7 +4314,8 @@ fn trans_for_each(&@block_ctxt cx, &@ast::local local, &@ast::expr seq,
43104314
// FIXME: possibly support alias-mode here?
43114315
auto decl_ty = node_id_type(lcx.ccx, local.node.id);
43124316
auto decl_id = local.node.id;
4313-
auto upvars = collect_upvars(cx, body, decl_id);
4317+
auto upvars = collect_upvars(cx, bind walk::walk_block(_, body),
4318+
~[decl_id]);
43144319

43154320
auto environment_data = build_environment(cx, upvars);
43164321
auto llenvptr = environment_data._0;

0 commit comments

Comments
 (0)