Skip to content

Commit 305a272

Browse files
authored
cgen: fix unwrapping option interface field (fix #23540) (#23541)
1 parent ea5f25e commit 305a272

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4013,6 +4013,9 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
40134013
if i == 0 && (is_option_unwrap || nested_unwrap) {
40144014
deref := if g.inside_selector {
40154015
'*'.repeat(field.smartcasts.last().nr_muls() + 1)
4016+
} else if sym.kind == .interface && !typ.is_ptr()
4017+
&& field.orig_type.has_flag(.option) {
4018+
''
40164019
} else {
40174020
'*'
40184021
}
@@ -4196,7 +4199,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
41964199
}
41974200
g.write(field_name)
41984201
if is_option_unwrap {
4199-
if field_typ.is_ptr() && g.table.final_sym(node.expr_type).kind in [.sum_type, .interface] {
4202+
if g.table.final_sym(node.expr_type).kind in [.sum_type, .interface] {
42004203
g.write('->')
42014204
} else {
42024205
g.write('.')
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
interface IBar {
2+
opt ?string
3+
}
4+
5+
struct Bar implements IBar {
6+
opt ?string
7+
}
8+
9+
struct Foo {
10+
field IBar
11+
}
12+
13+
fn (f &Foo) t() {
14+
if f.field.opt != none {
15+
assert f.field.opt == 'foo'
16+
}
17+
}
18+
19+
fn test_main() {
20+
a := Foo{
21+
field: Bar{
22+
opt: 'foo'
23+
}
24+
}
25+
if a.field.opt != none {
26+
assert a.field.opt == 'foo'
27+
}
28+
a.t()
29+
}

0 commit comments

Comments
 (0)