From a0b3379d4ad85b00b6f79da18c1e16ef608dfc26 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 8 Mar 2023 16:53:23 -0300 Subject: [PATCH] cgen: fix fn option-only return print (#17547) --- vlib/v/gen/c/str.v | 2 +- vlib/v/tests/option_void_fn_return_test.v | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/option_void_fn_return_test.v diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index f09a82edf3..eefdd5500c 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -85,7 +85,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { } else if typ == ast.bool_type { g.expr(expr) g.write(' ? _SLIT("true") : _SLIT("false")') - } else if sym.kind == .none_ { + } else if sym.kind == .none_ || typ == ast.void_type.set_flag(.option) { g.write('_SLIT("")') } else if sym.kind == .enum_ { if expr !is ast.EnumVal { diff --git a/vlib/v/tests/option_void_fn_return_test.v b/vlib/v/tests/option_void_fn_return_test.v new file mode 100644 index 0000000000..5c354ac5e3 --- /dev/null +++ b/vlib/v/tests/option_void_fn_return_test.v @@ -0,0 +1,8 @@ +fn foo() ? { + return none +} + +fn test_main() { + println(foo()) // + assert true +}