mirror of
https://github.com/vlang/v.git
synced 2025-09-13 09:25:45 -04:00
checker: fix $res()
used in defer {}
blocks with more statements, add a test (#22998)
This commit is contained in:
parent
153b7f00df
commit
acad4c2810
@ -2177,6 +2177,7 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
|
|||||||
c.inside_const = false
|
c.inside_const = false
|
||||||
}
|
}
|
||||||
ast.DeferStmt {
|
ast.DeferStmt {
|
||||||
|
c.inside_defer = true
|
||||||
if node.idx_in_fn < 0 && c.table.cur_fn != unsafe { nil } {
|
if node.idx_in_fn < 0 && c.table.cur_fn != unsafe { nil } {
|
||||||
node.idx_in_fn = c.table.cur_fn.defer_stmts.len
|
node.idx_in_fn = c.table.cur_fn.defer_stmts.len
|
||||||
c.table.cur_fn.defer_stmts << unsafe { &node }
|
c.table.cur_fn.defer_stmts << unsafe { &node }
|
||||||
@ -2203,7 +2204,6 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
|
|||||||
node.defer_vars[i] = id
|
node.defer_vars[i] = id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.inside_defer = true
|
|
||||||
c.stmts(mut node.stmts)
|
c.stmts(mut node.stmts)
|
||||||
c.inside_defer = false
|
c.inside_defer = false
|
||||||
}
|
}
|
||||||
|
@ -38,3 +38,19 @@ fn test_with_multi_return() {
|
|||||||
assert test.a == a
|
assert test.a == a
|
||||||
assert test.b == b
|
assert test.b == b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn f() int {
|
||||||
|
defer {
|
||||||
|
a := $res()
|
||||||
|
assert true
|
||||||
|
println('result is: ${a}')
|
||||||
|
}
|
||||||
|
if true {
|
||||||
|
return 123
|
||||||
|
}
|
||||||
|
return 42
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_res_in_defer_blocks_with_many_statements_works() {
|
||||||
|
assert f() == 123
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user