parser: fix string position

pull/4326/head
Daniel Däschle 2020-04-10 14:40:52 +02:00 committed by GitHub
parent 83dfc6b9b9
commit adb379dd63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 15 deletions

View File

@ -164,7 +164,7 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
if infix_expr.op == .left_shift { if infix_expr.op == .left_shift {
if left.kind != .array && !left.is_int() { if left.kind != .array && !left.is_int() {
// c.error('<< can only be used with numbers and arrays', infix_expr.pos) // c.error('<< can only be used with numbers and arrays', infix_expr.pos)
c.error('incompatible types: $left.name << $right.name', infix_expr.pos) c.error('cannot shift type $right.name into $left.name', expr_pos(infix_expr.right))
return table.void_type return table.void_type
} }
if left.kind == .array { if left.kind == .array {
@ -178,7 +178,7 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
// []T << []T // []T << []T
return table.void_type return table.void_type
} }
c.error('incompatible types: $left.name << $right.name', infix_expr.pos) c.error('cannot shift type $right.name into $left.name', expr_pos(infix_expr.right))
return table.void_type return table.void_type
} }
} }

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/left_shift_err.v:3:7: error: cannot shift type string into array_int
1| fn main() {
2| l := []int
3| l << 'test'
~~~~~~
4| }

View File

@ -0,0 +1,4 @@
fn main() {
l := []int
l << 'test'
}

View File

@ -1229,7 +1229,6 @@ fn (p mut Parser) if_expr() ast.IfExpr {
} }
fn (p mut Parser) string_expr() ast.Expr { fn (p mut Parser) string_expr() ast.Expr {
first_pos := p.tok.position()
is_raw := p.tok.kind == .name && p.tok.lit == 'r' is_raw := p.tok.kind == .name && p.tok.lit == 'r'
is_cstr := p.tok.kind == .name && p.tok.lit == 'c' is_cstr := p.tok.kind == .name && p.tok.lit == 'c'
if is_raw || is_cstr { if is_raw || is_cstr {
@ -1237,14 +1236,9 @@ fn (p mut Parser) string_expr() ast.Expr {
} }
mut node := ast.Expr{} mut node := ast.Expr{}
val := p.tok.lit val := p.tok.lit
pos := p.tok.position()
if p.peek_tok.kind != .str_dollar { if p.peek_tok.kind != .str_dollar {
p.next() p.next()
last_pos := p.tok.position()
pos := token.Position{
line_nr: first_pos.line_nr
pos: first_pos.pos
len: last_pos.pos - first_pos.pos
}
node = ast.StringLiteral{ node = ast.StringLiteral{
val: val val: val
is_raw: is_raw is_raw: is_raw
@ -1286,12 +1280,6 @@ fn (p mut Parser) string_expr() ast.Expr {
} }
efmts << efmt.join('') efmts << efmt.join('')
} }
last_pos := p.tok.position()
pos := token.Position{
line_nr: first_pos.line_nr
pos: first_pos.pos
len: last_pos.pos - first_pos.pos
}
node = ast.StringInterLiteral{ node = ast.StringInterLiteral{
vals: vals vals: vals
exprs: exprs exprs: exprs