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

cgen: fix if expr with sumtype value of map (#16445)

This commit is contained in:
yuyi 2022-11-16 20:59:34 +08:00 committed by GitHub
parent b60132d2ac
commit 523ccbcb70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -11,7 +11,10 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool {
return true
}
for branch in node.branches {
if branch.cond is ast.IfGuardExpr || branch.stmts.len > 1 {
if branch.stmts.len > 1 {
return true
}
if g.need_tmp_var_in_expr(branch.cond) {
return true
}
if branch.stmts.len == 1 {
@ -37,6 +40,17 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
return true
}
}
ast.IfGuardExpr {
return true
}
ast.InfixExpr {
if g.need_tmp_var_in_expr(expr.left) {
return true
}
if g.need_tmp_var_in_expr(expr.right) {
return true
}
}
ast.MatchExpr {
return true
}

View File

@ -0,0 +1,24 @@
module main
type ConfigValue = bool | int | string
type ConfigMap = map[string]ConfigValue
fn foo(conf ConfigMap) bool {
mut bar := false
// Check type
bar = if conf['baz'] or { false } is bool {
conf['baz'] or { false } as bool
} else {
false
} // Default value
return bar
}
fn test_if_expr_with_sumtype_map() {
conf := {
'baz': ConfigValue(123)
}
ret := foo(conf)
println(ret)
assert !ret
}