From 6dcedae784e1cdcf6b56e2bf0640b706f3fe3a7c Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 30 Jun 2025 16:04:15 -0300 Subject: [PATCH] cgen: fix if codegen when func parameter is option type (fix #24813) (#24816) --- vlib/v/gen/c/if.v | 1 + vlib/v/tests/fns/call_option_param_test.v | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 vlib/v/tests/fns/call_option_param_test.v diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 4305bdcc77..706037ab15 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -76,6 +76,7 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool { return true } } + return expr.expected_arg_types.any(it.has_flag(.option)) } ast.CastExpr { return g.need_tmp_var_in_expr(expr.expr) diff --git a/vlib/v/tests/fns/call_option_param_test.v b/vlib/v/tests/fns/call_option_param_test.v new file mode 100644 index 0000000000..a239ba436f --- /dev/null +++ b/vlib/v/tests/fns/call_option_param_test.v @@ -0,0 +1,15 @@ +pub fn new_group(t ?Texts) Group { + return Group{} +} + +struct Group { +} + +struct Texts { +} + +fn test_main() { + a := 0 + _ := if a != 0 { new_group(Texts{}) } else { Group{} } + _ := if a != 0 { new_group(none) } else { Group{} } +}