From 2f9b7fe0f7c7695b4384efbf5c179120c1772b08 Mon Sep 17 00:00:00 2001 From: Takahiro Yaota Date: Mon, 23 Nov 2020 18:36:20 +0900 Subject: [PATCH] vfmt: fix error with `return // comment` (#6914) --- vlib/v/fmt/tests/comments_expected.vv | 4 +++- vlib/v/fmt/tests/comments_input.vv | 3 ++- vlib/v/parser/parser.v | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/vlib/v/fmt/tests/comments_expected.vv b/vlib/v/fmt/tests/comments_expected.vv index 509cc14d72..b3df92b480 100644 --- a/vlib/v/fmt/tests/comments_expected.vv +++ b/vlib/v/fmt/tests/comments_expected.vv @@ -1,6 +1,6 @@ fn fun() int { // comment zero - return 0 + return 0 // another comment } fn mr_fun() (int, int) { @@ -34,4 +34,6 @@ fn main() { else { println('else') } + // empty return + return } diff --git a/vlib/v/fmt/tests/comments_input.vv b/vlib/v/fmt/tests/comments_input.vv index 827d49429f..e45ea17b6a 100644 --- a/vlib/v/fmt/tests/comments_input.vv +++ b/vlib/v/fmt/tests/comments_input.vv @@ -1,5 +1,5 @@ fn fun() int { - return /* comment zero */ 0 + return /* comment zero */ 0 // another comment } fn mr_fun() (int, int) { @@ -22,4 +22,5 @@ fn main() { else /* after else */ { println('else') } + return // empty return } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 605ca6defb..5827f67d62 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1737,13 +1737,16 @@ fn (mut p Parser) return_stmt() ast.Return { first_pos := p.tok.position() p.next() // no return + mut comments := p.eat_comments() if p.tok.kind == .rcbr { return ast.Return{ + comments: comments pos: first_pos } } // return exprs - exprs, comments := p.expr_list() + exprs, comments2 := p.expr_list() + comments << comments2 end_pos := exprs.last().position() return ast.Return{ exprs: exprs