From a5b98cb2672d05605df0e96d9e423f80302b812e Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 24 May 2022 10:15:31 +0800 Subject: [PATCH] parser: check fn decl multi return types without parentheses (#14508) --- vlib/v/parser/fn.v | 5 +++++ vlib/v/parser/tests/fn_decl_multi_return_types_err.out | 7 +++++++ vlib/v/parser/tests/fn_decl_multi_return_types_err.vv | 6 ++++++ 3 files changed, 18 insertions(+) create mode 100644 vlib/v/parser/tests/fn_decl_multi_return_types_err.out create mode 100644 vlib/v/parser/tests/fn_decl_multi_return_types_err.vv diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 461e8fc4bf..10a6e84206 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -400,6 +400,11 @@ fn (mut p Parser) fn_decl() ast.FnDecl { p.inside_fn_return = false return_type_pos = return_type_pos.extend(p.prev_tok.pos()) } + if p.tok.kind == .comma { + mr_pos := return_type_pos.extend(p.peek_tok.pos()) + p.error_with_pos('multiple return types in function declaration must use parentheses, .e.g (int, string)', + mr_pos) + } mut type_sym_method_idx := 0 no_body := p.tok.kind != .lcbr end_pos := p.prev_tok.pos() diff --git a/vlib/v/parser/tests/fn_decl_multi_return_types_err.out b/vlib/v/parser/tests/fn_decl_multi_return_types_err.out new file mode 100644 index 0000000000..c587d39cf8 --- /dev/null +++ b/vlib/v/parser/tests/fn_decl_multi_return_types_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/fn_decl_multi_return_types_err.vv:4:10: error: multiple return types in function declaration must use parentheses, .e.g (int, string) + 2 | } + 3 | + 4 | fn foo() int, int { + | ~~~~~~~~ + 5 | return 0, 0 + 6 | } diff --git a/vlib/v/parser/tests/fn_decl_multi_return_types_err.vv b/vlib/v/parser/tests/fn_decl_multi_return_types_err.vv new file mode 100644 index 0000000000..0753fe0470 --- /dev/null +++ b/vlib/v/parser/tests/fn_decl_multi_return_types_err.vv @@ -0,0 +1,6 @@ +fn main() { +} + +fn foo() int, int { + return 0, 0 +}