1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: disallow multi else branches in match (#18526)

This commit is contained in:
Swastik Baranwal 2023-06-23 01:09:35 +05:30 committed by GitHub
parent 0e4eea80ca
commit 0b2e947e0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -213,6 +213,7 @@ fn (mut p Parser) is_only_array_type() bool {
fn (mut p Parser) match_expr() ast.MatchExpr {
match_first_pos := p.tok.pos()
mut else_count := 0
p.inside_match = true
p.check(.key_match)
mut is_sum_type := false
@ -233,6 +234,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
mut is_else := false
if p.tok.kind == .key_else {
is_else = true
else_count += 1
p.next()
} else if (p.tok.kind == .name && !(p.tok.lit == 'C' && p.peek_tok.kind == .dot)
&& (((ast.builtin_type_names_matcher.matches(p.tok.lit) || p.tok.lit[0].is_capital())
@ -328,6 +330,9 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
if is_else && branches.len == 1 {
p.error_with_pos('`match` must have at least one non `else` branch', pos)
}
if else_count > 1 {
p.error_with_pos('`match` can have only one `else` branch', pos)
}
if p.tok.kind == .rcbr || (is_else && no_lcbr) {
break
}

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/match_multi_else_branch_err.vv:5:3: error: `match` can have only one `else` branch
3 | true { 1 }
4 | else { 3 }
5 | else { 5 }
| ~~~~
6 | }
7 | }

View File

@ -0,0 +1,7 @@
fn main() {
_ := match true {
true { 1 }
else { 3 }
else { 5 }
}
}