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

checker, cgen: fix go print (#15927)

This commit is contained in:
yuyi 2022-09-30 20:50:54 +08:00 committed by GitHub
parent b6bbbcf2e7
commit d694a26f39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 0 deletions

View File

@ -855,6 +855,7 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
c.fail_if_unreadable(arg.expr, arg.typ, 'argument to print')
c.inside_println_arg = false
node.return_type = ast.void_type
c.set_node_expected_arg_types(mut node, func)
/*
// TODO: optimize `struct T{} fn (t &T) str() string {return 'abc'} mut a := []&T{} a << &T{} println(a[0])`
// It currently generates:

View File

@ -2057,6 +2057,19 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
}
call_args_str = call_args_str.replace_each(rep_group)
g.gowrappers.write_string(call_args_str)
} else if expr.name in ['print', 'println', 'eprint', 'eprintln', 'panic']
&& expr.args[0].typ != ast.string_type {
pos := g.out.len
g.gen_expr_to_string(expr.args[0].expr, expr.args[0].typ)
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 {
expected_nr_muls := expr.expected_arg_types[i].nr_muls()

View File

@ -0,0 +1 @@
[1, 2, 3]

View File

@ -0,0 +1,4 @@
fn main(){
g := go print([1, 2, 3])
g.wait()
}