From fab65047d3bced165c79cea5fa49828ccefced1b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 15 Apr 2026 05:02:13 +0300 Subject: [PATCH] parser: fix inaccurate error message in match expressions (fixes #13669) --- vlib/v/parser/expr.v | 8 +++++++- vlib/v/parser/tests/match_invalid_case_expr_err.out | 7 +++++++ vlib/v/parser/tests/match_invalid_case_expr_err.vv | 9 +++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 vlib/v/parser/tests/match_invalid_case_expr_err.out create mode 100644 vlib/v/parser/tests/match_invalid_case_expr_err.vv diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index ae067b639..b2c200a35 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -891,7 +891,13 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr { if op in [.decl_assign, .assign] { p.inside_assign_rhs = true } - right = p.expr(if op == .power { precedence - 1 } else { precedence }) + if p.inside_match_case && p.tok.kind == .lcbr { + // In a match branch, a bare `{` opens the branch body; it is not the rhs of + // the infix operator. + p.unexpected(prepend_msg: 'invalid expression:') + } else { + right = p.expr(if op == .power { precedence - 1 } else { precedence }) + } p.inside_assign_rhs = old_assign_rhs if op in [.plus, .minus, .mul, .power, .div, .mod, .lt, .eq] && mut right is ast.PrefixExpr { mut right_expr := right.right diff --git a/vlib/v/parser/tests/match_invalid_case_expr_err.out b/vlib/v/parser/tests/match_invalid_case_expr_err.out new file mode 100644 index 000000000..518953da1 --- /dev/null +++ b/vlib/v/parser/tests/match_invalid_case_expr_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/match_invalid_case_expr_err.vv:4:8: error: invalid expression: unexpected token `{` + 2 | x := 1 + 3 | match true { + 4 | x > { + | ^ + 5 | y := 0 + 6 | } diff --git a/vlib/v/parser/tests/match_invalid_case_expr_err.vv b/vlib/v/parser/tests/match_invalid_case_expr_err.vv new file mode 100644 index 000000000..434882115 --- /dev/null +++ b/vlib/v/parser/tests/match_invalid_case_expr_err.vv @@ -0,0 +1,9 @@ +fn test_match_invalid_case_expr() { + x := 1 + match true { + x > { + y := 0 + } + else {} + } +} -- 2.39.5