ast: allow `a := match x { 101 { ... for {...} ... y }`

master
Delyan Angelov 2022-04-21 13:15:21 +03:00
parent 8a18f9175a
commit 2080557f50
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 61 additions and 7 deletions

View File

@ -1821,6 +1821,9 @@ pub fn (stmt Stmt) check_c_expr() ? {
AssignStmt {
return
}
ForCStmt, ForInStmt, ForStmt {
return
}
ExprStmt {
if stmt.expr.is_expr() {
return

View File

@ -1,10 +1,3 @@
vlib/v/checker/tests/if_match_expr.vv:8:6: error: `if` expression branch has unsupported statement (`v.ast.ForStmt`)
6 | if true {1} else {-1} // result
7 | } else {
8 | for {break}
| ^
9 | {}
10 | match true {true {} else {}} // statement not expression
vlib/v/checker/tests/if_match_expr.vv:9:2: error: `if` expression branch has unsupported statement (`v.ast.Block`)
7 | } else {
8 | for {break}

View File

@ -0,0 +1,58 @@
fn test_match_with_for_in_loop() {
a := 101
b := match a {
101 {
mut aa := []int{}
for i in 0 .. 3 {
aa << i
}
aa
}
else {
[0]
}
}
println(b)
assert b == [0, 1, 2]
}
fn test_match_with_for_c_loop() {
a := 101
b := match a {
101 {
mut aa := []int{}
for i := 0; i < 3; i++ {
aa << i
}
aa
}
else {
[0]
}
}
println(b)
assert b == [0, 1, 2]
}
fn test_match_with_for_loop() {
a := 101
b := match a {
101 {
mut aa := []int{}
mut i := 0
for {
aa << i
i++
if i == 3 {
break
}
}
aa
}
else {
[0]
}
}
println(b)
assert b == [0, 1, 2]
}