mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix error of nested match expr (#12334)
This commit is contained in:
@@ -1557,7 +1557,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
||||
// }
|
||||
old_is_void_expr_stmt := g.is_void_expr_stmt
|
||||
g.is_void_expr_stmt = !node.is_expr
|
||||
if node.typ != ast.void_type && g.expected_cast_type != 0 {
|
||||
if node.typ != ast.void_type && g.expected_cast_type != 0 && node.expr !is ast.MatchExpr {
|
||||
g.expr_with_cast(node.expr, node.typ, g.expected_cast_type)
|
||||
} else {
|
||||
g.expr(node.expr)
|
||||
|
||||
35
vlib/v/tests/match_expr_nested_test.v
Normal file
35
vlib/v/tests/match_expr_nested_test.v
Normal file
@@ -0,0 +1,35 @@
|
||||
struct Abc {}
|
||||
|
||||
type Test = Abc | bool | int
|
||||
|
||||
fn test(a Test) Test {
|
||||
return match a {
|
||||
Abc {
|
||||
20
|
||||
}
|
||||
int {
|
||||
match a {
|
||||
1 { true }
|
||||
2 { false }
|
||||
else { -1 }
|
||||
}
|
||||
}
|
||||
bool {
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_nested_match_expr() {
|
||||
println(test(1))
|
||||
assert test(1) == Test(true)
|
||||
|
||||
println(test(2))
|
||||
assert test(2) == Test(false)
|
||||
|
||||
println(test(3))
|
||||
assert test(3) == Test(-1)
|
||||
|
||||
println(test(true))
|
||||
assert test(true) == Test(1)
|
||||
}
|
||||
Reference in New Issue
Block a user