cgen: fix struct init with multi nested embed update expr (#13529)

pull/13545/head
yuyi 2022-02-20 02:46:44 +08:00 committed by GitHub
parent dbae2d6af4
commit 75ebac006d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 7 deletions

View File

@ -230,13 +230,19 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
g.write('.') g.write('.')
} }
if node.is_update_embed { if node.is_update_embed {
embed_sym := g.table.sym(node.typ) update_sym := g.table.sym(node.update_expr_type)
embed_name := embed_sym.embed_name() _, embeds := g.table.find_field_from_embeds(update_sym, field.name) or {
g.write(embed_name) ast.StructField{}, []ast.Type{}
if node.typ.is_ptr() { }
g.write('->') for embed in embeds {
} else { esym := g.table.sym(embed)
g.write('.') ename := esym.embed_name()
g.write(ename)
if embed.is_ptr() {
g.write('->')
} else {
g.write('.')
}
} }
} }
g.write(field.name) g.write(field.name)

View File

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