Skip to content

Commit 8aadba1

Browse files
authored
1 parent a577a88 commit 8aadba1

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

compiler/ccgstmts.nim

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,19 +698,21 @@ proc raiseExit(p: BProc) =
698698
lineCg(p, cpsStmts, "if (NIM_UNLIKELY(*nimErr_)) goto LA$1_;$n",
699699
[p.nestedTryStmts[^1].label])
700700

701+
proc finallyActions(p: BProc) =
702+
if p.config.exc != excGoto and p.nestedTryStmts.len > 0 and p.nestedTryStmts[^1].inExcept:
703+
# if the current try stmt have a finally block,
704+
# we must execute it before reraising
705+
let finallyBlock = p.nestedTryStmts[^1].fin
706+
if finallyBlock != nil:
707+
genSimpleBlock(p, finallyBlock[0])
708+
701709
proc genRaiseStmt(p: BProc, t: PNode) =
702-
if p.config.exc != excGoto:
703-
if p.config.exc == excCpp:
704-
discard cgsym(p.module, "popCurrentExceptionEx")
705-
if p.nestedTryStmts.len > 0 and p.nestedTryStmts[^1].inExcept:
706-
# if the current try stmt have a finally block,
707-
# we must execute it before reraising
708-
let finallyBlock = p.nestedTryStmts[^1].fin
709-
if finallyBlock != nil:
710-
genSimpleBlock(p, finallyBlock[0])
710+
if p.config.exc == excCpp:
711+
discard cgsym(p.module, "popCurrentExceptionEx")
711712
if t[0].kind != nkEmpty:
712713
var a: TLoc
713714
initLocExprSingleUse(p, t[0], a)
715+
finallyActions(p)
714716
var e = rdLoc(a)
715717
var typ = skipTypes(t[0].typ, abstractPtrs)
716718
genLineDir(p, t)
@@ -724,6 +726,7 @@ proc genRaiseStmt(p: BProc, t: PNode) =
724726
if optOwnedRefs in p.config.globalOptions:
725727
lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [e])
726728
else:
729+
finallyActions(p)
727730
genLineDir(p, t)
728731
# reraise the last exception:
729732
if p.config.exc == excCpp:

tests/destructor/tsetjmp_raise.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
discard """
2+
outputsub: "index 2 not in 0 .. 0 [IndexError]"
3+
exitcode: 1
4+
cmd: "nim c --gc:arc --exceptions:setjmp $file"
5+
"""
6+
7+
# bug #12961
8+
# --gc:arc --exceptions:setjmp
9+
let a = @[1]
10+
echo a[2]
11+

0 commit comments

Comments
 (0)