From 5e40827138526a9c03c1b1903c10c3a5f5df8c16 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 11 Jan 2025 19:28:42 -0300 Subject: [PATCH] fix --- vlib/v/gen/c/if.v | 9 +++++++++ .../options/option_ifguard_array_of_option_test.v | 14 ++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 vlib/v/tests/options/option_ifguard_array_of_option_test.v diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index bfb5175d5ba43f..d19389ade4b837 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -260,6 +260,15 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { cond.expr_type } g.writeln('${g.styp(g.unwrap_generic(cond_expr_type))} ${var_name};') + } else if cond.expr is ast.IndexExpr { + value_type := g.table.value_type(g.unwrap_generic(cond.expr.left_type)) + if value_type.has_flag(.option) { + var_name := g.new_tmp_var() + guard_vars[i] = var_name + g.writeln('${g.styp(value_type)} ${var_name};') + } else { + guard_vars[i] = '' + } } else { guard_vars[i] = '' } diff --git a/vlib/v/tests/options/option_ifguard_array_of_option_test.v b/vlib/v/tests/options/option_ifguard_array_of_option_test.v new file mode 100644 index 00000000000000..49466514dafd9e --- /dev/null +++ b/vlib/v/tests/options/option_ifguard_array_of_option_test.v @@ -0,0 +1,14 @@ +module main + +fn make_option() ?string { + return 'abc' +} + +fn main() { + cols := [make_option()] + if col := cols[0] { + assert col == 'abc' + assert true + dump(col) + } +}