From 82650ee813f8910601e5ddb28ffcc748a955e781 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sun, 14 Feb 2021 07:05:20 +0000 Subject: [PATCH] parser: fix parsing attribute after fn prototype with no return type (#8727) --- vlib/v/parser/fn.v | 6 ++++-- vlib/v/tests/fn_test.v | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 678b19b561..15937aaaac 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -327,8 +327,10 @@ fn (mut p Parser) fn_decl() ast.FnDecl { mut end_pos := p.prev_tok.position() // Return type mut return_type := table.void_type - if p.tok.kind.is_start_of_type() - || (p.tok.kind == .key_fn && p.tok.line_nr == p.prev_tok.line_nr) { + // don't confuse token on the next line: fn decl, [attribute] + same_line := p.tok.line_nr == p.prev_tok.line_nr + if (p.tok.kind.is_start_of_type() && (same_line || p.tok.kind != .lsbr)) + || (same_line && p.tok.kind == .key_fn) { return_type = p.parse_type() } mut type_sym_method_idx := 0 diff --git a/vlib/v/tests/fn_test.v b/vlib/v/tests/fn_test.v index 2b99572b29..25cbea8ba9 100644 --- a/vlib/v/tests/fn_test.v +++ b/vlib/v/tests/fn_test.v @@ -44,6 +44,11 @@ type F6 = fn (int, int) type F7 = fn (time.Time, int) fn C.atoi(byteptr) int +fn C.freec(ptr voidptr) + +[trusted] +fn C.exitc(code int) +// above checks attribute doesn't conflict with `freec` return type fn foo() { }