Skip to content

Commit 19ff25d

Browse files
authored
cgen: fix mixed fixed array and array initializing (#19246)
1 parent 652bb9a commit 19ff25d

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

vlib/v/gen/c/array.v

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
5353
} else {
5454
if node.elem_type.has_flag(.option) {
5555
g.expr_with_opt(expr, node.expr_types[i], node.elem_type)
56+
} else if elem_type.unaliased_sym.kind == .array_fixed
57+
&& expr in [ast.Ident, ast.SelectorExpr] {
58+
g.write('{')
59+
info := elem_type.unaliased_sym.info as ast.ArrayFixed
60+
for idx in 0 .. info.size {
61+
g.expr(expr)
62+
g.write('[${idx}]')
63+
if idx != info.size - 1 {
64+
g.write(', ')
65+
}
66+
}
67+
g.write('}')
5668
} else {
5769
g.expr_with_cast(expr, node.expr_types[i], node.elem_type)
5870
}
@@ -153,11 +165,29 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
153165
}
154166
g.write('{')
155167
if node.has_val {
168+
elem_type := (array_type.unaliased_sym.info as ast.ArrayFixed).elem_type
169+
elem_sym := g.table.final_sym(elem_type)
156170
for i, expr in node.exprs {
157-
if expr.is_auto_deref_var() {
158-
g.write('*')
171+
if elem_sym.kind == .array_fixed && expr in [ast.Ident, ast.SelectorExpr] {
172+
g.write('{')
173+
info := array_type.unaliased_sym.info as ast.ArrayFixed
174+
for idx in 0 .. info.size {
175+
if expr.is_auto_deref_var() {
176+
g.write('*')
177+
}
178+
g.expr(expr)
179+
g.write('[${idx}]')
180+
if idx != info.size - 1 {
181+
g.write(', ')
182+
}
183+
}
184+
g.write('}')
185+
} else {
186+
if expr.is_auto_deref_var() {
187+
g.write('*')
188+
}
189+
g.expr(expr)
159190
}
160-
g.expr(expr)
161191
if i != node.exprs.len - 1 {
162192
g.write(', ')
163193
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn test_mixed_fixed_array_and_array_init1() {
2+
a := [0.312, 12.0213]!
3+
b := [0.23, 2131.213]!
4+
ab := [a, b]
5+
println(ab)
6+
assert '${ab}' == '[[0.312, 12.0213], [0.23, 2131.213]]'
7+
}
8+
9+
fn test_mixed_fixed_array_and_array_init2() {
10+
a := [0.312, 12.0213]!
11+
b := [0.23, 2131.213]!
12+
mut ab := [a]
13+
ab << b
14+
println(ab)
15+
assert '${ab}' == '[[0.312, 12.0213], [0.23, 2131.213]]'
16+
}
17+
18+
fn test_mixed_fixed_array_and_array_init3() {
19+
a := [0.312, 12.0213]!
20+
b := [0.23, 2131.213]!
21+
mut ab := [a, b]!
22+
println(ab)
23+
assert '${ab}' == '[[0.312, 12.0213], [0.23, 2131.213]]'
24+
}

0 commit comments

Comments
 (0)