Skip to content

Commit 44ffd8e

Browse files
committed
Allow type annotations for blocks
I.e. {|foo: int| -> int foo + 2} Issue #1275
1 parent 54f72fb commit 44ffd8e

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/comp/syntax/parse/parser.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ fn parse_arg(p: parser) -> ast::arg {
567567
fn parse_fn_block_arg(p: parser) -> ast::arg {
568568
let m = parse_arg_mode(p);
569569
let i = parse_value_ident(p);
570-
let t = @spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_infer);
570+
let t = eat(p, token::COLON) ? parse_ty(p, false) :
571+
@spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_infer);
571572
ret {mode: m, ty: t, ident: i, id: p.get_id()};
572573
}
573574

@@ -1747,16 +1748,13 @@ fn parse_fn_decl(p: parser, purity: ast::purity, il: ast::inlineness) ->
17471748
}
17481749

17491750
fn parse_fn_block_decl(p: parser) -> ast::fn_decl {
1750-
let inputs =
1751-
if p.peek() == token::OROR {
1752-
p.bump();
1753-
[]
1754-
} else {
1755-
parse_seq(token::BINOP(token::OR), token::BINOP(token::OR),
1756-
seq_sep(token::COMMA), parse_fn_block_arg, p).node
1757-
};
1751+
let inputs = eat(p, token::OROR) ? [] :
1752+
parse_seq(token::BINOP(token::OR), token::BINOP(token::OR),
1753+
seq_sep(token::COMMA), parse_fn_block_arg, p).node;
1754+
let output = eat(p, token::RARROW) ? parse_ty(p, false) :
1755+
@spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_infer);
17581756
ret {inputs: inputs,
1759-
output: @spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_infer),
1757+
output: output,
17601758
purity: ast::impure_fn,
17611759
il: ast::il_normal,
17621760
cf: ast::return_val,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
fn as_buf<T>(s: str, f: block(str) -> T) -> T { f(s) }
3+
as_buf("foo", {|foo: str| -> () log_err foo;});
4+
}

0 commit comments

Comments
 (0)