Skip to content

Commit

Permalink
cgen: fix struct field init with fixed array using index (fix #22616) (
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Oct 24, 2024
1 parent 9e67739 commit f715d49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,8 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua
info := field_unwrap_sym.info as ast.ArrayFixed
g.fixed_array_var_init(g.expr_string(sfield.expr), sfield.expr.is_auto_deref_var(),
info.elem_type, info.size)
} else if field_unwrap_sym.kind == .array_fixed && sfield.expr is ast.CallExpr {
} else if field_unwrap_sym.kind == .array_fixed && (sfield.expr is ast.CallExpr
|| (sfield.expr is ast.ArrayInit && sfield.expr.has_index)) {
info := field_unwrap_sym.info as ast.ArrayFixed
tmp_var := g.expr_with_var(sfield.expr, sfield.typ, sfield.expected_type)
g.fixed_array_var_init(tmp_var, false, info.elem_type, info.size)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn test_struct_field_init_with_fixed_array_init() {
mut a := [5]int{init: [1, 2][index] or { 0 }}
struct ArrayTest {
aa [5]int
}

array_a := ArrayTest{
aa: a
}
println('array_a: ${array_a}')
assert array_a.aa == [1, 2, 0, 0, 0]!

array_b := ArrayTest{
aa: [5]int{init: [1, 2][index] or { 0 }}
}
println('array_b: ${array_b}')
assert array_b.aa == [1, 2, 0, 0, 0]!
}

0 comments on commit f715d49

Please sign in to comment.