Skip to content

Commit ab89a21

Browse files
authored
cgen: fix array init with option array (fix #26329) (#26331)
1 parent a375f05 commit ab89a21

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

vlib/v/gen/c/array.v

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -198,25 +198,11 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
198198
}
199199
} else if node.has_init {
200200
info := array_type.unaliased_sym.info as ast.ArrayFixed
201-
if node.elem_type.has_flag(.option) {
202-
for i in 0 .. info.size {
203-
g.expr_with_init(node)
204-
g.add_commas_and_prevent_long_lines(i, info.size)
205-
}
206-
} else {
207-
if g.table.final_sym(info.elem_type).kind in [.struct, .interface] {
208-
for i in 0 .. info.size {
209-
g.expr_with_init(node)
210-
g.add_commas_and_prevent_long_lines(i, info.size)
211-
}
212-
} else {
213-
before_expr_pos := g.out.len
214-
{
215-
g.expr_with_init(node)
216-
}
217-
sexpr := g.out.cut_to(before_expr_pos)
218-
g.write_c99_elements_for_array(info.size, sexpr)
219-
}
201+
// Always loop and call expr_with_init for each element to avoid copying
202+
// temporary variable declarations and initialization code
203+
for i in 0 .. info.size {
204+
g.expr_with_init(node)
205+
g.add_commas_and_prevent_long_lines(i, info.size)
220206
}
221207
} else if is_amp {
222208
g.write('0')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module main
2+
3+
fn test_fixed_array_init_with_option_array() {
4+
z := [2][]?int{init: []?int{len: 3}}
5+
assert z.str() == '[[Option(none), Option(none), Option(none)], [Option(none), Option(none), Option(none)]]'
6+
}

0 commit comments

Comments
 (0)