cgen: fix codegen for selector with embed field option (fix #24084) (#24085)

This commit is contained in:
Felipe Pena 2025-03-30 10:31:34 -03:00 committed by GitHub
parent 21e6c46ae8
commit ccb93e677c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View File

@ -7579,6 +7579,12 @@ fn (mut g Gen) as_cast(node ast.AsCast) {
g.unwrap_option_type(node.expr.typ, '${node.expr.expr.name}.${node.expr.field_name}', g.unwrap_option_type(node.expr.typ, '${node.expr.expr.name}.${node.expr.field_name}',
node.expr.expr.is_auto_heap()) node.expr.expr.is_auto_heap())
is_optional_ident_var = true is_optional_ident_var = true
} else if node.expr.expr is ast.SelectorExpr && node.expr.typ.has_flag(.option) {
if node.expr.expr.expr is ast.Ident {
g.unwrap_option_type(node.expr.typ, '${node.expr.expr.expr.name}.${node.expr.expr.field_name}.${node.expr.field_name}',
node.expr.expr.expr.is_auto_heap())
is_optional_ident_var = true
}
} }
} }
if !is_optional_ident_var { if !is_optional_ident_var {

View File

@ -0,0 +1,18 @@
struct Foo {
optional_one ?string
}
struct Bar {
foo Foo
}
fn test_main() {
b := Bar{Foo{
optional_one: 'hello world'
}}
println(b.foo.optional_one)
x := b.foo.optional_one as string
assert x == 'hello world'
assert (b.foo.optional_one as string) == 'hello world'
}