From a58dde48f89792d2367037f9f7fadce6e68319af Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 7 Apr 2022 14:12:23 +0800 Subject: [PATCH] cgen: fix error for optional multi return (#13959) --- vlib/v/gen/c/assign.v | 10 +++++----- vlib/v/gen/c/fn.v | 2 -- vlib/v/tests/optional_multi_return_test.v | 10 ++++++++++ 3 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 vlib/v/tests/optional_multi_return_test.v diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index c71c164f1d..0f5db0083a 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -471,7 +471,7 @@ fn (mut g Gen) gen_multi_return_assign(node &ast.AssignStmt, return_type ast.Typ // TODO Handle in if_expr is_opt := return_type.has_flag(.optional) mr_var_name := 'mr_$node.pos.pos' - mr_styp := g.typ(return_type) + mr_styp := g.typ(return_type.clear_flag(.optional)) g.write('$mr_styp $mr_var_name = ') g.expr(node.right[0]) g.writeln(';') @@ -505,9 +505,9 @@ fn (mut g Gen) gen_multi_return_assign(node &ast.AssignStmt, return_type ast.Typ if is_opt { mr_base_styp := g.base_type(return_type) if is_auto_heap { - g.writeln('HEAP${noscan}($mr_base_styp, *($mr_base_styp*)${mr_var_name}.data).arg$i) });') + g.writeln('HEAP${noscan}($mr_base_styp, ${mr_var_name}.arg$i) });') } else { - g.writeln('(*($mr_base_styp*)${mr_var_name}.data).arg$i });') + g.writeln('${mr_var_name}.arg$i });') } } else { if is_auto_heap { @@ -520,9 +520,9 @@ fn (mut g Gen) gen_multi_return_assign(node &ast.AssignStmt, return_type ast.Typ if is_opt { mr_base_styp := g.base_type(return_type) if is_auto_heap { - g.writeln(' = HEAP${noscan}($mr_base_styp, *($mr_base_styp*)${mr_var_name}.data).arg$i);') + g.writeln(' = HEAP${noscan}($mr_base_styp, ${mr_var_name}.arg$i);') } else { - g.writeln(' = (*($mr_base_styp*)${mr_var_name}.data).arg$i;') + g.writeln(' = ${mr_var_name}.arg$i;') } } else { if is_auto_heap { diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index a70a25ab21..876cb53eb1 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -695,8 +695,6 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { unwrapped_styp := g.typ(unwrapped_typ) if unwrapped_typ == ast.void_type { g.write('\n $cur_line') - } else if g.table.sym(node.return_type).kind == .multi_return { - g.write('\n $cur_line $tmp_opt /*U*/') } else { if !g.inside_const_optional { g.write('\n $cur_line (*($unwrapped_styp*)${tmp_opt}.data)') diff --git a/vlib/v/tests/optional_multi_return_test.v b/vlib/v/tests/optional_multi_return_test.v new file mode 100644 index 0000000000..cff19acf24 --- /dev/null +++ b/vlib/v/tests/optional_multi_return_test.v @@ -0,0 +1,10 @@ +fn tuple() ?(int, int) { + return 1, 2 +} + +fn test_optional_multi_return() ? { + println(tuple() ?) + a, b := tuple() ? + assert a == 1 + assert b == 2 +}