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:
parent
b6bbbcf2e7
commit
d694a26f39
|
@ -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:
|
||||
|
|
|
@ -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()
|
||||
|
|
1
vlib/v/tests/inout/go_print.out
Normal file
1
vlib/v/tests/inout/go_print.out
Normal file
|
@ -0,0 +1 @@
|
|||
[1, 2, 3]
|
4
vlib/v/tests/inout/go_print.vv
Normal file
4
vlib/v/tests/inout/go_print.vv
Normal file
|
@ -0,0 +1,4 @@
|
|||
fn main(){
|
||||
g := go print([1, 2, 3])
|
||||
g.wait()
|
||||
}
|
Loading…
Reference in New Issue
Block a user