From 71f5f7f3a7b35ebbb4de2a8baeb527215a1bb8c1 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 9 Sep 2022 03:39:49 +0800 Subject: [PATCH] cgen: fix if expr with fn call result (#15702) --- vlib/v/gen/c/if.v | 5 +++++ .../tests/if_expr_with_fn_call_result_test.v | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 vlib/v/tests/if_expr_with_fn_call_result_test.v diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 683e9f6de6..6a05c72ed8 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -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) diff --git a/vlib/v/tests/if_expr_with_fn_call_result_test.v b/vlib/v/tests/if_expr_with_fn_call_result_test.v new file mode 100644 index 0000000000..5259f01387 --- /dev/null +++ b/vlib/v/tests/if_expr_with_fn_call_result_test.v @@ -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 +}