Skip to content

Commit ccb93e6

Browse files
authored
cgen: fix codegen for selector with embed field option (fix #24084) (#24085)
1 parent 21e6c46 commit ccb93e6

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7579,6 +7579,12 @@ fn (mut g Gen) as_cast(node ast.AsCast) {
75797579
g.unwrap_option_type(node.expr.typ, '${node.expr.expr.name}.${node.expr.field_name}',
75807580
node.expr.expr.is_auto_heap())
75817581
is_optional_ident_var = true
7582+
} else if node.expr.expr is ast.SelectorExpr && node.expr.typ.has_flag(.option) {
7583+
if node.expr.expr.expr is ast.Ident {
7584+
g.unwrap_option_type(node.expr.typ, '${node.expr.expr.expr.name}.${node.expr.expr.field_name}.${node.expr.field_name}',
7585+
node.expr.expr.expr.is_auto_heap())
7586+
is_optional_ident_var = true
7587+
}
75827588
}
75837589
}
75847590
if !is_optional_ident_var {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct Foo {
2+
optional_one ?string
3+
}
4+
5+
struct Bar {
6+
foo Foo
7+
}
8+
9+
fn test_main() {
10+
b := Bar{Foo{
11+
optional_one: 'hello world'
12+
}}
13+
println(b.foo.optional_one)
14+
x := b.foo.optional_one as string
15+
16+
assert x == 'hello world'
17+
assert (b.foo.optional_one as string) == 'hello world'
18+
}

0 commit comments

Comments
 (0)