parser: fix position of array init (#6998)

pull/7005/head
Daniel Däschle 2020-11-28 16:05:14 +01:00 committed by GitHub
parent ea04d23e1d
commit 6e4dad9acf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 9 deletions

View File

@ -1072,9 +1072,6 @@ pub fn (expr Expr) position() token.Position {
InfixExpr { InfixExpr {
left_pos := expr.left.position() left_pos := expr.left.position()
right_pos := expr.right.position() right_pos := expr.right.position()
if left_pos.pos == 0 || right_pos.pos == 0 {
return expr.pos
}
return token.Position{ return token.Position{
line_nr: expr.pos.line_nr line_nr: expr.pos.line_nr
pos: left_pos.pos pos: left_pos.pos

View File

@ -5,7 +5,6 @@ module parser
import v.ast import v.ast
import v.table import v.table
import v.token
fn (mut p Parser) array_init() ast.ArrayInit { fn (mut p Parser) array_init() ast.ArrayInit {
first_pos := p.tok.position() first_pos := p.tok.position()
@ -23,6 +22,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
mut has_default := false mut has_default := false
mut default_expr := ast.Expr{} mut default_expr := ast.Expr{}
if p.tok.kind == .rsbr { if p.tok.kind == .rsbr {
last_pos = p.tok.position()
// []typ => `[]` and `typ` must be on the same line // []typ => `[]` and `typ` must be on the same line
line_nr := p.tok.line_nr line_nr := p.tok.line_nr
p.next() p.next()
@ -126,11 +126,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
} }
p.check(.rcbr) p.check(.rcbr)
} }
pos := token.Position{ pos := first_pos.extend(last_pos)
line_nr: first_pos.line_nr
pos: first_pos.pos
len: last_pos.pos - first_pos.pos + last_pos.len
}
return ast.ArrayInit{ return ast.ArrayInit{
is_fixed: is_fixed is_fixed: is_fixed
has_val: has_val has_val: has_val

View File

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

View File

@ -0,0 +1,3 @@
fn main() {
'' in []
}