Skip to content

Commit 523ccbc

Browse files
authored
cgen: fix if expr with sumtype value of map (#16445)
1 parent b60132d commit 523ccbc

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

vlib/v/gen/c/if.v

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool {
1111
return true
1212
}
1313
for branch in node.branches {
14-
if branch.cond is ast.IfGuardExpr || branch.stmts.len > 1 {
14+
if branch.stmts.len > 1 {
15+
return true
16+
}
17+
if g.need_tmp_var_in_expr(branch.cond) {
1518
return true
1619
}
1720
if branch.stmts.len == 1 {
@@ -37,6 +40,17 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
3740
return true
3841
}
3942
}
43+
ast.IfGuardExpr {
44+
return true
45+
}
46+
ast.InfixExpr {
47+
if g.need_tmp_var_in_expr(expr.left) {
48+
return true
49+
}
50+
if g.need_tmp_var_in_expr(expr.right) {
51+
return true
52+
}
53+
}
4054
ast.MatchExpr {
4155
return true
4256
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module main
2+
3+
type ConfigValue = bool | int | string
4+
type ConfigMap = map[string]ConfigValue
5+
6+
fn foo(conf ConfigMap) bool {
7+
mut bar := false
8+
// Check type
9+
bar = if conf['baz'] or { false } is bool {
10+
conf['baz'] or { false } as bool
11+
} else {
12+
false
13+
} // Default value
14+
return bar
15+
}
16+
17+
fn test_if_expr_with_sumtype_map() {
18+
conf := {
19+
'baz': ConfigValue(123)
20+
}
21+
ret := foo(conf)
22+
println(ret)
23+
assert !ret
24+
}

0 commit comments

Comments
 (0)