From a69b0b7224cbf5cc09b139ff21843a3b4d83ad61 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 6 Apr 2025 22:19:40 -0300 Subject: [PATCH] cgen: fix multi return with option type (#24144) --- vlib/v/gen/c/cgen.v | 9 ++++----- vlib/v/gen/c/comptime.v | 15 +++++++++++++++ .../tests/options/option_multi_return_opt_test.v | 10 ++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 vlib/v/tests/options/option_multi_return_opt_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 7789d373a..ff18c04f2 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5517,10 +5517,9 @@ fn (mut g Gen) concat_expr(node ast.ConcatExpr) { g.write('(${styp}){') for i, expr in node.vals { g.write('.arg${i}=') - if types[i].has_flag(.option) && expr.is_literal() { - g.write('{.data=') - g.expr(expr) - g.write('}') + expr_typ := g.get_expr_type(expr) + if expr_typ != ast.void_type && types[i].has_flag(.option) { + g.expr_with_opt(expr, expr_typ, types[i]) } else { old_left_is_opt := g.left_is_opt g.left_is_opt = true @@ -5942,7 +5941,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { multi_unpack += g.go_before_last_stmt() g.write(line) expr_styp := g.base_type(call_expr.return_type) - tmp = ('(*(${expr_styp}*)${tmp}.data)') + tmp = '(*(${expr_styp}*)${tmp}.data)' } expr_types := expr_sym.mr_info().types for j, _ in expr_types { diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 9fdc13636..188c4fd5c 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -472,6 +472,21 @@ fn (mut g Gen) get_expr_type(cond ast.Expr) ast.Type { } } } + ast.IntegerLiteral { + return ast.int_type + } + ast.BoolLiteral { + return ast.bool_type + } + ast.StringLiteral { + return ast.string_type + } + ast.CharLiteral { + return ast.char_type + } + ast.FloatLiteral { + return ast.f64_type + } else { return ast.void_type } diff --git a/vlib/v/tests/options/option_multi_return_opt_test.v b/vlib/v/tests/options/option_multi_return_opt_test.v new file mode 100644 index 000000000..9d3d3e564 --- /dev/null +++ b/vlib/v/tests/options/option_multi_return_opt_test.v @@ -0,0 +1,10 @@ +fn fails(i int) !(int, ?int) { + return error('fails') +} + +fn test_main() { + a2, b2 := fails(2) or { 22, 22 } + c2 := b2? as int + assert b2 != none + assert a2 == c2 +} -- 2.39.5