mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fix defer before returns
This commit is contained in:
parent
1b4656a4dc
commit
3245377683
@ -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(p.cur_fn.defer_text)
|
||||||
|
if expr_type == 'void*' {
|
||||||
p.cgen.resetln('return $ret')
|
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)
|
||||||
}
|
}
|
||||||
|
15
compiler/tests/defer_test.v
Normal file
15
compiler/tests/defer_test.v
Normal 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'
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user