mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix calling function-pointer fields on interfaces (#9948)
This commit is contained in:
parent
4348c2322d
commit
ef63491a8c
@ -757,7 +757,13 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
||||
// TODO: test node.left instead
|
||||
// left & left_type will be `x` and `x type` in `x.fieldfn()`
|
||||
// will be `0` for `foo()`
|
||||
mut is_interface_call := false
|
||||
if node.left_type != 0 {
|
||||
left_sym := g.table.get_type_symbol(node.left_type)
|
||||
if left_sym.kind == .interface_ {
|
||||
is_interface_call = true
|
||||
g.write('(*')
|
||||
}
|
||||
g.expr(node.left)
|
||||
if node.left_type.is_ptr() {
|
||||
g.write('->')
|
||||
@ -889,8 +895,12 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
||||
// g.writeln(';')
|
||||
// g.write(cur_line + ' /* <== af cur line*/')
|
||||
// }
|
||||
g.write(g.get_ternary_name(name))
|
||||
if is_interface_call {
|
||||
g.write(')')
|
||||
}
|
||||
mut tmp_cnt_save := -1
|
||||
g.write('${g.get_ternary_name(name)}(')
|
||||
g.write('(')
|
||||
if g.is_json_fn {
|
||||
g.write(json_obj)
|
||||
} else {
|
||||
|
@ -54,3 +54,21 @@ fn test_interface_fields() {
|
||||
assert c.breed == 'what??'
|
||||
assert d.breed == 'what??'
|
||||
}
|
||||
|
||||
struct Nofun {
|
||||
foo fn (int) int
|
||||
}
|
||||
|
||||
interface NofunInterface {
|
||||
foo fn (int) int
|
||||
}
|
||||
|
||||
fn my_fn(a int) int {
|
||||
assert a == 123
|
||||
return a * 2
|
||||
}
|
||||
|
||||
fn test_interface_fn_pointer_fields() {
|
||||
nf := NofunInterface(Nofun{my_fn})
|
||||
assert nf.foo(123) == 246
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user