2011-12-19 14:50:31 -06:00
|
|
|
import syntax::{ast, ast_util};
|
2012-01-12 10:59:49 -06:00
|
|
|
import driver::session::session;
|
2011-12-19 14:50:31 -06:00
|
|
|
import std::map;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2011-12-19 14:50:31 -06:00
|
|
|
|
|
|
|
export capture_mode;
|
|
|
|
export capture_var;
|
|
|
|
export capture_map;
|
|
|
|
export check_capture_clause;
|
|
|
|
export compute_capture_vars;
|
|
|
|
export cap_copy;
|
|
|
|
export cap_move;
|
|
|
|
export cap_drop;
|
|
|
|
export cap_ref;
|
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum capture_mode {
|
2012-01-19 19:56:05 -06:00
|
|
|
cap_copy, //< Copy the value into the closure.
|
|
|
|
cap_move, //< Move the value into the closure.
|
|
|
|
cap_drop, //< Drop value after creating closure.
|
|
|
|
cap_ref, //< Reference directly from parent stack frame (block fn).
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type capture_var = {
|
|
|
|
def: ast::def, //< The variable being accessed free.
|
|
|
|
mode: capture_mode //< How is the variable being accessed.
|
|
|
|
};
|
|
|
|
|
|
|
|
type capture_map = map::hashmap<ast::def_id, capture_var>;
|
|
|
|
|
|
|
|
// checks the capture clause for a fn_expr() and issues warnings or
|
|
|
|
// errors for any irregularities which we identify.
|
|
|
|
fn check_capture_clause(tcx: ty::ctxt,
|
|
|
|
fn_expr_id: ast::node_id,
|
|
|
|
fn_proto: ast::proto,
|
|
|
|
cap_clause: ast::capture_clause) {
|
|
|
|
let freevars = freevars::get_freevars(tcx, fn_expr_id);
|
|
|
|
let seen_defs = map::new_int_hash();
|
|
|
|
|
2012-01-09 18:12:37 -06:00
|
|
|
let check_capture_item = fn@(&&cap_item: @ast::capture_item) {
|
2011-12-19 14:50:31 -06:00
|
|
|
let cap_def = tcx.def_map.get(cap_item.id);
|
|
|
|
if !vec::any(*freevars, {|fv| fv.def == cap_def}) {
|
|
|
|
tcx.sess.span_warn(
|
|
|
|
cap_item.span,
|
2012-03-05 18:27:27 -06:00
|
|
|
#fmt("captured variable '%s' not used in closure",
|
2011-12-19 14:50:31 -06:00
|
|
|
cap_item.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
|
|
|
if !seen_defs.insert(cap_def_id, ()) {
|
|
|
|
tcx.sess.span_err(
|
|
|
|
cap_item.span,
|
2012-03-05 18:27:27 -06:00
|
|
|
#fmt("variable '%s' captured more than once",
|
2011-12-19 14:50:31 -06:00
|
|
|
cap_item.name));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-09 18:12:37 -06:00
|
|
|
let check_not_upvar = fn@(&&cap_item: @ast::capture_item) {
|
2011-12-19 14:50:31 -06:00
|
|
|
alt tcx.def_map.get(cap_item.id) {
|
|
|
|
ast::def_upvar(_, _, _) {
|
|
|
|
tcx.sess.span_err(
|
|
|
|
cap_item.span,
|
2012-03-05 18:27:27 -06:00
|
|
|
#fmt("upvars (like '%s') cannot be moved into a closure",
|
2011-12-19 14:50:31 -06:00
|
|
|
cap_item.name));
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-09 18:12:37 -06:00
|
|
|
let check_block_captures = fn@(v: [@ast::capture_item]) {
|
2011-12-19 14:50:31 -06:00
|
|
|
if check vec::is_not_empty(v) {
|
|
|
|
let cap_item0 = vec::head(v);
|
|
|
|
tcx.sess.span_err(
|
|
|
|
cap_item0.span,
|
2012-03-05 18:27:27 -06:00
|
|
|
"cannot capture values explicitly with a block closure");
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
alt fn_proto {
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::proto_any | ast::proto_block {
|
2011-12-19 14:50:31 -06:00
|
|
|
check_block_captures(cap_clause.copies);
|
|
|
|
check_block_captures(cap_clause.moves);
|
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::proto_bare | ast::proto_box | ast::proto_uniq {
|
2011-12-19 14:50:31 -06:00
|
|
|
vec::iter(cap_clause.copies, check_capture_item);
|
|
|
|
vec::iter(cap_clause.moves, check_capture_item);
|
|
|
|
vec::iter(cap_clause.moves, check_not_upvar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_capture_vars(tcx: ty::ctxt,
|
|
|
|
fn_expr_id: ast::node_id,
|
|
|
|
fn_proto: ast::proto,
|
|
|
|
cap_clause: ast::capture_clause) -> [capture_var] {
|
|
|
|
let freevars = freevars::get_freevars(tcx, fn_expr_id);
|
|
|
|
let cap_map = map::new_int_hash();
|
|
|
|
|
|
|
|
vec::iter(cap_clause.copies) { |cap_item|
|
|
|
|
let cap_def = tcx.def_map.get(cap_item.id);
|
|
|
|
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
|
|
|
if vec::any(*freevars, {|fv| fv.def == cap_def}) {
|
|
|
|
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_copy });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vec::iter(cap_clause.moves) { |cap_item|
|
|
|
|
let cap_def = tcx.def_map.get(cap_item.id);
|
|
|
|
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
|
|
|
if vec::any(*freevars, {|fv| fv.def == cap_def}) {
|
|
|
|
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_move });
|
|
|
|
} else {
|
|
|
|
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_drop });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let implicit_mode = alt fn_proto {
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::proto_any | ast::proto_block { cap_ref }
|
|
|
|
ast::proto_bare | ast::proto_box | ast::proto_uniq { cap_copy }
|
2011-12-19 14:50:31 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
vec::iter(*freevars) { |fvar|
|
|
|
|
let fvar_def_id = ast_util::def_id_of_def(fvar.def).node;
|
|
|
|
alt cap_map.find(fvar_def_id) {
|
|
|
|
option::some(_) { /* was explicitly named, do nothing */ }
|
2012-01-19 00:37:22 -06:00
|
|
|
option::none {
|
2011-12-19 14:50:31 -06:00
|
|
|
cap_map.insert(fvar_def_id, {def:fvar.def, mode:implicit_mode});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
cap_map.values { |cap_var| result += [cap_var]; }
|
|
|
|
ret result;
|
|
|
|
}
|