1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix embed struct with sumtype field (#17823)

This commit is contained in:
yuyi 2023-03-30 20:59:06 +08:00 committed by GitHub
parent 214f72ba03
commit b40aa4ffa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

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

View File

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