From af30bf939e1ab5d8831a99b9c057e5ccf55d1401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Fri, 17 Apr 2020 16:16:56 +0200 Subject: [PATCH] checker: fix return underlining --- vlib/v/checker/checker.v | 4 ++-- vlib/v/checker/tests/inout/return_type.out | 6 ++++++ vlib/v/checker/tests/inout/return_type.vv | 5 +++++ vlib/v/parser/parser.v | 13 +++++++++---- 4 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 vlib/v/checker/tests/inout/return_type.out create mode 100644 vlib/v/checker/tests/inout/return_type.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a53aa20093..300bf2b95c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -644,8 +644,8 @@ pub fn (c mut Checker) return_stmt(return_stmt mut ast.Return) { if !c.table.check(got_typ, exp_typ) { got_typ_sym := c.table.get_type_symbol(got_typ) exp_typ_sym := c.table.get_type_symbol(exp_typ) - c.error('cannot use `$got_typ_sym.name` as type `$exp_typ_sym.name` in return argument', - return_stmt.pos) + pos := return_stmt.exprs[i].position() + c.error('cannot use `$got_typ_sym.name` as type `$exp_typ_sym.name` in return argument', pos) } } } diff --git a/vlib/v/checker/tests/inout/return_type.out b/vlib/v/checker/tests/inout/return_type.out new file mode 100644 index 0000000000..2d41747047 --- /dev/null +++ b/vlib/v/checker/tests/inout/return_type.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/inout/return_type.v:2:9: error: cannot use `int` as type `bool` in return argument + 1| fn test() bool { + 2| return 100 + ~~~ + 3| } + 4| \ No newline at end of file diff --git a/vlib/v/checker/tests/inout/return_type.vv b/vlib/v/checker/tests/inout/return_type.vv new file mode 100644 index 0000000000..333ec9f622 --- /dev/null +++ b/vlib/v/checker/tests/inout/return_type.vv @@ -0,0 +1,5 @@ +fn test() bool { + return 100 +} + +fn main() {} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 551a414e26..b22a240fe9 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1748,12 +1748,13 @@ fn (var p Parser) interface_decl() ast.InterfaceDecl { } fn (var p Parser) return_stmt() ast.Return { + first_pos := p.tok.position() p.next() // return expressions var exprs := []ast.Expr if p.tok.kind == .rcbr { return ast.Return{ - pos: p.tok.position() + pos: first_pos } } for { @@ -1765,11 +1766,15 @@ fn (var p Parser) return_stmt() ast.Return { break } } - stmt := ast.Return{ + last_pos := exprs.last().position() + return ast.Return{ exprs: exprs - pos: p.tok.position() + pos: token.Position{ + line_nr: first_pos.line_nr + pos: first_pos.pos + len: last_pos.pos - first_pos.pos + last_pos.len + } } - return stmt } // left hand side of `=` or `:=` in `a,b,c := 1,2,3`