From 094931cdc008c775517398271741334bab8c9acd Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 25 Mar 2026 16:42:16 +0300 Subject: [PATCH] cgen: fix c error in conditional attribution (fixes #26375) --- vlib/v/gen/c/if.v | 10 +++++++++- .../conditions/ifs/if_expr_of_multi_stmts_test.v | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 77982e221..d16d3bdbe 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -262,6 +262,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { } else { g.unwrap_generic(node_typ) } + resolved_sym := g.table.final_sym(resolved_typ) mut styp := g.styp(resolved_typ) if (g.inside_if_option || node_typ.has_flag(.option)) && !g.inside_or_block { raw_state = g.inside_if_option @@ -293,13 +294,20 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { g.empty_line = true if tmp != '' && !use_outer_tmp { // Only declare the tmp var if it's not from outer context + mut declared_tmp := false if node.typ == ast.void_type && g.last_if_option_type != 0 { // nested if on return stmt g.write2(g.styp(g.unwrap_generic(g.last_if_option_type)), ' ') + } else if resolved_sym.kind == .function && resolved_sym.info is ast.FnType { + g.writeln('${g.fn_var_signature(resolved_typ, resolved_sym.info.func.return_type, + resolved_sym.info.func.params.map(it.typ), tmp)}; /* if prepend */') + declared_tmp = true } else { g.write('${styp} ') } - g.writeln('${tmp}; /* if prepend */') + if !declared_tmp { + g.writeln('${tmp}; /* if prepend */') + } g.set_current_pos_as_last_stmt_pos() } if g.infix_left_var_name.len > 0 { diff --git a/vlib/v/tests/conditions/ifs/if_expr_of_multi_stmts_test.v b/vlib/v/tests/conditions/ifs/if_expr_of_multi_stmts_test.v index 308db3053..8ea0663ec 100644 --- a/vlib/v/tests/conditions/ifs/if_expr_of_multi_stmts_test.v +++ b/vlib/v/tests/conditions/ifs/if_expr_of_multi_stmts_test.v @@ -13,3 +13,18 @@ fn test_if_expr_of_multi_stmts() { } assert ret == 10 } + +fn test_if_expr_of_multi_stmts_returning_anon_fn() { + condition := true + f := if condition { + println('f') + fn (value string) string { + return 'left:${value}' + } + } else { + fn (value string) string { + return 'right:${value}' + } + } + assert f('x') == 'left:x' +} -- 2.39.5