From 44ffd8e3aadccbceb544074a3b96e255d0d97325 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Fri, 9 Dec 2011 09:42:09 +0100 Subject: [PATCH] Allow type annotations for blocks I.e. {|foo: int| -> int foo + 2} Issue #1275 --- src/comp/syntax/parse/parser.rs | 18 ++++++++---------- src/test/run-pass/block-explicit-types.rs | 4 ++++ 2 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 src/test/run-pass/block-explicit-types.rs diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 52e2d92e7994..7b8bd726eedb 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -567,7 +567,8 @@ fn parse_arg(p: parser) -> ast::arg { fn parse_fn_block_arg(p: parser) -> ast::arg { let m = parse_arg_mode(p); let i = parse_value_ident(p); - let t = @spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_infer); + let t = eat(p, token::COLON) ? parse_ty(p, false) : + @spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_infer); ret {mode: m, ty: t, ident: i, id: p.get_id()}; } @@ -1747,16 +1748,13 @@ fn parse_fn_decl(p: parser, purity: ast::purity, il: ast::inlineness) -> } fn parse_fn_block_decl(p: parser) -> ast::fn_decl { - let inputs = - if p.peek() == token::OROR { - p.bump(); - [] - } else { - parse_seq(token::BINOP(token::OR), token::BINOP(token::OR), - seq_sep(token::COMMA), parse_fn_block_arg, p).node - }; + let inputs = eat(p, token::OROR) ? [] : + parse_seq(token::BINOP(token::OR), token::BINOP(token::OR), + seq_sep(token::COMMA), parse_fn_block_arg, p).node; + let output = eat(p, token::RARROW) ? parse_ty(p, false) : + @spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_infer); ret {inputs: inputs, - output: @spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_infer), + output: output, purity: ast::impure_fn, il: ast::il_normal, cf: ast::return_val, diff --git a/src/test/run-pass/block-explicit-types.rs b/src/test/run-pass/block-explicit-types.rs new file mode 100644 index 000000000000..1ed2e207f183 --- /dev/null +++ b/src/test/run-pass/block-explicit-types.rs @@ -0,0 +1,4 @@ +fn main() { + fn as_buf(s: str, f: block(str) -> T) -> T { f(s) } + as_buf("foo", {|foo: str| -> () log_err foo;}); +}