diff options
-rw-r--r-- | compiler/sigmatch.nim | 10 | ||||
-rw-r--r-- | lib/system.nim | 39 | ||||
-rw-r--r-- | tests/assert/tfailedassert.nim | 2 |
3 files changed, 23 insertions, 28 deletions
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 805266389..7e0820593 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -99,10 +99,12 @@ proc initCandidate*(ctx: PContext, c: var TCandidate, callee: PSym, c.calleeSym = callee if callee.kind in skProcKinds and calleeScope == -1: if callee.originatingModule == ctx.module: - let rootSym = if sfFromGeneric notin callee.flags: callee - else: callee.owner - c.calleeScope = 2 # rootSym.scope.depthLevel - #echo "SCOPE IS ", rootSym.scope.depthLevel + c.calleeScope = 2 + var owner = callee + while true: + owner = owner.skipGenericOwner + if owner.kind == skModule: break + inc c.calleeScope else: c.calleeScope = 1 else: diff --git a/lib/system.nim b/lib/system.nim index 52c29f757..83f071717 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -3044,13 +3044,12 @@ template currentSourcePath*: string = instantiationInfo(-1, true).filename proc raiseAssert*(msg: string) {.noinline.} = sysFatal(AssertionError, msg) -when true: - proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} = - # trick the compiler to not list ``EAssertionFailed`` when called - # by ``assert``. - type THide = proc (msg: string) {.noinline, raises: [], noSideEffect, - tags: [].} - THide(raiseAssert)(msg) +proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} = + # trick the compiler to not list ``AssertionError`` when called + # by ``assert``. + type THide = proc (msg: string) {.noinline, raises: [], noSideEffect, + tags: [].} + THide(raiseAssert)(msg) template assert*(cond: bool, msg = "") = ## Raises ``AssertionError`` with `msg` if `cond` is false. Note @@ -3115,24 +3114,18 @@ when not defined(nimhygiene): template onFailedAssert*(msg: expr, code: stmt): stmt {.dirty, immediate.} = ## Sets an assertion failure handler that will intercept any assert - ## statements following `onFailedAssert` in the current lexical scope. - ## Can be defined multiple times in a single function. + ## statements following `onFailedAssert` in the current module scope. ## ## .. code-block:: nim - ## - ## proc example(x: int): ErrorCode = - ## onFailedAssert(msg): - ## log msg - ## return E_FAIL - ## - ## assert(...) - ## - ## onFailedAssert(msg): - ## raise newException(MyError, msg) - ## - ## assert(...) - ## - template failedAssertImpl(msgIMPL: string): stmt {.dirty, immediate.} = + ## # module-wide policy to change the failed assert + ## # exception type in order to include a lineinfo + ## onFailedAssert(msg): + ## var e = new(TMyError) + ## e.msg = msg + ## e.lineinfo = instantiationInfo(-2) + ## raise e + ## + template failedAssertImpl(msgIMPL: string): stmt {.dirty.} = let msg = msgIMPL code diff --git a/tests/assert/tfailedassert.nim b/tests/assert/tfailedassert.nim index 729cdcac7..1e6764471 100644 --- a/tests/assert/tfailedassert.nim +++ b/tests/assert/tfailedassert.nim @@ -30,7 +30,7 @@ proc bar: int = # local overrides that are active only # in this proc onFailedAssert(msg): echo "WARNING: " & msg - + assert(false, "first assertion from bar") onFailedAssert(msg): |