Skip to content

Commit

Permalink
cgen: fix struct field of fixed array init (fix #19483) (#19487)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Oct 1, 2023
1 parent f8693ad commit 6bd4539
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
13 changes: 13 additions & 0 deletions vlib/v/gen/c/struct.v
Expand Up @@ -386,6 +386,19 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
tmp_var := g.new_tmp_var()
g.expr_with_tmp_var(ast.None{}, ast.none_type, field.typ, tmp_var)
return true
} else if sym.info is ast.ArrayFixed {
g.write('{')
for i in 0 .. sym.info.size {
if sym.info.elem_type.has_flag(.option) {
g.expr_with_opt(ast.None{}, ast.none_type, sym.info.elem_type)
} else {
g.write(g.type_default(sym.info.elem_type))
}
if i != sym.info.size - 1 {
g.write(', ')
}
}
g.write('}')
} else {
g.write(g.type_default(field.typ))
}
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/tests/array_elements_with_option_test.v
Expand Up @@ -13,8 +13,8 @@ fn get_no_option_fixed() int {
return foo.arr2[0]
}

fn test_option_fixed() ? {
x := get_has_option_fixed()?
fn test_option_fixed() {
x := get_has_option_fixed() or { 0 }
assert x == 0
assert get_no_option_fixed() == 0
}
Expand All @@ -37,7 +37,7 @@ fn get_no_option() int {
return foo.arr2[0]
}

fn test_option_non_fixed() ? {
fn test_option_non_fixed() {
x := get_has_option()?
assert x == 0
assert get_no_option() == 0
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/struct_field_fixed_array_init_test.v
@@ -0,0 +1,10 @@
struct Foo {
mut:
bar [2]string
}

fn test_struct_field_fixed_array_init() {
foo := Foo{}
println(foo.bar[0])
assert foo.bar[0] == ''
}

0 comments on commit 6bd4539

Please sign in to comment.