1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix error for optional multi return (#13959)

This commit is contained in:
yuyi 2022-04-07 14:12:23 +08:00 committed by GitHub
parent c9dcdf6744
commit a58dde48f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 7 deletions

View File

@ -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 {

View File

@ -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)')

View File

@ -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
}