Skip to content

Commit e2334d8

Browse files
authored
parser: fix checking for invalid PrefixExpr (fix #20388) (#20392)
1 parent 582f7be commit e2334d8

File tree

5 files changed

+18
-0
lines changed

5 files changed

+18
-0
lines changed

vlib/v/parser/expr.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,17 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
666666
p.inside_in_array = true
667667
}
668668

669+
right_op_pos := p.tok.pos()
669670
right = p.expr(precedence)
671+
if op in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && mut right is ast.PrefixExpr {
672+
mut right_expr := right.right
673+
for mut right_expr is ast.ParExpr {
674+
right_expr = right_expr.expr
675+
}
676+
if right.op in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && right_expr.is_pure_literal() {
677+
p.error_with_pos('invalid expression: unexpected token `${op}`', right_op_pos)
678+
}
679+
}
670680
if is_key_in {
671681
p.inside_in_array = false
672682
}

vlib/v/parser/tests/prefix_err_1.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vlib/v/parser/tests/prefix_err_1.vv:1:9: error: invalid expression: unexpected token `-`
2+
1 | _ = 5 - - -5
3+
| ^

vlib/v/parser/tests/prefix_err_1.vv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_ = 5 - - -5

vlib/v/parser/tests/prefix_err_2.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vlib/v/parser/tests/prefix_err_2.vv:1:9: error: invalid expression: unexpected token `-`
2+
1 | _ = 5 - - (((-5)))
3+
| ^

vlib/v/parser/tests/prefix_err_2.vv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_ = 5 - - (((-5)))

0 commit comments

Comments
 (0)