mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix struct fn ptr call (#18260)
This commit is contained in:
parent
dc16e50d55
commit
bc88183318
@ -1478,8 +1478,14 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
|||||||
mut is_interface_call := false
|
mut is_interface_call := false
|
||||||
mut is_selector_call := false
|
mut is_selector_call := false
|
||||||
if node.left_type != 0 {
|
if node.left_type != 0 {
|
||||||
|
mut fn_typ := ast.Type(0)
|
||||||
left_sym := g.table.sym(node.left_type)
|
left_sym := g.table.sym(node.left_type)
|
||||||
if left_sym.kind == .interface_ {
|
if node.is_field {
|
||||||
|
if field := g.table.find_field_with_embeds(left_sym, node.name) {
|
||||||
|
fn_typ = field.typ
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if left_sym.kind == .interface_ || fn_typ.is_ptr() {
|
||||||
is_interface_call = true
|
is_interface_call = true
|
||||||
g.write('(*')
|
g.write('(*')
|
||||||
}
|
}
|
||||||
|
22
vlib/v/tests/fn_ptr_call_test.v
Normal file
22
vlib/v/tests/fn_ptr_call_test.v
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
struct Animal {
|
||||||
|
mut:
|
||||||
|
age fn (p int) int
|
||||||
|
duck Duck
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Duck {
|
||||||
|
mut:
|
||||||
|
age &fn (p int) int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
mut animal := Animal{
|
||||||
|
age: fn (x int) int {
|
||||||
|
return 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
animal.duck = Duck{
|
||||||
|
age: &animal.age
|
||||||
|
}
|
||||||
|
assert animal.duck.age(1) == 5
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user