fix defer before returns

pull/1260/head
Alexander Medvednikov 2019-07-21 00:49:34 +02:00
parent 1b4656a4dc
commit 3245377683
2 changed files with 25 additions and 2 deletions

View File

@ -3206,7 +3206,6 @@ else {
} }
fn (p mut Parser) return_st() { fn (p mut Parser) return_st() {
p.cgen.insert_before(p.cur_fn.defer_text)
p.check(.key_return) p.check(.key_return)
p.fgen(' ') p.fgen(' ')
fn_returns := p.cur_fn.typ != 'void' fn_returns := p.cur_fn.typ != 'void'
@ -3223,11 +3222,20 @@ fn (p mut Parser) return_st() {
ret := p.cgen.cur_line.right(ph) ret := p.cgen.cur_line.right(ph)
p.cgen.resetln('$expr_type $tmp = ($expr_type)($ret);') p.cgen.resetln('$expr_type $tmp = ($expr_type)($ret);')
p.cgen(p.cur_fn.defer_text)
p.gen('return opt_ok(&$tmp, sizeof($expr_type))') p.gen('return opt_ok(&$tmp, sizeof($expr_type))')
} }
else { else {
ret := p.cgen.cur_line.right(ph) ret := p.cgen.cur_line.right(ph)
p.cgen.resetln('return $ret') p.cgen(p.cur_fn.defer_text)
if expr_type == 'void*' {
p.cgen.resetln('return $ret')
} else {
tmp := p.get_tmp()
p.cgen.resetln('$expr_type $tmp = $ret;')
p.genln(p.cur_fn.defer_text)
p.genln('return $tmp;')
}
} }
p.check_types(expr_type, p.cur_fn.typ) p.check_types(expr_type, p.cur_fn.typ)
} }

View File

@ -0,0 +1,15 @@
fn foo() string {
println('foo()')
return 'foo'
}
fn foo2() string {
println('start')
defer { println('defer') }
println('end')
return foo()
}
fn test_defer() {
assert foo2() == 'foo'
}