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

cgen: fix array initialization with options (#17562)

This commit is contained in:
Felipe Pena 2023-03-08 16:54:00 -03:00 committed by GitHub
parent a0b3379d4a
commit b19052949f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -50,9 +50,13 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
g.write('string_clone(')
g.expr(expr)
g.write(')')
} else {
if node.elem_type.has_flag(.option) {
g.expr_with_opt(expr, node.expr_types[i], node.elem_type)
} else {
g.expr_with_cast(expr, node.expr_types[i], node.elem_type)
}
}
if i != len - 1 {
if i > 0 && i & 7 == 0 { // i > 0 && i % 8 == 0
g.writeln(',')

View File

@ -0,0 +1,11 @@
fn test_main() {
a := [?int(1), 0, 2]
mut t := a[0]
assert t? == 1
t = a[1]
assert t? == 0
t = a[2]
assert t? == 2
_ := [?int(none), none]
}