From 7c6dba33925865e3ad25ce38cdd7d6601113c4af Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 27 Dec 2025 18:17:56 -0300 Subject: [PATCH] cgen: fix option array init with non option values (fix #26148) (#26170) --- vlib/v/gen/c/array.v | 6 +++++- vlib/v/tests/options/option_array_init_mix_test.v | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/options/option_array_init_mix_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 58c01b34a..1183a8d47 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -175,7 +175,11 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st } else { node.elem_type } - g.expr_with_cast(expr, expr_type, node.elem_type) + if node.elem_type.has_flag(.option) { + g.expr_with_opt(expr, expr_type, node.elem_type) + } else { + g.expr_with_cast(expr, expr_type, node.elem_type) + } } g.add_commas_and_prevent_long_lines(i, nelen) } diff --git a/vlib/v/tests/options/option_array_init_mix_test.v b/vlib/v/tests/options/option_array_init_mix_test.v new file mode 100644 index 000000000..0bca5810c --- /dev/null +++ b/vlib/v/tests/options/option_array_init_mix_test.v @@ -0,0 +1,9 @@ +fn test_main() { + a := [?string('a'), 'b']! + println(a) + assert '${a}' == "[Option('a'), Option('b')]" + + b := [?int(1), 7]! + println(b) + assert '${b}' == '[Option(1), Option(7)]' +} -- 2.39.5