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;')
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 := ''
if free {
// `return foo(a, b, c)`
@ -4777,7 +4778,9 @@ fn (mut g Gen) return_statement(node ast.Return) {
// autofree before `return`
// set free_parent_scopes to true, since all variables defined in parent
// scopes need to be freed before the return
if g.pref.autofree {
g.autofree_scope_vars(node.pos.pos - 1, node.pos.line_nr, true)
}
if tmp != '' {
g.write('return $tmp')
}

View File

@ -421,7 +421,8 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
} 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())
g.tmp_count--
}

View File

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