From b0b08bc01806f763a6606d7484182f7fc20c14ce Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 9 Jan 2025 04:07:15 -0300 Subject: [PATCH] cgen: fix shared array fixed initializing with `-cstrict` (fix build of chip8-v project) (#23414) --- vlib/v/gen/c/struct.v | 22 ++++++++++++------- .../shared_fixed_array_init_test.v | 9 ++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 vlib/v/tests/concurrency/shared_fixed_array_init_test.v diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 23e892f87..afc1e2d8e 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -498,15 +498,21 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool { g.write(g.type_default_sumtype(field.typ, sym)) return true } else if sym.info is ast.ArrayFixed { + elem_is_option := sym.info.elem_type.has_flag(.option) 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(', ') + if !elem_is_option && field.typ.has_flag(.shared_f) { + g.write('0') + } else { + default_str := g.type_default(sym.info.elem_type) + for i in 0 .. sym.info.size { + if elem_is_option { + g.gen_option_error(sym.info.elem_type, ast.None{}) + } else { + g.write(default_str) + } + if i != sym.info.size - 1 { + g.write(', ') + } } } g.write('}') diff --git a/vlib/v/tests/concurrency/shared_fixed_array_init_test.v b/vlib/v/tests/concurrency/shared_fixed_array_init_test.v new file mode 100644 index 000000000..16230b343 --- /dev/null +++ b/vlib/v/tests/concurrency/shared_fixed_array_init_test.v @@ -0,0 +1,9 @@ +struct Foo { +mut: + bar shared [1024]bool +} + +fn test_main() { + _ := Foo{} + assert true +} -- 2.39.5