Skip to content

Commit

Permalink
cgen: fix fixed array with index variable (#19405)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Sep 21, 2023
1 parent 5ddbbfc commit b440f69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 7 additions & 7 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,19 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
}
g.write('{')
if node.has_val {
for i, expr in node.exprs {
if expr.is_auto_deref_var() {
g.write('*')
}
for i in 0 .. node.exprs.len {
g.write('0')
if i != node.exprs.len - 1 {
g.write(', ')
}
}
} else if node.has_default {
g.write('0')
info := array_type.unaliased_sym.info as ast.ArrayFixed
for _ in 1 .. info.size {
g.write(', ')
for i in 0 .. info.size {
g.write('0')
if i != info.size - 1 {
g.write(', ')
}
}
} else {
g.write('0')
Expand All @@ -124,6 +122,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.writeln('${elem_typ}* pelem = (${elem_typ}*)${past.tmp_var};')
g.writeln('int _len = (int)sizeof(${past.tmp_var}) / sizeof(${elem_typ});')
g.writeln('for (int index=0; index<_len; index++, pelem++) {')
g.set_current_pos_as_last_stmt_pos()
g.indent++
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
g.write('*pelem = ')
Expand All @@ -133,6 +132,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.writeln('}')
g.indent--
g.writeln('}')
g.set_current_pos_as_last_stmt_pos()
return
}
need_tmp_var := g.inside_call && !g.inside_struct_init && node.exprs.len == 0
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/tests/fixed_array_of_option_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ fn test_fixed_array_of_option() {
a2[0] = 1
println(a2)
assert '${a2}' == '[Option(1), Option(none), Option(none)]'

a3 := [3]?int{init: ?int(index * 2)}
println(a3)
assert '${a3}' == '[Option(0), Option(2), Option(4)]'
}

0 comments on commit b440f69

Please sign in to comment.