Skip to content

Commit 46aec2c

Browse files
committed
rustc: Record the parent function of each function
1 parent 8f071bb commit 46aec2c

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/rustc/middle/region.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import std::map::hashmap;
1212
type region_map = {
1313
/* Mapping from a block to its parent block, if there is one. */
1414
parent_blocks: hashmap<ast::node_id,ast::node_id>,
15+
/* Mapping from a lambda to its parent function, if there is one. */
16+
parent_fns: hashmap<ast::node_id,ast::node_id>,
1517
/* Mapping from a region type in the AST to its resolved region. */
1618
ast_type_to_region: hashmap<ast::node_id,ty::region>,
1719
/* Mapping from a local variable to its containing block. */
@@ -40,7 +42,8 @@ type ctxt = {
4042
*/
4143
mut queued_locals: [ast::node_id],
4244

43-
parent: parent
45+
parent: parent,
46+
mut parent_fn: option<ast::node_id>
4447
};
4548

4649
// Returns true if `subblock` is equal to or is lexically nested inside
@@ -206,10 +209,23 @@ fn resolve_pat(pat: @ast::pat, cx: ctxt, visitor: visit::vt<ctxt>) {
206209
visit::visit_pat(pat, cx, visitor);
207210
}
208211

212+
fn resolve_expr(expr: @ast::expr, cx: ctxt, visitor: visit::vt<ctxt>) {
213+
alt expr.node {
214+
ast::expr_fn(_, _, _, _) | ast::expr_fn_block(_, _) {
215+
let parent_fns = cx.region_map.parent_fns;
216+
parent_fns.insert(expr.id, option::get(cx.parent_fn));
217+
let new_cx = {parent_fn: some(expr.id) with cx};
218+
visit::visit_expr(expr, new_cx, visitor);
219+
}
220+
_ { visit::visit_expr(expr, cx, visitor); }
221+
}
222+
}
223+
209224
fn resolve_item(item: @ast::item, cx: ctxt, visitor: visit::vt<ctxt>) {
210225
// Items create a new outer block scope as far as we're concerned.
211226
let new_cx: ctxt = {names_in_scope: map::new_str_hash(),
212-
parent: pa_item(item.id)
227+
parent: pa_item(item.id),
228+
parent_fn: some(item.id)
213229
with cx};
214230
visit::visit_item(item, new_cx, visitor);
215231
}
@@ -219,17 +235,20 @@ fn resolve_crate(sess: session, def_map: resolve::def_map, crate: @ast::crate)
219235
let cx: ctxt = {sess: sess,
220236
def_map: def_map,
221237
region_map: @{parent_blocks: map::new_int_hash(),
238+
parent_fns: map::new_int_hash(),
222239
ast_type_to_region: map::new_int_hash(),
223240
local_blocks: map::new_int_hash()},
224241
names_in_scope: map::new_str_hash(),
225242
mut queued_locals: [],
226-
parent: pa_crate};
243+
parent: pa_crate,
244+
mut parent_fn: none};
227245
let visitor = visit::mk_vt(@{
228246
visit_block: resolve_block,
229247
visit_item: resolve_item,
230248
visit_ty: resolve_ty,
231249
visit_arm: resolve_arm,
232-
visit_pat: resolve_pat
250+
visit_pat: resolve_pat,
251+
visit_expr: resolve_expr
233252
with *visit::default_visitor()
234253
});
235254
visit::visit_crate(*crate, cx, visitor);

0 commit comments

Comments
 (0)