cgen: fix sumtype with embedded struct of option field (fix #22984) (#22996)

This commit is contained in:
yuyi 2024-11-28 16:51:00 +08:00 committed by GitHub
parent 5e78eef08a
commit 4a7f8cc139
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View File

@ -6963,7 +6963,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
if sym.info.fields.len > 0 {
g.writeln('\t// pointers to common sumtype fields')
for field in sym.info.fields {
g.type_definitions.writeln('\t${g.styp(field.typ.ref())} ${c_name(field.name)};')
g.type_definitions.writeln('\t${g.styp(field.typ)}* ${c_name(field.name)};')
}
}
g.type_definitions.writeln('};')

View File

@ -0,0 +1,30 @@
struct Value {
x int
}
struct BValue {
v ?Value
}
struct Word {
BValue
}
struct Long {
BValue
}
struct Variadic {
BValue
}
type Param = Word | Long | Variadic
fn test_sumtype_with_embedded_struct_of_option_field() {
a := [Param(Word{}), Long{}, Variadic{}]
dump(a)
f := a[0]
v := f.v
dump(v)
assert v == none
}