From 2edfb583b40156bc9174dc90b658c5b3554ea220 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 19 Apr 2025 21:00:56 -0300 Subject: [PATCH] cgen: fix codegen for fixed array init with init using structinit (#24269) --- vlib/v/gen/c/array.v | 17 +++++++++++----- .../v/tests/fixed_array_init_with_init_test.v | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 vlib/v/tests/fixed_array_init_with_init_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index b79c5334c..2dbd2cc79 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -180,12 +180,19 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st g.add_commas_and_prevent_long_lines(i, info.size) } } else { - before_expr_pos := g.out.len - { - g.expr_with_init(node) + if g.table.final_sym(info.elem_type).kind in [.struct, .interface] { + for i in 0 .. info.size { + g.expr_with_init(node) + g.add_commas_and_prevent_long_lines(i, info.size) + } + } else { + before_expr_pos := g.out.len + { + g.expr_with_init(node) + } + sexpr := g.out.cut_to(before_expr_pos) + g.write_c99_elements_for_array(info.size, sexpr) } - sexpr := g.out.cut_to(before_expr_pos) - g.write_c99_elements_for_array(info.size, sexpr) } } else if is_amp { g.write('0') diff --git a/vlib/v/tests/fixed_array_init_with_init_test.v b/vlib/v/tests/fixed_array_init_with_init_test.v new file mode 100644 index 000000000..050914309 --- /dev/null +++ b/vlib/v/tests/fixed_array_init_with_init_test.v @@ -0,0 +1,20 @@ +struct Structure1 { + world [2]Structure2 +} + +struct Structure2 { + liste []Structure3 +} + +struct Structure3 { + coo []int +} + +fn test_main() { + app := Structure1{ + world: [2]Structure2{init: Structure2{ + liste: []Structure3{len: 1, init: Structure3{}} + }} + } + assert app.world.len == 2 +} -- 2.39.5