Skip to content

Commit

Permalink
removing warnings related to BareExcept codegen (#288)
Browse files Browse the repository at this point in the history
* cosmetic

* rm spam

* add a couple CatchableError

* the problem child

* missed one

* discretion is the better part of valor
  • Loading branch information
disruptek authored Feb 8, 2023
1 parent 46f25d0 commit 7ffdf5c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
4 changes: 3 additions & 1 deletion cps/rewrites.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 4 additions & 5 deletions cps/transform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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")),
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/preamble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.}
Expand Down
2 changes: 1 addition & 1 deletion tests/texprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 13 additions & 13 deletions tests/ttry.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ suite "try statements":
try:
noop()
inc r
except:
except CatchableError:
fail "this branch should not run"
inc r

Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7ffdf5c

Please sign in to comment.