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

cgen: fix match expr with optional (#15658)

This commit is contained in:
yuyi 2022-09-04 18:21:16 +08:00 committed by GitHub
parent cee8b38221
commit 0c84ad847e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -28,7 +28,7 @@ fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool {
if branch.stmts.len == 1 {
if branch.stmts[0] is ast.ExprStmt {
stmt := branch.stmts[0] as ast.ExprStmt
if stmt.expr in [ast.CallExpr, ast.IfExpr, ast.MatchExpr]
if stmt.expr in [ast.CallExpr, ast.IfExpr, ast.MatchExpr, ast.StructInit]
|| (stmt.expr is ast.IndexExpr
&& (stmt.expr as ast.IndexExpr).or_expr.kind != .absent) {
return true

View File

@ -0,0 +1,17 @@
struct Test {
a string
}
fn test_match_expr_with_struct_init() {
a := map[string]string{}
b := match 'test' {
'test' {
Test{a['test'] or { '' }}
}
else {
Test{''}
}
}
println(b)
assert b.a == ''
}