From 1f952330c4e91ddc0a33218af77c4eda7fb53ff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Thu, 10 Dec 2020 10:56:08 +0100 Subject: [PATCH] parser: fix fn args eol errors (#7222) --- vlib/v/checker/tests/globals/no_type.out | 2 +- vlib/v/parser/fn.v | 24 +++++++++++++++++++++++ vlib/v/parser/parse_type.v | 2 +- vlib/v/parser/tests/c_struct_no_embed.out | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/vlib/v/checker/tests/globals/no_type.out b/vlib/v/checker/tests/globals/no_type.out index b69a8c175f..71899d904a 100644 --- a/vlib/v/checker/tests/globals/no_type.out +++ b/vlib/v/checker/tests/globals/no_type.out @@ -1,3 +1,3 @@ -vlib/v/checker/tests/globals/no_type.vv:1:17: error: bad type syntax +vlib/v/checker/tests/globals/no_type.vv:1:17: error: expecting type declaration 1 | __global ( test ) | ^ diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 3edc217a64..935d1bfffb 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -431,6 +431,10 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { // p.warn('types only') mut arg_no := 1 for p.tok.kind != .rpar { + if p.tok.kind == .eof { + p.error_with_pos('expecting `)`', p.tok.position()) + return []table.Param{}, false, false + } is_shared := p.tok.kind == .key_shared is_atomic := p.tok.kind == .key_atomic is_mut := p.tok.kind == .key_mut || is_shared || is_atomic @@ -443,6 +447,10 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { } pos := p.tok.position() mut arg_type := p.parse_type() + if arg_type == 0 { + // error is added in parse_type + return []table.Param{}, false, false + } if is_mut { if !arg_type.has_flag(.generic) { if is_shared { @@ -471,6 +479,10 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { if is_variadic { arg_type = arg_type.set_flag(.variadic) } + if p.tok.kind == .eof { + p.error_with_pos('expecting `)`', p.prev_tok.position()) + return []table.Param{}, false, false + } if p.tok.kind == .comma { if is_variadic { p.error_with_pos('cannot use ...(variadic) with non-final parameter no $arg_no', @@ -493,6 +505,10 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { } } else { for p.tok.kind != .rpar { + if p.tok.kind == .eof { + p.error_with_pos('expecting `)`', p.tok.position()) + return []table.Param{}, false, false + } is_shared := p.tok.kind == .key_shared is_atomic := p.tok.kind == .key_atomic mut is_mut := p.tok.kind == .key_mut || is_shared || is_atomic @@ -522,6 +538,10 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { } pos := p.tok.position() mut typ := p.parse_type() + if typ == 0 { + // error is added in parse_type + return []table.Param{}, false, false + } if is_mut { if !typ.has_flag(.generic) { if is_shared { @@ -561,6 +581,10 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { return []table.Param{}, false, false } } + if p.tok.kind == .eof { + p.error_with_pos('expecting `)`', p.prev_tok.position()) + return []table.Param{}, false, false + } if p.tok.kind != .rpar { p.check(.comma) } diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index a4d85e62e2..5111017ce6 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -254,7 +254,7 @@ pub fn (mut p Parser) parse_any_type(language table.Language, is_ptr bool, check } if name == '' { // This means the developer is using some wrong syntax like `x: int` instead of `x int` - p.error('bad type syntax') + p.error('expecting type declaration') return 0 } match name { diff --git a/vlib/v/parser/tests/c_struct_no_embed.out b/vlib/v/parser/tests/c_struct_no_embed.out index 193bd21868..89972cdcd8 100644 --- a/vlib/v/parser/tests/c_struct_no_embed.out +++ b/vlib/v/parser/tests/c_struct_no_embed.out @@ -1,4 +1,4 @@ -vlib/v/parser/tests/c_struct_no_embed.vv:7:1: error: bad type syntax +vlib/v/parser/tests/c_struct_no_embed.vv:7:1: error: expecting type declaration 5 | struct C.Unknown { 6 | Foo 7 | }