From ca55da63c3ce02f5ee058cec61c42ef32d9b412e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 1 Jun 2024 19:31:12 -0300 Subject: [PATCH] cgen: fix missing call to a function returning option, when called inside a print (fix #21616) (#21623) --- vlib/v/gen/c/str.v | 6 ++++++ vlib/v/tests/print_void_ret_test.v | 10 ++++++++++ 2 files changed, 16 insertions(+) create mode 100644 vlib/v/tests/print_void_ret_test.v diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 4483b12e92df1d..c8f0f8ca997730 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -92,6 +92,12 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { g.expr(expr) g.write(' ? _SLIT("true") : _SLIT("false")') } else if sym.kind == .none_ || typ == ast.void_type.set_flag(.option) { + if expr is ast.CallExpr { + stmt_str := g.go_before_last_stmt() + g.expr(expr) + g.writeln(';') + g.write(stmt_str) + } g.write('_SLIT("")') } else if sym.kind == .enum_ { if expr !is ast.EnumVal { diff --git a/vlib/v/tests/print_void_ret_test.v b/vlib/v/tests/print_void_ret_test.v new file mode 100644 index 00000000000000..a98ce5839cab9b --- /dev/null +++ b/vlib/v/tests/print_void_ret_test.v @@ -0,0 +1,10 @@ +fn foo(mut a &int) ? { + println('XXX') + a++ +} + +fn test_main() { + mut a := 1 + println(foo(mut &a)) + assert a == 2 +}