parser: fix checking for invalid PrefixExpr (fix #20388) (#20392)

This commit is contained in:
shove 2024-01-05 18:22:08 +08:00 committed by GitHub
parent 582f7be2e1
commit e2334d8b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 0 deletions

View File

@ -666,7 +666,17 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
p.inside_in_array = true p.inside_in_array = true
} }
right_op_pos := p.tok.pos()
right = p.expr(precedence) right = p.expr(precedence)
if op in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && mut right is ast.PrefixExpr {
mut right_expr := right.right
for mut right_expr is ast.ParExpr {
right_expr = right_expr.expr
}
if right.op in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && right_expr.is_pure_literal() {
p.error_with_pos('invalid expression: unexpected token `${op}`', right_op_pos)
}
}
if is_key_in { if is_key_in {
p.inside_in_array = false p.inside_in_array = false
} }

View File

@ -0,0 +1,3 @@
vlib/v/parser/tests/prefix_err_1.vv:1:9: error: invalid expression: unexpected token `-`
1 | _ = 5 - - -5
| ^

View File

@ -0,0 +1 @@
_ = 5 - - -5

View File

@ -0,0 +1,3 @@
vlib/v/parser/tests/prefix_err_2.vv:1:9: error: invalid expression: unexpected token `-`
1 | _ = 5 - - (((-5)))
| ^

View File

@ -0,0 +1 @@
_ = 5 - - (((-5)))