From b6bbd2463ce8d39f9108740246975c6f6312b1fb Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 28 Apr 2023 10:59:18 -0300 Subject: [PATCH] cgen: fix shared struct field initialization with default value (#18075) --- vlib/v/gen/c/struct.v | 5 +---- vlib/v/tests/shared_struct_field_test.v | 13 +++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 vlib/v/tests/shared_struct_field_test.v diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index d38a7e9407..e46f149512 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -161,9 +161,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) { g.is_shared = old_is_shared2 } for mut field in info.fields { - if !field.typ.has_flag(.shared_f) { - g.is_shared = false - } + g.is_shared = field.typ.has_flag(.shared_f) if mut sym.info is ast.Struct { mut found_equal_fields := 0 for mut sifield in sym.info.fields { @@ -280,7 +278,6 @@ fn (mut g Gen) struct_init(node ast.StructInit) { g.write(',') } initialized = true - g.is_shared = old_is_shared } g.is_shared = old_is_shared } diff --git a/vlib/v/tests/shared_struct_field_test.v b/vlib/v/tests/shared_struct_field_test.v new file mode 100644 index 0000000000..0a68ddba9b --- /dev/null +++ b/vlib/v/tests/shared_struct_field_test.v @@ -0,0 +1,13 @@ +module main + +struct StructA { + ints shared []int = [0] +} + +fn test_main() { + a := StructA{} + rlock a.ints { + dump(a) + assert true + } +}