From 57c1faadbe6fc77623afb4559ec4fd8a268ffea9 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 13 Dec 2021 01:54:29 +0800 Subject: [PATCH] cgen: fix multiple nested embed struct with duplicate field init (#12805) --- vlib/v/gen/c/cgen.v | 1 + ...ed_struct_with_duplicate_field_init_test.v | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 vlib/v/tests/multiple_embed_struct_with_duplicate_field_init_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 0fe74f3d7b..5f00e21457 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6023,6 +6023,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { default_init := ast.StructInit{ ...struct_init typ: embed + fields: init_fields_to_embed } g.write('.$embed_name = ') g.struct_init(default_init) diff --git a/vlib/v/tests/multiple_embed_struct_with_duplicate_field_init_test.v b/vlib/v/tests/multiple_embed_struct_with_duplicate_field_init_test.v new file mode 100644 index 0000000000..04dc3b600a --- /dev/null +++ b/vlib/v/tests/multiple_embed_struct_with_duplicate_field_init_test.v @@ -0,0 +1,19 @@ +struct Foo { +mut: + x int +} + +struct Bar { + Foo +mut: + x int +} + +fn test_multiple_embed_struct_with_duplicate_field_init() { + mut b := Bar{ + x: 2 + } + println(b) + assert b.x == 2 + assert b.Foo.x == 0 +}