Skip to content

Commit a985192

Browse files
authored
cgen: fix multi dim fixed array with opt elem (fix #26243) (#26258)
1 parent 046901e commit a985192

6 files changed

Lines changed: 32 additions & 2 deletions

File tree

vlib/v/gen/c/array.v

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,14 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
236236
} else if elem_sym.kind == .array_fixed {
237237
// nested fixed array -- [N][N]type
238238
arr_info := elem_sym.array_fixed_info()
239-
before_arr_expr_pos := g.out.len
240239
{
241240
g.expr(ast.ArrayInit{
242241
exprs: [ast.IntegerLiteral{}]
243242
typ: node.elem_type
244243
elem_type: arr_info.elem_type
245244
})
246245
}
247-
sarr_expr := g.out.cut_to(before_arr_expr_pos)
246+
sarr_expr := g.cut_and_get_fixed_array_init_elements()
248247
g.write_c99_elements_for_array(array_info.size, sarr_expr)
249248
} else if elem_sym.kind == .chan {
250249
// fixed array for chan -- [N]chan
@@ -280,6 +279,28 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
280279
}
281280
}
282281

282+
// cut_and_get_fixed_array_init_elements
283+
// for `Array_fixed_Array_fixed_Array_fixed__option_int_2_2_2 a = {{ _t1, _t2}`
284+
// will cut to `=` and return `{ _t1, _t2}`
285+
@[direct_array_access]
286+
fn (mut g Gen) cut_and_get_fixed_array_init_elements() string {
287+
// extract the `{{},{}}` string
288+
mut nested_level := 0
289+
for i := g.out.len - 1; i >= 0; i-- {
290+
if g.out[i] == `}` {
291+
nested_level++
292+
} else if g.out[i] == `{` {
293+
nested_level--
294+
} else if g.out[i] == ` ` {
295+
continue
296+
}
297+
if nested_level == 0 {
298+
return g.out.cut_to(i)
299+
}
300+
}
301+
return '/*this should not happend*/'
302+
}
303+
283304
fn (mut g Gen) expr_with_init(node ast.ArrayInit) {
284305
if node.elem_type.has_flag(.option) {
285306
g.expr_with_opt(node.init_expr, node.init_type, node.elem_type)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[[Option(none), Option(none)], [Option(none), Option(none)], [Option(none), Option(none)]], [[Option(none), Option(none)], [Option(none), Option(none)], [Option(none), Option(none)]]]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a := [2][3][2]?int{}
2+
println(a)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
done
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_ := [2][3]?int{}
2+
println('done')

vlib/v/markused/walker.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,9 @@ pub fn (mut w Walker) mark_by_sym(isym ast.TypeSymbol) {
12471247
if !w.uses_array && !w.is_direct_array_access {
12481248
w.uses_array = true
12491249
}
1250+
if isym.info.elem_type.has_flag(.option) {
1251+
w.used_option++
1252+
}
12501253
w.mark_by_type(isym.info.elem_type)
12511254
}
12521255
ast.SumType {

0 commit comments

Comments
 (0)