cgen: fix multiple return with sumtype (#12930)

pull/12938/head
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;') g.writeln('return $tmpvar;')
return return
} }
// typ_sym := g.table.sym(g.fn_decl.return_type) typ_sym := g.table.sym(g.fn_decl.return_type)
// mr_info := typ_sym.info as ast.MultiReturn mr_info := typ_sym.info as ast.MultiReturn
mut styp := '' mut styp := ''
if fn_return_is_optional { if fn_return_is_optional {
g.writeln('$ret_typ $tmpvar;') g.writeln('$ret_typ $tmpvar;')
@ -4826,7 +4826,11 @@ fn (mut g Gen) return_stmt(node ast.Return) {
if expr.is_auto_deref_var() { if expr.is_auto_deref_var() {
g.write('*') 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) g.expr(expr)
}
arg_idx++ arg_idx++
if i < node.exprs.len - 1 { if i < node.exprs.len - 1 {
g.write(', ') 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'
}