Skip to content

Commit

Permalink
fix other cases and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Dec 18, 2024
1 parent c9522fc commit 9abb53a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
23 changes: 18 additions & 5 deletions vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -688,13 +688,26 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua
if field_unwrap_sym.info is ast.ArrayFixed {
match sfield.expr {
ast.Ident, ast.SelectorExpr {
g.fixed_array_var_init(g.expr_string(sfield.expr), is_auto_deref_var,
field_unwrap_sym.info.elem_type, field_unwrap_sym.info.size)
if sfield.expected_type.has_flag(.option) {
if field_unwrap_typ.has_flag(.option) {
g.expr_with_opt(sfield.expr, sfield.expected_type, field_unwrap_typ)
} else {
g.expr_with_fixed_array_opt(sfield.expr, sfield.expected_type,
field_unwrap_typ)
}
} else {
g.fixed_array_var_init(g.expr_string(sfield.expr), is_auto_deref_var,
field_unwrap_sym.info.elem_type, field_unwrap_sym.info.size)
}
}
ast.CastExpr, ast.CallExpr {
if sfield.expected_type.has_flag(.option) && !field_unwrap_typ.has_flag(.option) {
g.expr_with_fixed_array_opt(sfield.expr, sfield.expected_type,
field_unwrap_typ)
if sfield.expected_type.has_flag(.option) {
if field_unwrap_typ.has_flag(.option) {
g.expr_with_opt(sfield.expr, sfield.expected_type, field_unwrap_typ)
} else {
g.expr_with_fixed_array_opt(sfield.expr, sfield.expected_type,
field_unwrap_typ)
}
} else {
tmp_var := g.expr_with_var(sfield.expr, sfield.expected_type,
false)
Expand Down
23 changes: 20 additions & 3 deletions vlib/v/tests/structs/struct_field_init_with_fixed_array_opt_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ struct Foo {
}

fn test_struct_field_init_with_fixed_array_opt() {
f := Foo{
f1 := Foo{
bar: 1
baz: Arr([u8(5), 4, 3, 2]!)
}
println(f)
assert f.baz as Arr == [u8(5), 4, 3, 2]!
println(f1)
assert f1.baz as Arr == [u8(5), 4, 3, 2]!

f2 := Foo{
bar: 1
baz: ?Arr(none)
}
println(f2)
assert f2.bar == 1
assert f2.baz == none

arr := Arr([u8(5), 4, 3, 2]!)
f3 := Foo{
bar: 1
baz: arr
}
println(f3)
assert f3.bar == 1
assert f3.baz as Arr == [u8(5), 4, 3, 2]!
}

0 comments on commit 9abb53a

Please sign in to comment.