diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index d45f965145..9dda0c2054 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -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 } diff --git a/vlib/v/tests/if_expr_with_sumtype_map_test.v b/vlib/v/tests/if_expr_with_sumtype_map_test.v new file mode 100644 index 0000000000..09a379c770 --- /dev/null +++ b/vlib/v/tests/if_expr_with_sumtype_map_test.v @@ -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 +}