diff --git a/vlib/v/checker/tests/globals_run/global_nested_struct_test.run.out b/vlib/v/checker/tests/globals_run/global_nested_struct_test.run.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vlib/v/checker/tests/globals_run/global_nested_struct_test.vv b/vlib/v/checker/tests/globals_run/global_nested_struct_test.vv new file mode 100644 index 0000000000..5cf451a548 --- /dev/null +++ b/vlib/v/checker/tests/globals_run/global_nested_struct_test.vv @@ -0,0 +1,21 @@ +pub struct Foo { + val Bar +} + +pub struct Bar { + number int +} + +fn test_get() { + +} + +[cinit] +__global ( + baz = Foo{Bar{number: 5}} +) + + +fn main() { + print(baz) +} \ No newline at end of file diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 7a4e2d8272..f3bb3c63fd 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -140,6 +140,7 @@ mut: inside_const_opt_or_res bool inside_lambda bool inside_for_in_any_cond bool + inside_cinit bool loop_depth int ternary_names map[string]string ternary_level_names map[string][]string @@ -5050,6 +5051,10 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) { } // should the global be initialized now, not later in `vinit()` cinit := node.attrs.contains('cinit') + g.inside_cinit = cinit + defer { + g.inside_cinit = false + } cextern := node.attrs.contains('c_extern') should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module) || (g.pref.build_mode == .build_module && g.module_built == node.mod) diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 7e33c659cd..80c20295ee 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -42,7 +42,8 @@ fn (mut g Gen) struct_init(node ast.StructInit) { is_anon = info.is_anon } - if !is_anon { + + if !g.inside_cinit && !is_anon { g.write('(') defer { g.write(')') @@ -63,6 +64,12 @@ fn (mut g Gen) struct_init(node ast.StructInit) { } else { g.write('&(${basetyp}){') } + } else if g.inside_cinit { + if is_multiline { + g.writeln('{') + } else { + g.write('{') + } } else { if is_multiline { g.writeln('(${styp}){')