Skip to content

Commit 2edfb58

Browse files
authored
cgen: fix codegen for fixed array init with init using structinit (#24269)
1 parent 11e25bc commit 2edfb58

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

vlib/v/gen/c/array.v

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,19 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
180180
g.add_commas_and_prevent_long_lines(i, info.size)
181181
}
182182
} else {
183-
before_expr_pos := g.out.len
184-
{
185-
g.expr_with_init(node)
183+
if g.table.final_sym(info.elem_type).kind in [.struct, .interface] {
184+
for i in 0 .. info.size {
185+
g.expr_with_init(node)
186+
g.add_commas_and_prevent_long_lines(i, info.size)
187+
}
188+
} else {
189+
before_expr_pos := g.out.len
190+
{
191+
g.expr_with_init(node)
192+
}
193+
sexpr := g.out.cut_to(before_expr_pos)
194+
g.write_c99_elements_for_array(info.size, sexpr)
186195
}
187-
sexpr := g.out.cut_to(before_expr_pos)
188-
g.write_c99_elements_for_array(info.size, sexpr)
189196
}
190197
} else if is_amp {
191198
g.write('0')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct Structure1 {
2+
world [2]Structure2
3+
}
4+
5+
struct Structure2 {
6+
liste []Structure3
7+
}
8+
9+
struct Structure3 {
10+
coo []int
11+
}
12+
13+
fn test_main() {
14+
app := Structure1{
15+
world: [2]Structure2{init: Structure2{
16+
liste: []Structure3{len: 1, init: Structure3{}}
17+
}}
18+
}
19+
assert app.world.len == 2
20+
}

0 commit comments

Comments
 (0)