From 1bfeda62563c2227aab044df793a16c30ef1022e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 3 Jan 2025 13:01:25 -0300 Subject: [PATCH] cgen: fix codegen for alias struct embed (fix #23347) (#23353) --- vlib/v/gen/c/struct.v | 6 +++++- vlib/v/tests/struct_with_alias_embed_test.v | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/struct_with_alias_embed_test.v diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 2103ba4fb1e1b3..23e892f87380ae 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -197,7 +197,11 @@ fn (mut g Gen) struct_init(node ast.StructInit) { embed_sym := g.table.sym(embed) embed_name := embed_sym.embed_name() if embed_name !in inited_fields { - embed_info := embed_sym.info as ast.Struct + embed_info := if embed_sym.info is ast.Struct { + embed_sym.info + } else { + g.table.final_sym(embed).info as ast.Struct + } embed_field_names := embed_info.fields.map(it.name) fields_to_embed := init_fields_to_embed.filter(it.name !in used_embed_fields && it.name in embed_field_names) diff --git a/vlib/v/tests/struct_with_alias_embed_test.v b/vlib/v/tests/struct_with_alias_embed_test.v new file mode 100644 index 00000000000000..3833c740f82b86 --- /dev/null +++ b/vlib/v/tests/struct_with_alias_embed_test.v @@ -0,0 +1,13 @@ +struct Foo1 {} + +type Foo2 = Foo1 + +struct Bar { + Foo2 +} + +fn test_main() { + assert Bar{}.str() == 'Bar{ + Foo2: Foo2(Foo1{}) +}' +}