cgen: fix `return if cond { x,y } else { a,b }` generation

pull/9708/head
Delyan Angelov 2021-04-13 11:31:40 +03:00
parent a1121d0eb0
commit 1ef718c1e1
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 20 additions and 0 deletions

View File

@ -4687,6 +4687,16 @@ fn (mut g Gen) return_stmt(node ast.Return) {
} }
// regular cases // regular cases
if fn_return_is_multi && node.exprs.len > 0 && !g.expr_is_multi_return_call(node.exprs[0]) { // not_optional_none { //&& !fn_return_is_optional { if fn_return_is_multi && node.exprs.len > 0 && !g.expr_is_multi_return_call(node.exprs[0]) { // not_optional_none { //&& !fn_return_is_optional {
if node.exprs.len == 1 && node.exprs[0] is ast.IfExpr {
// use a temporary for `return if cond { x,y } else { a,b }`
tmpvar := g.new_tmp_var()
tmptyp := g.typ(g.fn_decl.return_type)
g.write('$tmptyp $tmpvar = ')
g.expr(node.exprs[0])
g.writeln(';')
g.writeln('return $tmpvar;')
return
}
// typ_sym := g.table.get_type_symbol(g.fn_decl.return_type) // typ_sym := g.table.get_type_symbol(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 := ''

View File

@ -72,3 +72,13 @@ fn test_multiple_ret() {
assert res3_1 == 'replaced' assert res3_1 == 'replaced'
assert res3_2 == 'val' assert res3_2 == 'val'
} }
fn multi_values() (string, string) {
return if 1 > 0 { 'abc', 'def' } else { 'jkl', 'mno' }
}
fn test_multi_values() {
x, y := multi_values()
assert x == 'abc'
assert y == 'def'
}