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:
parent
b60132d2ac
commit
523ccbcb70
@ -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
|
||||
}
|
||||
|
24
vlib/v/tests/if_expr_with_sumtype_map_test.v
Normal file
24
vlib/v/tests/if_expr_with_sumtype_map_test.v
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user