cgen: fix sumtype aggregate with struct embeded (fix #22481) (#22532)

This commit is contained in:
yuyi 2024-10-15 23:29:02 +08:00 committed by GitHub
parent 78bdeca9b1
commit e6d4fac5dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 0 deletions

View File

@ -4161,6 +4161,11 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
} else {
g.write_selector_expr_embed_name(node, node.from_embed_types)
}
} else if sym.info is ast.Aggregate {
agg_sym := g.table.sym(sym.info.types[g.aggregate_type_idx])
if !g.table.struct_has_field(agg_sym, field_name) {
g.write_selector_expr_embed_name(node, node.from_embed_types)
}
} else {
g.write_selector_expr_embed_name(node, node.from_embed_types)
}

View File

@ -0,0 +1,24 @@
type SumType = StructA | StructB
struct StructA {
member string
}
struct StructB {
StructA
}
fn get_member(st SumType) string {
match st {
StructA, StructB {
return st.member
}
}
}
fn test_aggregate_with_struct_embed() {
struct_a := StructA{'hello'}
ret := get_member(struct_a)
println(ret)
assert ret == 'hello'
}