From 3573030b9b61211e34983bbd08d4bca95c5d7051 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 10 Apr 2020 15:11:05 +0300 Subject: [PATCH] checker: fix error position for assignment expression --- vlib/v/checker/checker.v | 2 +- .../tests/inout/void_function_assign_to_string.out | 7 +++++++ .../checker/tests/inout/void_function_assign_to_string.vv | 8 ++++++++ vlib/v/parser/parser.v | 3 ++- vlib/v/util/errors.v | 8 ++++++-- 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 vlib/v/checker/tests/inout/void_function_assign_to_string.out create mode 100644 vlib/v/checker/tests/inout/void_function_assign_to_string.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f0917b0c9a..ec97a62f4c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) } diff --git a/vlib/v/checker/tests/inout/void_function_assign_to_string.out b/vlib/v/checker/tests/inout/void_function_assign_to_string.out new file mode 100644 index 0000000000..1536611344 --- /dev/null +++ b/vlib/v/checker/tests/inout/void_function_assign_to_string.out @@ -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| } diff --git a/vlib/v/checker/tests/inout/void_function_assign_to_string.vv b/vlib/v/checker/tests/inout/void_function_assign_to_string.vv new file mode 100644 index 0000000000..839d84d00c --- /dev/null +++ b/vlib/v/checker/tests/inout/void_function_assign_to_string.vv @@ -0,0 +1,8 @@ +fn x(x,y int) { + +} +fn main(){ + mut a := '' + a = x(1,2) // hello + eprintln('a: $a') +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e8c7e4777d..7d87e2bebb 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 } diff --git a/vlib/v/util/errors.v b/vlib/v/util/errors.v index 8a1929be7f..32c90b4adb 100644 --- a/vlib/v/util/errors.v +++ b/vlib/v/util/errors.v @@ -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('')