Skip to content

Commit

Permalink
cgen: fix option unwrap for fields of interface type (fixes #22930) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pisaiah authored Nov 21, 2024
1 parent c2d96c3 commit e384e74
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -4059,7 +4059,8 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write('*')
}
cast_sym := g.table.sym(g.unwrap_generic(typ))
if field_sym.kind == .interface && cast_sym.kind == .interface {
if field_sym.kind == .interface && cast_sym.kind == .interface
&& !is_option_unwrap {
ptr := '*'.repeat(field.typ.nr_muls())
dot := if node.expr_type.is_ptr() { '->' } else { '.' }
g.write('I_${field_sym.cname}_as_I_${cast_sym.cname}(${ptr}${node.expr}${dot}${node.field_name}))')
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/tests/options/option_selector_unwrap_types_test.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
type SumType = int | string

interface Interface {
a int
}

struct Struct {
a int
}
Expand All @@ -9,6 +13,7 @@ struct Foo {
b ?string
c ?SumType
d ?Struct
e ?Interface
}

fn test_main() {
Expand All @@ -19,6 +24,9 @@ fn test_main() {
d: Struct{
a: 123
}
e: Struct{
a: 456
}
}
if w.a != none {
dump(w.a)
Expand All @@ -44,4 +52,10 @@ fn test_main() {
} else {
assert false
}
if w.e != none {
dump(w.e)
assert w.e.a == 456
} else {
assert false
}
}

0 comments on commit e384e74

Please sign in to comment.