Skip to content

Commit

Permalink
Fix #1474: check that block args resolved to bare fns do not make use…
Browse files Browse the repository at this point in the history
… of upvars
  • Loading branch information
nikomatsakis committed Jan 9, 2012
1 parent 8944a39 commit 51364b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/comp/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ fn check_crate(tcx: ty::ctxt, method_map: typeck::method_map,
// Yields the appropriate function to check the kind of closed over
// variables. `id` is the node_id for some expression that creates the
// closure.
fn with_closure_check_fn(cx: ctx, id: node_id,
b: block(fn(ctx, ty::t, sp: span))) {
fn with_appropriate_checker(cx: ctx, id: node_id,
b: block(fn(ctx, ty::t, sp: span))) {
let fty = ty::node_id_to_monotype(cx.tcx, id);
alt ty::ty_fn_proto(cx.tcx, fty) {
proto_send. { b(check_send); }
proto_shared(_) { b(check_copy); }
proto_block. | proto_bare. { /* no check needed */ }
proto_block. { /* no check needed */ }
proto_bare. { b(check_none); }
}
}

Expand All @@ -81,11 +82,11 @@ fn check_fn(fk: visit::fn_kind, decl: fn_decl, body: blk, sp: span,
// "future-proof" to do it this way, as check_fn_body() is supposed to be
// the common flow point for all functions that appear in the AST.

with_closure_check_fn(cx, id) { |check_fn|
with_appropriate_checker(cx, id) { |checker|
for @{def, span} in *freevars::get_freevars(cx.tcx, id) {
let id = ast_util::def_id_of_def(def).node;
let ty = ty::node_id_to_type(cx.tcx, id);
check_fn(cx, ty, span);
checker(cx, ty, span);
}
}

Expand All @@ -96,21 +97,21 @@ fn check_fn_cap_clause(cx: ctx,
id: node_id,
cap_clause: capture_clause) {
// Check that the variables named in the clause which are not free vars
// (if any) are also legal. freevars are checked above in check_fn_body.
// (if any) are also legal. freevars are checked above in check_fn().
// This is kind of a degenerate case, as captured variables will generally
// appear free in the body.
let freevars = freevars::get_freevars(cx.tcx, id);
let freevar_ids = vec::map(*freevars, { |freevar|
ast_util::def_id_of_def(freevar.def).node
});
//log("freevar_ids", freevar_ids);
with_closure_check_fn(cx, id) { |check_fn|
with_appropriate_checker(cx, id) { |checker|
let check_var = lambda(&&cap_item: @capture_item) {
let cap_def = cx.tcx.def_map.get(cap_item.id);
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
if !vec::member(cap_def_id, freevar_ids) {
let ty = ty::node_id_to_type(cx.tcx, cap_def_id);
check_fn(cx, ty, cap_item.span);
checker(cx, ty, cap_item.span);
}
};
vec::iter(cap_clause.copies, check_var);
Expand Down Expand Up @@ -240,6 +241,10 @@ fn check_send(cx: ctx, ty: ty::t, sp: span) {
}
}

fn check_none(cx: ctx, _ty: ty::t, sp: span) {
cx.tcx.sess.span_err(sp, "attempted dynamic environment capture");
}

//
// Local Variables:
// mode: rust
Expand Down
7 changes: 7 additions & 0 deletions src/test/compile-fail/bad-var-env-capture-in-block-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let x = 3;
fn blah(_a: fn()) {}
blah({||
log(debug, x); //! ERROR attempted dynamic environment capture
});
}

0 comments on commit 51364b5

Please sign in to comment.