1 import syntax::{ast, ast_util};
7 export check_capture_clause;
8 export compute_capture_vars;
15 cap_copy; //< Copy the value into the closure.
16 cap_move; //< Move the value into the closure.
17 cap_drop; //< Drop value after creating closure.
18 cap_ref; //< Reference directly from parent stack frame (block fn).
22 def: ast::def, //< The variable being accessed free.
23 mode: capture_mode //< How is the variable being accessed.
26 type capture_map = map::hashmap<ast::def_id, capture_var>;
28 // checks the capture clause for a fn_expr() and issues warnings or
29 // errors for any irregularities which we identify.
30 fn check_capture_clause(tcx: ty::ctxt,
31 fn_expr_id: ast::node_id,
33 cap_clause: ast::capture_clause) {
34 let freevars = freevars::get_freevars(tcx, fn_expr_id);
35 let seen_defs = map::new_int_hash();
37 let check_capture_item = fn@(&&cap_item: @ast::capture_item) {
38 let cap_def = tcx.def_map.get(cap_item.id);
39 if !vec::any(*freevars, {|fv| fv.def == cap_def}) {
42 #fmt("Captured variable '%s' not used in closure",
46 let cap_def_id = ast_util::def_id_of_def(cap_def).node;
47 if !seen_defs.insert(cap_def_id, ()) {
50 #fmt("Variable '%s' captured more than once",
55 let check_not_upvar = fn@(&&cap_item: @ast::capture_item) {
56 alt tcx.def_map.get(cap_item.id) {
57 ast::def_upvar(_, _, _) {
60 #fmt("Upvars (like '%s') cannot be moved into a closure",
67 let check_block_captures = fn@(v: [@ast::capture_item]) {
68 if check vec::is_not_empty(v) {
69 let cap_item0 = vec::head(v);
72 "Cannot capture values explicitly with a block closure");
78 check_block_captures(cap_clause.copies);
79 check_block_captures(cap_clause.moves);
81 ast::proto_bare. | ast::proto_shared. | ast::proto_send. {
82 vec::iter(cap_clause.copies, check_capture_item);
83 vec::iter(cap_clause.moves, check_capture_item);
84 vec::iter(cap_clause.moves, check_not_upvar);
89 fn compute_capture_vars(tcx: ty::ctxt,
90 fn_expr_id: ast::node_id,
92 cap_clause: ast::capture_clause) -> [capture_var] {
93 let freevars = freevars::get_freevars(tcx, fn_expr_id);
94 let cap_map = map::new_int_hash();
96 vec::iter(cap_clause.copies) { |cap_item|
97 let cap_def = tcx.def_map.get(cap_item.id);
98 let cap_def_id = ast_util::def_id_of_def(cap_def).node;
99 if vec::any(*freevars, {|fv| fv.def == cap_def}) {
100 cap_map.insert(cap_def_id, { def:cap_def, mode:cap_copy });
104 vec::iter(cap_clause.moves) { |cap_item|
105 let cap_def = tcx.def_map.get(cap_item.id);
106 let cap_def_id = ast_util::def_id_of_def(cap_def).node;
107 if vec::any(*freevars, {|fv| fv.def == cap_def}) {
108 cap_map.insert(cap_def_id, { def:cap_def, mode:cap_move });
110 cap_map.insert(cap_def_id, { def:cap_def, mode:cap_drop });
114 let implicit_mode = alt fn_proto {
115 ast::proto_block. { cap_ref }
116 ast::proto_bare. | ast::proto_shared. | ast::proto_send. { cap_copy }
119 vec::iter(*freevars) { |fvar|
120 let fvar_def_id = ast_util::def_id_of_def(fvar.def).node;
121 alt cap_map.find(fvar_def_id) {
122 option::some(_) { /* was explicitly named, do nothing */ }
124 cap_map.insert(fvar_def_id, {def:fvar.def, mode:implicit_mode});
130 cap_map.values { |cap_var| result += [cap_var]; }