checker: fix return underlining

pull/4465/head
Daniel Däschle 2020-04-17 16:16:56 +02:00 committed by GitHub
parent c1d9e22ca6
commit af30bf939e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 6 deletions

View File

@ -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)
}
}
}

View File

@ -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|

View File

@ -0,0 +1,5 @@
fn test() bool {
return 100
}
fn main() {}

View File

@ -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`