From 6eb44f472aaf7a9966c8b8297c2d5d62946e7a52 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 22 Dec 2021 18:09:08 +0800 Subject: [PATCH] cgen: fix multiple return with sumtype (#12930) --- vlib/v/gen/c/cgen.v | 10 +++++++--- vlib/v/tests/multiret_with_sumtype_test.v | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/multiret_with_sumtype_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index f805424411..46580faa64 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4763,8 +4763,8 @@ fn (mut g Gen) return_stmt(node ast.Return) { g.writeln('return $tmpvar;') return } - // typ_sym := g.table.sym(g.fn_decl.return_type) - // mr_info := typ_sym.info as ast.MultiReturn + typ_sym := g.table.sym(g.fn_decl.return_type) + mr_info := typ_sym.info as ast.MultiReturn mut styp := '' if fn_return_is_optional { g.writeln('$ret_typ $tmpvar;') @@ -4826,7 +4826,11 @@ fn (mut g Gen) return_stmt(node ast.Return) { if expr.is_auto_deref_var() { g.write('*') } - g.expr(expr) + if g.table.sym(mr_info.types[i]).kind in [.sum_type, .interface_] { + g.expr_with_cast(expr, node.types[i], mr_info.types[i]) + } else { + g.expr(expr) + } arg_idx++ if i < node.exprs.len - 1 { g.write(', ') diff --git a/vlib/v/tests/multiret_with_sumtype_test.v b/vlib/v/tests/multiret_with_sumtype_test.v new file mode 100644 index 0000000000..91eacb6688 --- /dev/null +++ b/vlib/v/tests/multiret_with_sumtype_test.v @@ -0,0 +1,15 @@ +module main + +type Abc = int | rune | string | u32 + +fn cyz() (Abc, string) { + return 'a', 'b' +} + +fn test_multiret_with_sumtype() { + x, y := cyz() + println(x) + println(y) + assert x == Abc('a') + assert y == 'b' +}