From 1ba5996404d43531562820db3c8a709a438abc59 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 29 Jun 2020 10:53:12 +0300 Subject: [PATCH] parser: fix interface functions with no params (ui examples) --- vlib/v/parser/fn.v | 3 ++- .../tests/fn_type_only_args_in_interfaces.out | 5 +++++ .../tests/fn_type_only_args_in_interfaces.vv | 22 +++++++++++++++++++ .../tests/fn_type_only_args_unknown_name.out | 5 +++-- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100755 vlib/v/parser/tests/fn_type_only_args_in_interfaces.out create mode 100644 vlib/v/parser/tests/fn_type_only_args_in_interfaces.vv diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 0ea5e45f7..cf49cec57 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -367,7 +367,8 @@ fn (mut p Parser) fn_args() ([]table.Arg, bool, bool) { mut args := []table.Arg{} mut is_variadic := false // `int, int, string` (no names, just types) - types_only := p.tok.kind in [.amp, .ellipsis, .key_fn] || (p.peek_tok.kind in [.comma, .rpar] && p.table.known_type(p.tok.lit)) + types_only := p.tok.kind in [.amp, .ellipsis, .key_fn] || (p.peek_tok.kind == .comma && p.table.known_type(p.tok.lit)) || + p.peek_tok.kind == .rpar // TODO copy pasta, merge 2 branches if types_only { // p.warn('types only') diff --git a/vlib/v/parser/tests/fn_type_only_args_in_interfaces.out b/vlib/v/parser/tests/fn_type_only_args_in_interfaces.out new file mode 100755 index 000000000..0ce9615bb --- /dev/null +++ b/vlib/v/parser/tests/fn_type_only_args_in_interfaces.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/fn_type_only_args_in_interfaces.v:22:1: error: `syntax_error` evaluated but not used + 20 | } + 21 | + 22 | syntax_error + | ~~~~~~~~~~~~ diff --git a/vlib/v/parser/tests/fn_type_only_args_in_interfaces.vv b/vlib/v/parser/tests/fn_type_only_args_in_interfaces.vv new file mode 100644 index 000000000..0a3e83966 --- /dev/null +++ b/vlib/v/parser/tests/fn_type_only_args_in_interfaces.vv @@ -0,0 +1,22 @@ + +struct Type1 {} +struct Type2 {} +struct Type3 {} + + +pub interface Widget1 { + init(Type1, Type2) +} + +pub interface Widget2 { + init(Type1) + draw(Type2, Type3) +} + +pub interface Widget3 { + fnoparams1() + fnoparams2() + draw(Type1, Type2) +} + +syntax_error diff --git a/vlib/v/parser/tests/fn_type_only_args_unknown_name.out b/vlib/v/parser/tests/fn_type_only_args_unknown_name.out index a91e79972..f90443c8e 100644 --- a/vlib/v/parser/tests/fn_type_only_args_unknown_name.out +++ b/vlib/v/parser/tests/fn_type_only_args_unknown_name.out @@ -1,5 +1,6 @@ -vlib/v/parser/tests/fn_type_only_args_unknown_name.v:1:16: error: unexpected `{`, expecting `,` +vlib/v/parser/tests/fn_type_only_args_unknown_name.v:2:1: error: functions with type only args can not have bodies 1 | fn test(param) { - | ^ 2 | } + | ^ 3 | + 4 | fn main() { -- 2.39.5