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

cgen: fix default initialization of option array with none values (#17536)

This commit is contained in:
Felipe Pena 2023-03-07 17:53:07 -03:00 committed by GitHub
parent b6c4e88462
commit b4f0bb247d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -361,7 +361,11 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.write(' (voidptr)${tmp})')
} else if node.has_default {
g.write('&(${elem_styp}[]){')
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
}
g.write('})')
} else if node.has_len && node.elem_type.has_flag(.option) {
g.write('&')

View File

@ -0,0 +1,10 @@
fn test_main() {
a := []?int{len: 3, init: none}
mut t := a[0]
assert t == none
t = a[1]
assert t == none
t = a[2]
assert t == none
assert a.len == 3
}