Skip to content

Commit

Permalink
cgen: fix call_expr in branches and left_expr of call_expr with or_bl…
Browse files Browse the repository at this point in the history
…ock(fix vlang#20044)
  • Loading branch information
shove70 committed Dec 5, 2023
1 parent cce21a4 commit 9a67936
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vlib/v/gen/c/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
if expr.or_block.kind != .absent {
return true
}
if g.need_tmp_var_in_expr(expr.left) {
return true
}
for arg in expr.args {
if g.need_tmp_var_in_expr(arg.expr) {
return true
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/if_match_branches_with_call_expr_with_or_block_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn foo() !string {
return 'abc'
}

// exists call_expr in the if-else branches and the left_expr of call_expr with or_block
fn test_if() {
res := if false {
''
} else {
foo() or { panic('error') }.trim('')
}
assert res == 'abc'
}

// exists call_expr in the match branches and the left_expr of call_expr with or_block
fn test_match() {
res := match false {
true {
''
}
else {
foo() or { panic('error') }.trim('')
}
}
assert res == 'abc'
}

0 comments on commit 9a67936

Please sign in to comment.