From 75ebac006d4abde6e0436a2a79aa9ea447557059 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 20 Feb 2022 02:46:44 +0800 Subject: [PATCH] cgen: fix struct init with multi nested embed update expr (#13529) --- vlib/v/gen/c/struct.v | 20 ++++++++----- ...init_with_multi_nested_embed_update_test.v | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 vlib/v/tests/struct_init_with_multi_nested_embed_update_test.v diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 10bd0e09b0..6bcaec0b28 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -230,13 +230,19 @@ fn (mut g Gen) struct_init(node ast.StructInit) { g.write('.') } if node.is_update_embed { - embed_sym := g.table.sym(node.typ) - embed_name := embed_sym.embed_name() - g.write(embed_name) - if node.typ.is_ptr() { - g.write('->') - } else { - g.write('.') + update_sym := g.table.sym(node.update_expr_type) + _, embeds := g.table.find_field_from_embeds(update_sym, field.name) or { + ast.StructField{}, []ast.Type{} + } + for embed in embeds { + esym := g.table.sym(embed) + ename := esym.embed_name() + g.write(ename) + if embed.is_ptr() { + g.write('->') + } else { + g.write('.') + } } } g.write(field.name) diff --git a/vlib/v/tests/struct_init_with_multi_nested_embed_update_test.v b/vlib/v/tests/struct_init_with_multi_nested_embed_update_test.v new file mode 100644 index 0000000000..5df88e9f8c --- /dev/null +++ b/vlib/v/tests/struct_init_with_multi_nested_embed_update_test.v @@ -0,0 +1,28 @@ +module main + +struct Animal { + Duck + id int +} + +struct Duck { + RedDuck + action string +} + +struct RedDuck { + color string +} + +fn test_struct_init_with_multi_nested_embed_update() { + d := Animal{Duck{RedDuck{'red'}, 'fly'}, 1} + println(d) + + e := Animal{ + ...d + id: 2 + } + println(e) + assert d.id == 1 + assert e.id == 2 +}