From 3b8f2fe5f5824642664aa6686c6aeb7ca4b2e244 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 10 May 2025 11:36:51 -0300 Subject: [PATCH] cgen: fix array init with interface element type (fix #24442) (#24454) --- vlib/v/gen/c/array.v | 4 ++-- .../interfaces/interface_array_init_test.v | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/interfaces/interface_array_init_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 22ed01831..cb17fcff1 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -45,12 +45,12 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) { g.writeln('') g.write('\t\t') } - is_sumtype := elem_sym.kind == .sum_type + is_iface_or_sumtype := elem_sym.kind in [.sum_type, .interface] for i, expr in node.exprs { expr_type := if node.expr_types.len > i { node.expr_types[i] } else { node.elem_type } if expr_type == ast.string_type && expr !in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral, ast.InfixExpr] { - if is_sumtype { + if is_iface_or_sumtype { g.expr_with_cast(expr, expr_type, node.elem_type) } else { g.write('string_clone(') diff --git a/vlib/v/tests/interfaces/interface_array_init_test.v b/vlib/v/tests/interfaces/interface_array_init_test.v new file mode 100644 index 000000000..3d38815f2 --- /dev/null +++ b/vlib/v/tests/interfaces/interface_array_init_test.v @@ -0,0 +1,20 @@ +module main + +interface Value {} + +struct CreateRegionData { + name string + currency_code string + tax_rate f32 + tax_code string @[omitempty] + countries []string + includes_tax bool @[omitempty] +} + +fn test_main() { + rd := CreateRegionData{} + id := 'bro' + the_array := [Value(id), rd.name] + println(the_array) + assert '${the_array}' == "[Value('bro'), Value('')]" +} -- 2.39.5