cgen: always use tmp vars in return_statement()

pull/9004/head
Alexander Medvednikov 2021-02-27 20:00:19 +03:00
parent d39866d4f7
commit be4a2e17d3
3 changed files with 7 additions and 5 deletions

View File

@ -4745,7 +4745,8 @@ fn (mut g Gen) return_statement(node ast.Return) {
g.writeln('return $opt_tmp;') g.writeln('return $opt_tmp;')
return return
} }
free := g.is_autofree && !g.is_builtin_mod // node.exprs[0] is ast.CallExpr // free := g.is_autofree && !g.is_builtin_mod // node.exprs[0] is ast.CallExpr
free := !g.is_builtin_mod // node.exprs[0] is ast.CallExpr
mut tmp := '' mut tmp := ''
if free { if free {
// `return foo(a, b, c)` // `return foo(a, b, c)`
@ -4777,7 +4778,9 @@ fn (mut g Gen) return_statement(node ast.Return) {
// autofree before `return` // autofree before `return`
// set free_parent_scopes to true, since all variables defined in parent // set free_parent_scopes to true, since all variables defined in parent
// scopes need to be freed before the return // scopes need to be freed before the return
g.autofree_scope_vars(node.pos.pos - 1, node.pos.line_nr, true) if g.pref.autofree {
g.autofree_scope_vars(node.pos.pos - 1, node.pos.line_nr, true)
}
if tmp != '' { if tmp != '' {
g.write('return $tmp') g.write('return $tmp')
} }

View File

@ -421,7 +421,8 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
} else { } else {
'' ''
} }
if gen_or && g.pref.autofree && g.inside_return { // if gen_or && g.pref.autofree && g.inside_return {
if gen_or && g.inside_return {
// TODO optional return af hack (tmp_count gets increased in .return_statement()) // TODO optional return af hack (tmp_count gets increased in .return_statement())
g.tmp_count-- g.tmp_count--
} }

View File

@ -364,7 +364,6 @@ struct MultiOptionalFieldTest {
b ?int b ?int
} }
/*
fn foo() ?int { fn foo() ?int {
return 0 return 0
} }
@ -380,4 +379,3 @@ fn test_return_or() {
x := foo2() or { return } x := foo2() or { return }
assert x == 0 assert x == 0
} }
*/