Skip to content

Commit

Permalink
parser: fix checking for invalid PrefixExpr (fix #20388) (#20392)
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 committed Jan 5, 2024
1 parent 582f7be commit e2334d8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions vlib/v/parser/expr.v
Expand Up @@ -666,7 +666,17 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
p.inside_in_array = true
}

right_op_pos := p.tok.pos()
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 {
p.inside_in_array = false
}
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/prefix_err_1.out
@@ -0,0 +1,3 @@
vlib/v/parser/tests/prefix_err_1.vv:1:9: error: invalid expression: unexpected token `-`
1 | _ = 5 - - -5
| ^
1 change: 1 addition & 0 deletions vlib/v/parser/tests/prefix_err_1.vv
@@ -0,0 +1 @@
_ = 5 - - -5
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/prefix_err_2.out
@@ -0,0 +1,3 @@
vlib/v/parser/tests/prefix_err_2.vv:1:9: error: invalid expression: unexpected token `-`
1 | _ = 5 - - (((-5)))
| ^
1 change: 1 addition & 0 deletions vlib/v/parser/tests/prefix_err_2.vv
@@ -0,0 +1 @@
_ = 5 - - (((-5)))

0 comments on commit e2334d8

Please sign in to comment.