From ade2a4cd0188182d4f58989800161544c7d2e5e9 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 11 Dec 2021 15:28:32 +0800 Subject: [PATCH] cgen: fix multiple embed struct init (#12791) --- vlib/v/gen/c/cgen.v | 2 +- .../v/tests/multiple_embed_struct_init_test.v | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/multiple_embed_struct_init_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 10c9f2062c..51e3c6a1ea 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6020,8 +6020,8 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { && it.name in embed_field_names) used_embed_fields << fields_to_embed.map(it.name) default_init := ast.StructInit{ + ...struct_init typ: embed - fields: fields_to_embed } g.write('.$embed_name = ') g.struct_init(default_init) diff --git a/vlib/v/tests/multiple_embed_struct_init_test.v b/vlib/v/tests/multiple_embed_struct_init_test.v new file mode 100644 index 0000000000..f3e304191d --- /dev/null +++ b/vlib/v/tests/multiple_embed_struct_init_test.v @@ -0,0 +1,31 @@ +pub struct Boundary { +mut: + x int + y int + offset_x int + offset_y int + width int + height int +} + +pub struct WidgetBase { + Boundary +mut: + id string + z_index int + hidden bool +} + +pub struct Label { + WidgetBase +} + +fn test_multiple_embed_struct_init() { + l := Label{ + x: 100 + y: 200 + } + println(l) + assert l.x == 100 + assert l.y == 200 +}