1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix array init with it (#15523)

This commit is contained in:
yuyi 2022-08-25 13:00:11 +08:00 committed by GitHub
parent 3ad22eb0dd
commit 86496aa191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -246,6 +246,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.indent++
g.writeln('$elem_typ* pelem = ($elem_typ*)${tmp}.data;')
g.writeln('for(int it=0; it<${tmp}.len; it++, pelem++) {')
g.set_current_pos_as_last_stmt_pos()
g.indent++
g.write('*pelem = ')
g.expr(node.default_expr)
@ -254,6 +255,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.writeln('}')
g.indent--
g.writeln('}')
g.set_current_pos_as_last_stmt_pos()
if var_name.len == 0 {
if s_ends_with_ln {
g.writeln(s)

View File

@ -15,3 +15,12 @@ fn test_array_with_it() {
a4 := []int{len: 5, init: 5 - it}
assert a4 == [5, 4, 3, 2, 1]
}
fn test_array_init_with_optional() {
input := [3.1, 1.1]
arr := []f64{len: 3, init: input[it] or { 0.0 }}
println(arr)
assert arr[0] == 3.1
assert arr[1] == 1.1
assert arr[2] == 0.0
}