fixes #2500
This commit is contained in:
parent
6e6c6446a4
commit
3a9500f7b1
3 changed files with 22 additions and 27 deletions
|
|
@ -99,10 +99,12 @@ proc initCandidate*(ctx: PContext, c: var TCandidate, callee: PSym,
|
||||||
c.calleeSym = callee
|
c.calleeSym = callee
|
||||||
if callee.kind in skProcKinds and calleeScope == -1:
|
if callee.kind in skProcKinds and calleeScope == -1:
|
||||||
if callee.originatingModule == ctx.module:
|
if callee.originatingModule == ctx.module:
|
||||||
let rootSym = if sfFromGeneric notin callee.flags: callee
|
c.calleeScope = 2
|
||||||
else: callee.owner
|
var owner = callee
|
||||||
c.calleeScope = 2 # rootSym.scope.depthLevel
|
while true:
|
||||||
#echo "SCOPE IS ", rootSym.scope.depthLevel
|
owner = owner.skipGenericOwner
|
||||||
|
if owner.kind == skModule: break
|
||||||
|
inc c.calleeScope
|
||||||
else:
|
else:
|
||||||
c.calleeScope = 1
|
c.calleeScope = 1
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -3044,13 +3044,12 @@ template currentSourcePath*: string = instantiationInfo(-1, true).filename
|
||||||
proc raiseAssert*(msg: string) {.noinline.} =
|
proc raiseAssert*(msg: string) {.noinline.} =
|
||||||
sysFatal(AssertionError, msg)
|
sysFatal(AssertionError, msg)
|
||||||
|
|
||||||
when true:
|
proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} =
|
||||||
proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} =
|
# trick the compiler to not list ``AssertionError`` when called
|
||||||
# trick the compiler to not list ``EAssertionFailed`` when called
|
# by ``assert``.
|
||||||
# by ``assert``.
|
type THide = proc (msg: string) {.noinline, raises: [], noSideEffect,
|
||||||
type THide = proc (msg: string) {.noinline, raises: [], noSideEffect,
|
tags: [].}
|
||||||
tags: [].}
|
THide(raiseAssert)(msg)
|
||||||
THide(raiseAssert)(msg)
|
|
||||||
|
|
||||||
template assert*(cond: bool, msg = "") =
|
template assert*(cond: bool, msg = "") =
|
||||||
## Raises ``AssertionError`` with `msg` if `cond` is false. Note
|
## 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.} =
|
template onFailedAssert*(msg: expr, code: stmt): stmt {.dirty, immediate.} =
|
||||||
## Sets an assertion failure handler that will intercept any assert
|
## Sets an assertion failure handler that will intercept any assert
|
||||||
## statements following `onFailedAssert` in the current lexical scope.
|
## statements following `onFailedAssert` in the current module scope.
|
||||||
## Can be defined multiple times in a single function.
|
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
|
## # 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
|
||||||
##
|
##
|
||||||
## proc example(x: int): ErrorCode =
|
template failedAssertImpl(msgIMPL: string): stmt {.dirty.} =
|
||||||
## onFailedAssert(msg):
|
|
||||||
## log msg
|
|
||||||
## return E_FAIL
|
|
||||||
##
|
|
||||||
## assert(...)
|
|
||||||
##
|
|
||||||
## onFailedAssert(msg):
|
|
||||||
## raise newException(MyError, msg)
|
|
||||||
##
|
|
||||||
## assert(...)
|
|
||||||
##
|
|
||||||
template failedAssertImpl(msgIMPL: string): stmt {.dirty, immediate.} =
|
|
||||||
let msg = msgIMPL
|
let msg = msgIMPL
|
||||||
code
|
code
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ proc bar: int =
|
||||||
# local overrides that are active only
|
# local overrides that are active only
|
||||||
# in this proc
|
# in this proc
|
||||||
onFailedAssert(msg): echo "WARNING: " & msg
|
onFailedAssert(msg): echo "WARNING: " & msg
|
||||||
|
|
||||||
assert(false, "first assertion from bar")
|
assert(false, "first assertion from bar")
|
||||||
|
|
||||||
onFailedAssert(msg):
|
onFailedAssert(msg):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue