File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,10 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool {
11
11
return true
12
12
}
13
13
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) {
15
18
return true
16
19
}
17
20
if branch.stmts.len == 1 {
@@ -37,6 +40,17 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
37
40
return true
38
41
}
39
42
}
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
+ }
40
54
ast.MatchExpr {
41
55
return true
42
56
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments