Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 24, 2024
1 parent 61097a0 commit f432f66
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
13 changes: 10 additions & 3 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -4208,6 +4208,8 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
mut sum_type_dot := '.'
mut field_typ := ast.void_type
mut is_option_unwrap := false
mut is_dereferenced := node.expr is ast.SelectorExpr && node.expr.expr_type.is_ptr()
&& g.table.final_sym(node.expr.expr_type).kind in [.interface, .sum_type]
if f := g.table.find_field_with_embeds(sym, node.field_name) {
field_sym := g.table.sym(f.typ)
field_typ = f.typ
Expand Down Expand Up @@ -4374,8 +4376,8 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
}
alias_to_ptr := sym.info is ast.Alias && sym.info.parent_type.is_ptr()
if field_is_opt
|| ((g.unwrap_generic(node.expr_type).is_ptr() || sym.kind == .chan || alias_to_ptr)
&& node.from_embed_types.len == 0)
|| (((!is_dereferenced && g.unwrap_generic(node.expr_type).is_ptr())
|| sym.kind == .chan || alias_to_ptr) && node.from_embed_types.len == 0)
|| (node.expr.is_as_cast() && g.inside_smartcast) {
g.write('->')
} else {
Expand All @@ -4389,7 +4391,12 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
}
g.write(field_name)
if is_option_unwrap {
g.write('.data))')
if field_typ.is_ptr() {
g.write('->')
} else {
g.write('.')
}
g.write('data))')
}
if sum_type_deref_field != '' {
g.write('${sum_type_dot}${sum_type_deref_field})')
Expand Down
35 changes: 35 additions & 0 deletions vlib/v/tests/options/option_selector_interface_ptr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module main

interface IGameObject {
mut:
name string
parent ?&IGameObject
children []&IGameObject
add_child(mut o IGameObject)
}

@[heap]
struct GameObject implements IGameObject {
mut:
name string
parent ?&IGameObject
children []&IGameObject
}

fn (mut gameobject GameObject) add_child(mut o IGameObject) {
o.parent = gameobject
gameobject.children << o
}

fn test_main() {
mut v1 := &GameObject{
name: 'v1'
}
mut v2 := &GameObject{
name: 'v2'
}
v1.add_child(mut v2)
if v1.children[0].parent != none {
assert '${v1.children[0].parent.name}' == 'v1'
}
}

0 comments on commit f432f66

Please sign in to comment.