Fix exception tracking in try blocks (#10455)
Exceptions raised inside a nkFinally/nkExcept block are not caught by the block itself. Fixes #3886
This commit is contained in:
parent
a0623a2356
commit
3ce6b2acb9
2 changed files with 23 additions and 1 deletions
|
|
@ -353,6 +353,8 @@ proc trackTryStmt(tracked: PEffects, n: PNode) =
|
||||||
|
|
||||||
var branches = 1
|
var branches = 1
|
||||||
var hasFinally = false
|
var hasFinally = false
|
||||||
|
|
||||||
|
# Collect the exceptions caught by the except branches
|
||||||
for i in 1 ..< n.len:
|
for i in 1 ..< n.len:
|
||||||
let b = n.sons[i]
|
let b = n.sons[i]
|
||||||
let blen = sonsLen(b)
|
let blen = sonsLen(b)
|
||||||
|
|
@ -368,12 +370,18 @@ proc trackTryStmt(tracked: PEffects, n: PNode) =
|
||||||
else:
|
else:
|
||||||
assert(b.sons[j].kind == nkType)
|
assert(b.sons[j].kind == nkType)
|
||||||
catches(tracked, b.sons[j].typ)
|
catches(tracked, b.sons[j].typ)
|
||||||
|
else:
|
||||||
|
assert b.kind == nkFinally
|
||||||
|
# Add any other exception raised in the except bodies
|
||||||
|
for i in 1 ..< n.len:
|
||||||
|
let b = n.sons[i]
|
||||||
|
let blen = sonsLen(b)
|
||||||
|
if b.kind == nkExceptBranch:
|
||||||
setLen(tracked.init, oldState)
|
setLen(tracked.init, oldState)
|
||||||
track(tracked, b.sons[blen-1])
|
track(tracked, b.sons[blen-1])
|
||||||
for i in oldState..<tracked.init.len:
|
for i in oldState..<tracked.init.len:
|
||||||
addToIntersection(inter, tracked.init[i])
|
addToIntersection(inter, tracked.init[i])
|
||||||
else:
|
else:
|
||||||
assert b.kind == nkFinally
|
|
||||||
setLen(tracked.init, oldState)
|
setLen(tracked.init, oldState)
|
||||||
track(tracked, b.sons[blen-1])
|
track(tracked, b.sons[blen-1])
|
||||||
hasFinally = true
|
hasFinally = true
|
||||||
|
|
|
||||||
14
tests/effects/teffects7.nim
Normal file
14
tests/effects/teffects7.nim
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
discard """
|
||||||
|
errormsg: "can raise an unlisted exception: ref FloatingPointError"
|
||||||
|
line: 10
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc foo() {.raises: [].} =
|
||||||
|
try:
|
||||||
|
discard
|
||||||
|
except KeyError:
|
||||||
|
raise newException(FloatingPointError, "foo")
|
||||||
|
except Exception:
|
||||||
|
discard
|
||||||
|
|
||||||
|
foo()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue