cgen: fix fixed array with index variable (#19405)

This commit is contained in:
yuyi 2023-09-22 07:31:30 +08:00 committed by GitHub
parent 5ddbbfcfd8
commit b440f693aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View file

@ -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')
@ -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 = ')
@ -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

View file

@ -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)]'
}