Skip to content

Commit b2b2a43

Browse files
committed
resolve capture clauses
1 parent b0f1a5f commit b2b2a43

File tree

6 files changed

+49
-30
lines changed

6 files changed

+49
-30
lines changed

src/comp/front/test.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,9 @@ fn mk_test_wrapper(cx: test_ctxt,
347347
body: wrapper_body
348348
};
349349

350-
let wrapper_capture: @ast::capture = @{
351-
node: {
352-
copies: [],
353-
moves: []
354-
},
355-
span: span
350+
let wrapper_capture: @ast::capture_clause = @{
351+
copies: [],
352+
moves: []
356353
};
357354

358355
let wrapper_expr: ast::expr = {

src/comp/middle/resolve.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,16 @@ fn check_unused_imports(e: @env) {
299299
};
300300
}
301301

302+
fn resolve_capture_item(e: @env, sc: scopes, &&cap_item: @ast::capture_item) {
303+
let dcur = lookup_in_scope_strict(
304+
*e, sc, cap_item.span, cap_item.name, ns_value);
305+
maybe_insert(e, cap_item.id, dcur);
306+
}
307+
308+
fn maybe_insert(e: @env, id: node_id, def: option::t<def>) {
309+
if option::is_some(def) { e.def_map.insert(id, option::get(def)); }
310+
}
311+
302312
fn resolve_names(e: @env, c: @ast::crate) {
303313
e.used_imports.track = true;
304314
let v =
@@ -325,6 +335,11 @@ fn resolve_names(e: @env, c: @ast::crate) {
325335
lookup_path_strict(*e, sc, exp.span, p.node,
326336
ns_value));
327337
}
338+
ast::expr_fn(_, cap_clause) {
339+
let rci = bind resolve_capture_item(e, sc, _);
340+
vec::iter(cap_clause.copies, rci);
341+
vec::iter(cap_clause.moves, rci);
342+
}
328343
_ { }
329344
}
330345
}
@@ -361,10 +376,6 @@ fn resolve_names(e: @env, c: @ast::crate) {
361376
_ { }
362377
}
363378
}
364-
365-
fn maybe_insert(e: @env, id: node_id, def: option::t<def>) {
366-
if option::is_some(def) { e.def_map.insert(id, option::get(def)); }
367-
}
368379
}
369380

370381

src/comp/syntax/ast.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ tag expr_ {
226226
expr_for(@local, @expr, blk);
227227
expr_do_while(blk, @expr);
228228
expr_alt(@expr, [arm]);
229-
expr_fn(_fn, @capture);
229+
expr_fn(_fn, @capture_clause);
230230
expr_block(blk);
231231

232232
/*
@@ -261,13 +261,15 @@ tag expr_ {
261261
expr_mac(mac);
262262
}
263263

264-
// At the moment, one can only capture local variables.
265-
type capture_ = {
266-
copies: [spanned<ident>],
267-
moves: [spanned<ident>]
264+
type capture_item = {
265+
id: int,
266+
name: ident, // Currently, can only capture a local var.
267+
span: span
268+
};
269+
type capture_clause = {
270+
copies: [@capture_item],
271+
moves: [@capture_item]
268272
};
269-
270-
type capture = spanned<capture_>;
271273

272274
/*
273275
// Says whether this is a block the user marked as

src/comp/syntax/parse/parser.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ fn parse_if_expr(p: parser) -> @ast::expr {
12441244
// CC := [copy ID*; move ID*]
12451245
//
12461246
// where any part is optional and trailing ; is permitted.
1247-
fn parse_capture_clause(p: parser) -> @ast::capture {
1247+
fn parse_capture_clause(p: parser) -> @ast::capture_clause {
12481248
fn expect_opt_trailing_semi(p: parser) {
12491249
if !eat(p, token::SEMI) {
12501250
if p.peek() != token::RBRACKET {
@@ -1253,15 +1253,15 @@ fn parse_capture_clause(p: parser) -> @ast::capture {
12531253
}
12541254
}
12551255

1256-
fn eat_ident_list(p: parser) -> [ast::spanned<ast::ident>] {
1256+
fn eat_ident_list(p: parser) -> [@ast::capture_item] {
12571257
let res = [];
12581258
while true {
12591259
alt p.peek() {
12601260
token::IDENT(_, _) {
1261-
let i = spanned(p.get_lo_pos(),
1262-
p.get_hi_pos(),
1263-
parse_ident(p));
1264-
res += [i];
1261+
let id = p.get_id();
1262+
let sp = ast_util::mk_sp(p.get_lo_pos(), p.get_hi_pos());
1263+
let ident = parse_ident(p);
1264+
res += [@{id:id, name:ident, span:sp}];
12651265
if !eat(p, token::COMMA) {
12661266
ret res;
12671267
}
@@ -1276,7 +1276,6 @@ fn parse_capture_clause(p: parser) -> @ast::capture {
12761276
let copies = [];
12771277
let moves = [];
12781278

1279-
let lo = p.get_lo_pos();
12801279
if eat(p, token::LBRACKET) {
12811280
while !eat(p, token::RBRACKET) {
12821281
if eat_word(p, "copy") {
@@ -1291,27 +1290,25 @@ fn parse_capture_clause(p: parser) -> @ast::capture {
12911290
}
12921291
}
12931292
}
1294-
let hi = p.get_last_hi_pos();
12951293

1296-
ret @spanned(lo, hi, {copies: copies, moves: moves});
1294+
ret @{copies: copies, moves: moves};
12971295
}
12981296

12991297
fn parse_fn_expr(p: parser, proto: ast::proto) -> @ast::expr {
13001298
let lo = p.get_last_lo_pos();
1301-
let captures = parse_capture_clause(p);
1299+
let capture_clause = parse_capture_clause(p);
13021300
let decl = parse_fn_decl(p, ast::impure_fn, ast::il_normal);
13031301
let body = parse_block(p);
13041302
let _fn = {decl: decl, proto: proto, body: body};
1305-
ret mk_expr(p, lo, body.span.hi, ast::expr_fn(_fn, captures));
1303+
ret mk_expr(p, lo, body.span.hi, ast::expr_fn(_fn, capture_clause));
13061304
}
13071305

13081306
fn parse_fn_block_expr(p: parser) -> @ast::expr {
13091307
let lo = p.get_last_lo_pos();
13101308
let decl = parse_fn_block_decl(p);
1311-
let mid = p.get_last_hi_pos();
13121309
let body = parse_block_tail(p, lo, ast::default_blk);
13131310
let _fn = {decl: decl, proto: ast::proto_block, body: body};
1314-
let captures = @spanned(lo, mid, {copies: [], moves: []});
1311+
let captures = @{copies: [], moves: []};
13151312
ret mk_expr(p, lo, body.span.hi, ast::expr_fn(_fn, captures));
13161313
}
13171314

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// error-pattern:unresolved name: z
2+
fn main() {
3+
let x = 5;
4+
let y = sendfn[copy z, x]() {
5+
};
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// error-pattern:unresolved name: z
2+
fn main() {
3+
let x = 5;
4+
let y = sendfn[move z, x]() {
5+
};
6+
}

0 commit comments

Comments
 (0)