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

cgen: fix multiple return with sumtype (#12930)

This commit is contained in:
yuyi 2021-12-22 18:09:08 +08:00 committed by GitHub
parent 6a4fa6096e
commit 6eb44f472a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -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('*')
}
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(', ')

View File

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