checker: fix error position for assignment expression

pull/4326/head
Delyan Angelov 2020-04-10 15:11:05 +03:00
parent 3ea563291c
commit 3573030b9b
5 changed files with 24 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 `$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` ', assign_expr.pos)
}
c.check_expr_opt_call(assign_expr.val, right_type, true)
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/inout/void_function_assign_to_string.v:6:6: error: cannot assign `void` to variable `a` of type `string`
4| fn main(){
5| mut a := ''
6| a = x(1,2) // hello
^
7| eprintln('a: $a')
8| }

View File

@ -0,0 +1,8 @@
fn x(x,y int) {
}
fn main(){
mut a := ''
a = x(1,2) // hello
eprintln('a: $a')
}

View File

@ -461,6 +461,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
op := p.tok.kind
p.next()
pos := p.tok.position()
val := p.expr(0)
match left {
ast.IndexExpr {
@ -473,7 +474,7 @@ pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
left: left
val: val
op: op
pos: p.tok.position()
pos: pos
}
return node
}

View File

@ -95,8 +95,12 @@ pub fn formatted_error(kind string /*error or warn*/, emsg string, filepath stri
}
continue
}
underline := '~'.repeat(pos.len)
pointerline << if emanager.support_color { term.bold(term.blue(underline)) } else { underline }
if pos.len > 1 {
underline := '~'.repeat(pos.len)
pointerline << if emanager.support_color { term.bold(term.blue(underline)) } else { underline }
}else{
pointerline << if emanager.support_color { term.bold(term.blue('^')) } else { '^' }
}
break
}
clines << ' ' + pointerline.join('')