cgen: fix `go fn(mut x Interface){ println(x) }(x)` (#10585)

pull/10602/head
yuyi 2021-06-28 21:31:11 +08:00 committed by GitHub
parent 625dc6e0ff
commit 4af4f2b5a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 4 deletions

View File

@ -5942,10 +5942,34 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
g.gowrappers.write_string(', ')
}
}
for i in 0 .. expr.args.len {
g.gowrappers.write_string('arg->arg${i + 1}')
if i < expr.args.len - 1 {
g.gowrappers.write_string(', ')
if expr.args.len > 0 {
mut has_cast := false
for i in 0 .. expr.args.len {
if g.table.get_type_symbol(expr.expected_arg_types[i]).kind == .interface_
&& g.table.get_type_symbol(expr.args[i].typ).kind != .interface_ {
has_cast = true
break
}
}
if has_cast {
pos := g.out.len
g.call_args(expr)
mut call_args_str := g.out.after(pos)
g.out.go_back(call_args_str.len)
mut rep_group := []string{cap: 2 * expr.args.len}
for i in 0 .. expr.args.len {
rep_group << g.expr_string(expr.args[i].expr)
rep_group << 'arg->arg${i + 1}'
}
call_args_str = call_args_str.replace_each(rep_group)
g.gowrappers.write_string(call_args_str)
} else {
for i in 0 .. expr.args.len {
g.gowrappers.write_string('arg->arg${i + 1}')
if i != expr.args.len - 1 {
g.gowrappers.write_string(', ')
}
}
}
}
g.gowrappers.writeln(');')

View File

@ -0,0 +1,17 @@
struct St1 {
val int = 5
}
interface In1 {
val int
}
fn test_go_wait_with_fn_of_interface_type() {
mut var := &St1{}
(go fn1(mut var)).wait()
}
fn fn1(mut v In1) {
println(v)
assert v.val == 5
}