summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2020-01-03 14:30:54 +0100
committerGitHub <noreply@github.com>2020-01-03 14:30:54 +0100
commit8aadba1a2a1759d13fd93909d100fbd0b166d11d (patch)
treebf36b39cdeaa5f9fdc56ee7963cd22ec2f372002
parenta577a88c363d5be606b0b7eab1eb56fe611927d6 (diff)
downloadNim-8aadba1a2a1759d13fd93909d100fbd0b166d11d.tar.gz
fixes #12961 (#13019)
-rw-r--r--compiler/ccgstmts.nim21
-rw-r--r--tests/destructor/tsetjmp_raise.nim11
2 files changed, 23 insertions, 9 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim
index 4c342aaf6..30444e2aa 100644
--- a/compiler/ccgstmts.nim
+++ b/compiler/ccgstmts.nim
@@ -698,19 +698,21 @@ proc raiseExit(p: BProc) =
     lineCg(p, cpsStmts, "if (NIM_UNLIKELY(*nimErr_)) goto LA$1_;$n",
       [p.nestedTryStmts[^1].label])
 
+proc finallyActions(p: BProc) =
+  if p.config.exc != excGoto and p.nestedTryStmts.len > 0 and p.nestedTryStmts[^1].inExcept:
+    # if the current try stmt have a finally block,
+    # we must execute it before reraising
+    let finallyBlock = p.nestedTryStmts[^1].fin
+    if finallyBlock != nil:
+      genSimpleBlock(p, finallyBlock[0])
+
 proc genRaiseStmt(p: BProc, t: PNode) =
-  if p.config.exc != excGoto:
-    if p.config.exc == excCpp:
-      discard cgsym(p.module, "popCurrentExceptionEx")
-    if p.nestedTryStmts.len > 0 and p.nestedTryStmts[^1].inExcept:
-      # if the current try stmt have a finally block,
-      # we must execute it before reraising
-      let finallyBlock = p.nestedTryStmts[^1].fin
-      if finallyBlock != nil:
-        genSimpleBlock(p, finallyBlock[0])
+  if p.config.exc == excCpp:
+    discard cgsym(p.module, "popCurrentExceptionEx")
   if t[0].kind != nkEmpty:
     var a: TLoc
     initLocExprSingleUse(p, t[0], a)
+    finallyActions(p)
     var e = rdLoc(a)
     var typ = skipTypes(t[0].typ, abstractPtrs)
     genLineDir(p, t)
@@ -724,6 +726,7 @@ proc genRaiseStmt(p: BProc, t: PNode) =
       if optOwnedRefs in p.config.globalOptions:
         lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [e])
   else:
+    finallyActions(p)
     genLineDir(p, t)
     # reraise the last exception:
     if p.config.exc == excCpp:
diff --git a/tests/destructor/tsetjmp_raise.nim b/tests/destructor/tsetjmp_raise.nim
new file mode 100644
index 000000000..b6078ada2
--- /dev/null
+++ b/tests/destructor/tsetjmp_raise.nim
@@ -0,0 +1,11 @@
+discard """
+  outputsub: "index 2 not in 0 .. 0 [IndexError]"
+  exitcode: 1
+  cmd: "nim c --gc:arc --exceptions:setjmp $file"
+"""
+
+# bug #12961
+# --gc:arc --exceptions:setjmp
+let a = @[1]
+echo a[2]
+