From b40aa4ffa9d93176cf56fa3b2db7dc2678439037 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 30 Mar 2023 20:59:06 +0800 Subject: [PATCH] cgen: fix embed struct with sumtype field (#17823) --- vlib/v/gen/c/cgen.v | 2 +- .../embed_struct_with_sumtype_field_test.v | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/embed_struct_with_sumtype_field_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index fe2cc32c99..cb104ea99e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3491,7 +3491,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { } mut sum_type_deref_field := '' mut sum_type_dot := '.' - if f := g.table.find_field(sym, node.field_name) { + if f := g.table.find_field_with_embeds(sym, node.field_name) { field_sym := g.table.sym(f.typ) if field_sym.kind in [.sum_type, .interface_] { if !prevent_sum_type_unwrapping_once { diff --git a/vlib/v/tests/embed_struct_with_sumtype_field_test.v b/vlib/v/tests/embed_struct_with_sumtype_field_test.v new file mode 100644 index 0000000000..329f3c1276 --- /dev/null +++ b/vlib/v/tests/embed_struct_with_sumtype_field_test.v @@ -0,0 +1,28 @@ +type Prefix = [2]string | string + +struct FooStruct { + prefix Prefix +} + +struct BarStruct { + FooStruct +} + +fn (b BarStruct) print_prefix() string { + if b.prefix is [2]string { + eprint(b.prefix[0]) + return b.prefix[0] + } else if b.prefix is string { + eprint(b.prefix) + return b.prefix + } + return '' +} + +fn test_embed_struct_with_sumtype_field() { + b := BarStruct{ + prefix: ['abc', 'bcd']! + } + ret := b.print_prefix() + assert ret == 'abc' +}