parser: fix void assign check

pull/4330/head
Daniel Däschle 2020-04-10 14:53:06 +02:00 committed by GitHub
parent 42b3b19af4
commit c815f84722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 4 deletions

View File

@ -215,7 +215,7 @@ fn (c mut Checker) assign_expr(assign_expr mut ast.AssignExpr) {
if !c.table.check(right_type, left_type) {
left_type_sym := c.table.get_type_symbol(left_type)
right_type_sym := c.table.get_type_symbol(right_type)
c.error('cannot assign `$right_type_sym.name` to variable `${assign_expr.left.str()}` of type `$left_type_sym.name` ', assign_expr.pos)
c.error('cannot assign `$right_type_sym.name` to variable `${assign_expr.left.str()}` of type `$left_type_sym.name` ', expr_pos(assign_expr.val))
}
c.check_expr_opt_call(assign_expr.val, right_type, true)
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/inout/void_fn_as_value.v:5:8: error: unknown fn: x
3| fn main() {
4| mut a := 'aa'
5| a += x('a','b')
~~~~~~~~~~
6| mut b := 'abcdef'
7| }

View File

@ -0,0 +1,7 @@
module checker
fn main() {
mut a := 'aa'
a += x('a','b')
mut b := 'abcdef'
}

View File

@ -2,6 +2,6 @@ vlib/v/checker/tests/inout/void_function_assign_to_string.v:6:6: error: cannot a
4| fn main(){
5| mut a := ''
6| a = x(1,2) // hello
^
~~~~~~
7| eprintln('a: $a')
8| }

View File

@ -7,9 +7,11 @@ import (
v.ast
v.table
v.scanner
v.token
)
pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr {
first_pos := p.tok.position()
tok := p.tok
name := p.check_name()
fn_name := if is_c {
@ -21,6 +23,13 @@ pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr {
}
p.check(.lpar)
args := p.call_args()
last_pos := p.tok.position()
p.check(.rpar)
pos := token.Position{
line_nr: first_pos.line_nr
pos: first_pos.pos
len: last_pos.pos - first_pos.pos + last_pos.len
}
mut or_stmts := []ast.Stmt
mut is_or_block_used := false
if p.tok.kind == .key_orelse {
@ -42,7 +51,7 @@ pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr {
name: fn_name
args: args
mod: p.mod
pos: tok.position()
pos: pos
is_c: is_c
or_block: ast.OrExpr{
stmts: or_stmts
@ -69,7 +78,6 @@ pub fn (p mut Parser) call_args() []ast.CallArg {
p.check(.comma)
}
}
p.check(.rpar)
return args
}

View File

@ -960,6 +960,7 @@ fn (p mut Parser) dot_expr(left ast.Expr) ast.Expr {
if p.tok.kind == .lpar {
p.next()
args := p.call_args()
p.check(.rpar)
mut or_stmts := []ast.Stmt
mut is_or_block_used := false
if p.tok.kind == .key_orelse {