From dbb18e3656211619f3f2c58d702def7325c5ee8a Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 14 Mar 2022 22:19:05 +0800 Subject: [PATCH] parser: fix error for match branch with array expression (#13733) --- vlib/v/parser/if_match.v | 20 ++++++++++++++++++- .../match_branch_with_array_expression_test.v | 8 ++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/match_branch_with_array_expression_test.v diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index ad4ce01a92..0bad3f9a63 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -173,6 +173,24 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { } } +fn (mut p Parser) is_only_array_type() bool { + if p.tok.kind == .lsbr { + for i in 1 .. 20 { + if p.peek_token(i).kind == .rsbr { + next_kind := p.peek_token(i + 1).kind + if next_kind == .name { + return true + } else if next_kind == .lsbr { + continue + } else { + return false + } + } + } + } + return false +} + fn (mut p Parser) match_expr() ast.MatchExpr { match_first_pos := p.tok.pos() p.inside_match = true @@ -199,7 +217,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr { } else if (p.tok.kind == .name && !(p.tok.lit == 'C' && p.peek_tok.kind == .dot) && (((ast.builtin_type_names_matcher.find(p.tok.lit) > 0 || p.tok.lit[0].is_capital()) && p.peek_tok.kind != .lpar) || (p.peek_tok.kind == .dot && p.peek_token(2).lit.len > 0 - && p.peek_token(2).lit[0].is_capital()))) || p.tok.kind == .lsbr { + && p.peek_token(2).lit[0].is_capital()))) || p.is_only_array_type() { mut types := []ast.Type{} for { // Sum type match diff --git a/vlib/v/tests/match_branch_with_array_expression_test.v b/vlib/v/tests/match_branch_with_array_expression_test.v new file mode 100644 index 0000000000..7b868fe3cd --- /dev/null +++ b/vlib/v/tests/match_branch_with_array_expression_test.v @@ -0,0 +1,8 @@ +fn test_match_branch_with_array_expression() { + ret := match true { + [''].contains('') { 22 } + else { 0 } + } + println(ret) + assert ret == 22 +}