1
0
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:
Felipe Pena 2023-05-25 04:52:05 -03:00 committed by GitHub
parent dc16e50d55
commit bc88183318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -1478,8 +1478,14 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
mut is_interface_call := false
mut is_selector_call := false
if node.left_type != 0 {
mut fn_typ := ast.Type(0)
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
g.write('(*')
}

View 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
}