Skip to content

Commit

Permalink
cgen: fix sumtype aggregate with struct embeded (fix #22481) (#22532)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Oct 15, 2024
1 parent 78bdeca commit e6d4fac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 24 additions & 0 deletions vlib/v/tests/sumtypes/aggregate_with_struct_embed_test.v
Original file line number Diff line number Diff line change
@@ -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'
}

0 comments on commit e6d4fac

Please sign in to comment.