diff --git a/cps/rewrites.nim b/cps/rewrites.nim index e5178aa6..40e72062 100644 --- a/cps/rewrites.nim +++ b/cps/rewrites.nim @@ -234,7 +234,9 @@ proc normalizingRewrites*(n: NimNode): NormNode = newStmtList: # let ex: ref T = (ref T)(getCurrentException()) nnkLetSection.newTree: - newIdentDefs(ex, refTyp, newCall(refTyp, newCall(bindSym"getCurrentException"))) + newIdentDefs ex, refTyp: + newCall refTyp: + newCall bindSym"getCurrentException" # add the rewritten body result.last.add: diff --git a/cps/transform.nim b/cps/transform.nim index 15dfa676..d97d7a81 100644 --- a/cps/transform.nim +++ b/cps/transform.nim @@ -390,6 +390,7 @@ macro cpsWithException(cont, ex, n: typed): untyped = result.body.NormNode.withException(nextCont, ex), # On an exception not handled by the body. nnkExceptBranch.newTree( + bindSym"CatchableError", newStmtList( # Set `ex` to nil. newAssignment(ex, newNilLit()), @@ -432,7 +433,7 @@ macro cpsTryExcept(cont, contType: typed; name: static[string]; # add an except branch to invoke the handler newTry.add: - nnkExceptBranch.newTree: + nnkExceptBranch.newTree bindSym"CatchableError": newStmtList [ # capture the exception to ex newAssignment(ex, newCall(bindName"getCurrentException")), @@ -593,16 +594,14 @@ macro cpsTryFinally(cont, contType: typed; name: static[string]; tryTemplate.add placeholder # Add an except branch to jump to our finally - tryTemplate.add( - nnkExceptBranch.newTree( + tryTemplate.add: + nnkExceptBranch.newTree bindSym"CatchableError": newStmtList( # Stash the exception newAssignment(ex, newCall(bindName"getCurrentException")), # Then jump to reraise cont.tailCall(contType, reraise.name) ) - ) - ) # Wrap the body with this template and we are done it.add body.wrapContinuationWith(cont, placeholder, tryTemplate) diff --git a/tests/preamble.nim b/tests/preamble.nim index cf90db52..1f612ebe 100644 --- a/tests/preamble.nim +++ b/tests/preamble.nim @@ -19,8 +19,9 @@ proc trampoline[T: Continuation](c: sink T) {.used.} = var y = c.fn var x = y(c) c = x - except: - writeStackFrames c + except CatchableError: + if not c.dismissed: + writeStackFrames c raise # the current exception should not change check getCurrentException() == exception @@ -32,7 +33,6 @@ proc trampoline[T: Continuation](c: sink T) {.used.} = "continuations test best when they, uh, bounce" proc noop*(c: Cont): Cont {.cpsMagic.} = c -proc dismiss*(c: Cont): Cont {.cpsMagic.} = nil # We have a lot of these for the purpose of control-flow validation {.warning[UnreachableCode]: off.} diff --git a/tests/texprs.nim b/tests/texprs.nim index 45b6cc79..d4bba0ca 100644 --- a/tests/texprs.nim +++ b/tests/texprs.nim @@ -115,7 +115,7 @@ suite "expression flattening": check e.msg == "something" step 2 42 - except: + except CatchableError: fail "this branch should not run" -1 finally: diff --git a/tests/ttry.nim b/tests/ttry.nim index 437db542..78e950b7 100644 --- a/tests/ttry.nim +++ b/tests/ttry.nim @@ -17,7 +17,7 @@ suite "try statements": try: noop() inc r - except: + except CatchableError: fail "this branch should not run" inc r @@ -34,7 +34,7 @@ suite "try statements": inc r raise newException(CatchableError, "test") fail "statement run after raise" - except: + except CatchableError: check getCurrentExceptionMsg() == "test" inc r inc r @@ -52,7 +52,7 @@ suite "try statements": inc r raise newException(CatchableError, "test") fail "statement run after raise" - except: + except CatchableError: inc r noop() check getCurrentExceptionMsg() == "test" @@ -71,7 +71,7 @@ suite "try statements": inc r raise newException(CatchableError, "test") fail "statement run after raise" - except: + except CatchableError: inc r noop() check getCurrentExceptionMsg() == "test" @@ -161,7 +161,7 @@ suite "try statements": inc r raise newException(CatchableError, "") fail "statement run after raise" - except: + except CatchableError: inc r finally: inc r @@ -219,7 +219,7 @@ suite "try statements": inc r raise newException(CatchableError, "test") fail "statement run after raise" - except: + except CatchableError: check getCurrentExceptionMsg() == "test" inc r @@ -228,7 +228,7 @@ suite "try statements": inc r raise newException(CatchableError, "test 2") fail "statement run after raise" - except: + except CatchableError: check getCurrentExceptionMsg() == "test 2" inc r @@ -379,7 +379,7 @@ suite "try statements": proc foo() {.cps: Cont.} = try: noop() - except: + except CatchableError: fail"this except branch should not run" inc r @@ -427,7 +427,7 @@ suite "try statements": var x = 0 try: x = bar() - except: + except CatchableError: fail "This branch should not be executed" step 2 @@ -516,7 +516,7 @@ suite "try statements": inc r try: raise newException(CatchableError, "test") - except: + except CatchableError: let frames = renderStackFrames() check frames.len > 0, "expected at least one stack trace record" check "ttry.nim" in frames[0], "couldn't find ttry.nim in the trace" @@ -624,7 +624,7 @@ when defined(gcArc) or defined(gcOrc): try: noop() step 2 - except: + except CatchableError: fail "this branch should not run" step 3 @@ -640,7 +640,7 @@ when defined(gcArc) or defined(gcOrc): step 2 raise newException(CatchableError, "") fail "statement run after raise" - except: + except CatchableError: step 3 step 4 @@ -670,7 +670,7 @@ when defined(gcArc) or defined(gcOrc): step 2 raise newException(CatchableError, "") fail "statement run after raise" - except: + except CatchableError: step 3 finally: step 4