1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix go call interface method (fix #10520) (#10602)

This commit is contained in:
yuyi 2021-06-29 16:16:15 +08:00 committed by GitHub
parent 2bcc733ff6
commit 04221475d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 2 deletions

View File

@ -5935,12 +5935,29 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
} else {
g.gowrappers.write_string('\t')
}
g.gowrappers.write_string('${name}(')
if expr.is_method {
g.gowrappers.write_string('arg->arg0')
unwrapped_rec_type := g.unwrap_generic(expr.receiver_type)
typ_sym := g.table.get_type_symbol(unwrapped_rec_type)
if typ_sym.kind == .interface_
&& (typ_sym.info as ast.Interface).defines_method(expr.name) {
rec_cc_type := g.cc_type(unwrapped_rec_type, false)
receiver_type_name := util.no_dots(rec_cc_type)
g.gowrappers.write_string('${c_name(receiver_type_name)}_name_table[')
g.gowrappers.write_string('arg->arg0')
dot := if expr.left_type.is_ptr() { '->' } else { '.' }
mname := c_name(expr.name)
g.gowrappers.write_string('${dot}_typ]._method_${mname}(')
g.gowrappers.write_string('arg->arg0')
g.gowrappers.write_string('${dot}_object')
} else {
g.gowrappers.write_string('${name}(')
g.gowrappers.write_string('arg->arg0')
}
if expr.args.len > 0 {
g.gowrappers.write_string(', ')
}
} else {
g.gowrappers.write_string('${name}(')
}
if expr.args.len > 0 {
mut has_cast := false

View File

@ -0,0 +1,28 @@
interface CanPerformTask {
task()
}
struct Task1 {}
fn (task1 Task1) task() {
println('task1')
}
struct Task2 {}
fn (task2 Task2) task() {
println('task2')
}
fn test_go_call_interface_method() {
mut tasks := []CanPerformTask{}
tasks << Task1{}
tasks << Task2{}
for task in tasks {
go task.task()
}
assert true
}