mirror of
https://github.com/vlang/v.git
synced 2025-09-15 23:42:28 +03:00
cgen: fix fixed array with index variable (#19405)
This commit is contained in:
parent
5ddbbfcfd8
commit
b440f693aa
2 changed files with 11 additions and 7 deletions
|
@ -98,21 +98,19 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
|
||||||
}
|
}
|
||||||
g.write('{')
|
g.write('{')
|
||||||
if node.has_val {
|
if node.has_val {
|
||||||
for i, expr in node.exprs {
|
for i in 0 .. node.exprs.len {
|
||||||
if expr.is_auto_deref_var() {
|
|
||||||
g.write('*')
|
|
||||||
}
|
|
||||||
g.write('0')
|
g.write('0')
|
||||||
if i != node.exprs.len - 1 {
|
if i != node.exprs.len - 1 {
|
||||||
g.write(', ')
|
g.write(', ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if node.has_default {
|
} else if node.has_default {
|
||||||
g.write('0')
|
|
||||||
info := array_type.unaliased_sym.info as ast.ArrayFixed
|
info := array_type.unaliased_sym.info as ast.ArrayFixed
|
||||||
for _ in 1 .. info.size {
|
for i in 0 .. info.size {
|
||||||
g.write(', ')
|
|
||||||
g.write('0')
|
g.write('0')
|
||||||
|
if i != info.size - 1 {
|
||||||
|
g.write(', ')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
g.write('0')
|
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('${elem_typ}* pelem = (${elem_typ}*)${past.tmp_var};')
|
||||||
g.writeln('int _len = (int)sizeof(${past.tmp_var}) / sizeof(${elem_typ});')
|
g.writeln('int _len = (int)sizeof(${past.tmp_var}) / sizeof(${elem_typ});')
|
||||||
g.writeln('for (int index=0; index<_len; index++, pelem++) {')
|
g.writeln('for (int index=0; index<_len; index++, pelem++) {')
|
||||||
|
g.set_current_pos_as_last_stmt_pos()
|
||||||
g.indent++
|
g.indent++
|
||||||
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
|
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
|
||||||
g.write('*pelem = ')
|
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.writeln('}')
|
||||||
g.indent--
|
g.indent--
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
|
g.set_current_pos_as_last_stmt_pos()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
need_tmp_var := g.inside_call && !g.inside_struct_init && node.exprs.len == 0
|
need_tmp_var := g.inside_call && !g.inside_struct_init && node.exprs.len == 0
|
||||||
|
|
|
@ -9,4 +9,8 @@ fn test_fixed_array_of_option() {
|
||||||
a2[0] = 1
|
a2[0] = 1
|
||||||
println(a2)
|
println(a2)
|
||||||
assert '${a2}' == '[Option(1), Option(none), Option(none)]'
|
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)]'
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue