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 +}