mirror of
https://github.com/vlang/v.git
synced 2025-09-09 07:15:50 -04:00
parent
7e157caed7
commit
23c3af8b4d
@ -4012,10 +4012,11 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
|
||||
mut sum_type_dot := '.'
|
||||
mut field_typ := ast.void_type
|
||||
mut is_option_unwrap := false
|
||||
is_iface_or_sumtype := sym.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
|
||||
if sym.kind in [.interface, .sum_type] {
|
||||
if is_iface_or_sumtype {
|
||||
g.write('(*(')
|
||||
}
|
||||
is_option := field_typ.has_flag(.option)
|
||||
@ -4036,7 +4037,11 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
|
||||
for i, typ in field.smartcasts {
|
||||
if i == 0 && (is_option_unwrap || nested_unwrap) {
|
||||
deref := if g.inside_selector {
|
||||
'*'.repeat(field.smartcasts.last().nr_muls() + 1)
|
||||
if is_iface_or_sumtype {
|
||||
'*'.repeat(field.smartcasts.last().nr_muls())
|
||||
} else {
|
||||
'*'.repeat(field.smartcasts.last().nr_muls() + 1)
|
||||
}
|
||||
} else if sym.kind == .interface && !typ.is_ptr()
|
||||
&& field.orig_type.has_flag(.option) {
|
||||
''
|
||||
@ -4138,7 +4143,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if sym.kind in [.interface, .sum_type] {
|
||||
if is_iface_or_sumtype {
|
||||
g.write('(*(')
|
||||
}
|
||||
}
|
||||
|
39
vlib/v/tests/options/option_ptr_unwrap_selector_test.v
Normal file
39
vlib/v/tests/options/option_ptr_unwrap_selector_test.v
Normal file
@ -0,0 +1,39 @@
|
||||
module main
|
||||
|
||||
@[heap]
|
||||
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)
|
||||
for child in v1.children {
|
||||
if child.parent != none {
|
||||
eprintln('parent: ${child.parent.name}')
|
||||
}
|
||||
}
|
||||
assert true
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user