summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorheterodoxic <122719743+heterodoxic@users.noreply.github.com>2023-06-04 08:56:11 +0200
committerGitHub <noreply@github.com>2023-06-04 08:56:11 +0200
commit0d4d70f15c8c440a3b71cc3b39d677520221f3e0 (patch)
treee64e0c9a7fada14a9daa376af82164b87cbbeb71
parent929cb4d6017455b1749d2e3d7d1d903046f66163 (diff)
downloadNim-0d4d70f15c8c440a3b71cc3b39d677520221f3e0.tar.gz
shallow fix for #21972, #18552 by moving std::exception_ptr to the parent sco… (#21988)
shallow fix for #21972 by moving std::exception_ptr to the parent scope, minor cleanup
-rw-r--r--compiler/ccgstmts.nim5
-rw-r--r--tests/ccgbugs/t21972.nim33
2 files changed, 35 insertions, 3 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim
index 36ef4f3ea..09350c034 100644
--- a/compiler/ccgstmts.nim
+++ b/compiler/ccgstmts.nim
@@ -1009,7 +1009,7 @@ proc genRestoreFrameAfterException(p: BProc) =
 proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
   #[ code to generate:
 
-    std::exception_ptr error = nullptr;
+    std::exception_ptr error;
     try {
       body;
     } catch (Exception e) {
@@ -1040,7 +1040,7 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
   inc(p.labels, 2)
   let etmp = p.labels
 
-  p.procSec(cpsInit).add(ropecg(p.module, "\tstd::exception_ptr T$1_ = nullptr;$n", [etmp]))
+  lineCg(p, cpsStmts, "std::exception_ptr T$1_;$n", [etmp])
 
   let fin = if t[^1].kind == nkFinally: t[^1] else: nil
   p.nestedTryStmts.add((fin, false, 0.Natural))
@@ -1074,7 +1074,6 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
       if hasIf: lineF(p, cpsStmts, "else ", [])
       startBlock(p)
       # we handled the error:
-      linefmt(p, cpsStmts, "T$1_ = nullptr;$n", [etmp])
       expr(p, t[i][0], d)
       linefmt(p, cpsStmts, "#popCurrentException();$n", [])
       endBlock(p)
diff --git a/tests/ccgbugs/t21972.nim b/tests/ccgbugs/t21972.nim
new file mode 100644
index 000000000..58d0cfc62
--- /dev/null
+++ b/tests/ccgbugs/t21972.nim
@@ -0,0 +1,33 @@
+discard """

+    targets: "c cpp"

+    outputsub: "Error: unhandled exception: Err2 [IOError]"

+    exitcode: "1"

+"""

+

+proc bar(x: var int) =

+  inc x

+  if x == 3:

+    raise newException(ValueError, "H0")

+

+  elif x == 5:

+    raise newException(IOError, "H1")

+

+  elif x > 7:

+    raise newException(IOError, "H2")

+

+

+proc foo() =

+  var i = 0

+  while true:

+    try:

+      bar(i)

+      echo i

+

+    except ValueError:

+      debugEcho("ValueError")

+

+    except IOError:

+      raise newException(IOError, "Err2")

+

+when isMainModule:

+  foo()
\ No newline at end of file