From 6e4dad9acf69de21c70b067c5714627eaff1fbe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Sat, 28 Nov 2020 16:05:14 +0100 Subject: [PATCH] parser: fix position of array init (#6998) --- vlib/v/ast/ast.v | 3 --- vlib/v/parser/containers.v | 8 ++------ vlib/v/parser/tests/array_pos_err.out | 5 +++++ vlib/v/parser/tests/array_pos_err.vv | 3 +++ 4 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 vlib/v/parser/tests/array_pos_err.out create mode 100644 vlib/v/parser/tests/array_pos_err.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index a3e6f7bf72..46dec82d1b 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1072,9 +1072,6 @@ pub fn (expr Expr) position() token.Position { InfixExpr { left_pos := expr.left.position() right_pos := expr.right.position() - if left_pos.pos == 0 || right_pos.pos == 0 { - return expr.pos - } return token.Position{ line_nr: expr.pos.line_nr pos: left_pos.pos diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 11ae7f730d..1655e942c5 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -5,7 +5,6 @@ module parser import v.ast import v.table -import v.token fn (mut p Parser) array_init() ast.ArrayInit { first_pos := p.tok.position() @@ -23,6 +22,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { mut has_default := false mut default_expr := ast.Expr{} if p.tok.kind == .rsbr { + last_pos = p.tok.position() // []typ => `[]` and `typ` must be on the same line line_nr := p.tok.line_nr p.next() @@ -126,11 +126,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { } p.check(.rcbr) } - pos := token.Position{ - line_nr: first_pos.line_nr - pos: first_pos.pos - len: last_pos.pos - first_pos.pos + last_pos.len - } + pos := first_pos.extend(last_pos) return ast.ArrayInit{ is_fixed: is_fixed has_val: has_val diff --git a/vlib/v/parser/tests/array_pos_err.out b/vlib/v/parser/tests/array_pos_err.out new file mode 100644 index 0000000000..85930545f8 --- /dev/null +++ b/vlib/v/parser/tests/array_pos_err.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/array_pos_err.vv:2:2: error: expression evaluated but not used + 1 | fn main() { + 2 | '' in [] + | ~~~~~~~~ + 3 | } \ No newline at end of file diff --git a/vlib/v/parser/tests/array_pos_err.vv b/vlib/v/parser/tests/array_pos_err.vv new file mode 100644 index 0000000000..0988140700 --- /dev/null +++ b/vlib/v/parser/tests/array_pos_err.vv @@ -0,0 +1,3 @@ +fn main() { + '' in [] +} \ No newline at end of file