Skip to content

Commit

Permalink
cgen: fix if expr with fn call result (#15702)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 8, 2022
1 parent ec2ca38 commit 71f5f7f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/v/gen/c/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
if expr.or_block.kind != .absent {
return true
}
for arg in expr.args {
if g.need_tmp_var_in_expr(arg.expr) {
return true
}
}
}
ast.CastExpr {
return g.need_tmp_var_in_expr(expr.expr)
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/tests/if_expr_with_fn_call_result_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn foo() !int {
return 0
}

fn bar(n int) bool {
return true
}

fn is_ok(b bool) !bool {
return if b {
bar(foo()!)
} else {
true
}
}

fn test_if_expr_with_fn_call_result() {
ret := is_ok(true) or { false }
println(ret)
assert ret
}

0 comments on commit 71f5f7f

Please sign in to comment.