Skip to content

Commit d694a26

Browse files
authored
checker, cgen: fix go print (#15927)
1 parent b6bbbcf commit d694a26

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

vlib/v/checker/fn.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
855855
c.fail_if_unreadable(arg.expr, arg.typ, 'argument to print')
856856
c.inside_println_arg = false
857857
node.return_type = ast.void_type
858+
c.set_node_expected_arg_types(mut node, func)
858859
/*
859860
// TODO: optimize `struct T{} fn (t &T) str() string {return 'abc'} mut a := []&T{} a << &T{} println(a[0])`
860861
// It currently generates:

vlib/v/gen/c/fn.v

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,19 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
20572057
}
20582058
call_args_str = call_args_str.replace_each(rep_group)
20592059
g.gowrappers.write_string(call_args_str)
2060+
} else if expr.name in ['print', 'println', 'eprint', 'eprintln', 'panic']
2061+
&& expr.args[0].typ != ast.string_type {
2062+
pos := g.out.len
2063+
g.gen_expr_to_string(expr.args[0].expr, expr.args[0].typ)
2064+
mut call_args_str := g.out.after(pos)
2065+
g.out.go_back(call_args_str.len)
2066+
mut rep_group := []string{cap: 2 * expr.args.len}
2067+
for i in 0 .. expr.args.len {
2068+
rep_group << g.expr_string(expr.args[i].expr)
2069+
rep_group << 'arg->arg${i + 1}'
2070+
}
2071+
call_args_str = call_args_str.replace_each(rep_group)
2072+
g.gowrappers.write_string(call_args_str)
20602073
} else {
20612074
for i in 0 .. expr.args.len {
20622075
expected_nr_muls := expr.expected_arg_types[i].nr_muls()

vlib/v/tests/inout/go_print.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1, 2, 3]

vlib/v/tests/inout/go_print.vv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main(){
2+
g := go print([1, 2, 3])
3+
g.wait()
4+
}

0 commit comments

Comments
 (0)