diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2018-08-25 12:48:37 -0700 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-08-25 21:48:37 +0200 |
commit | 3a626179ee041ee3b4e2068d9baf7799bad8ca35 (patch) | |
tree | c41a410ffbac8621f7ba6390eaca608f1432266d /lib | |
parent | 81f920a4ee0c234b953d34ab7d7e8db891638f3f (diff) | |
download | Nim-3a626179ee041ee3b4e2068d9baf7799bad8ca35.tar.gz |
doAssert, assert now print full path of failing line on error (#8555)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/core/macros.nim | 4 | ||||
-rw-r--r-- | lib/system.nim | 28 | ||||
-rw-r--r-- | lib/system/helpers.nim | 11 |
3 files changed, 27 insertions, 16 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 90fea440e..bac9ce7a2 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -8,6 +8,7 @@ # include "system/inclrtl" +include "system/helpers" ## This module contains the interface to the compiler's abstract syntax ## tree (`AST`:idx:). Macros operate on this tree. @@ -422,7 +423,8 @@ type line*,column*: int proc `$`*(arg: Lineinfo): string = - result = arg.filename & "(" & $arg.line & ", " & $arg.column & ")" + # BUG: without `result = `, gives compile error + result = lineInfoToString(arg.filename, arg.line, arg.column) #proc lineinfo*(n: NimNode): LineInfo {.magic: "NLineInfo", noSideEffect.} ## returns the position the node appears in the original source file diff --git a/lib/system.nim b/lib/system.nim index dcfb04c5a..8b98706ab 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -3758,6 +3758,16 @@ proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} = tags: [].} Hide(raiseAssert)(msg) +include "system/helpers" # for `lineInfoToString` + +template assertImpl(cond: bool, msg = "", enabled: static[bool]) = + const loc = $instantiationInfo(-1, true) + bind instantiationInfo + mixin failedAssertImpl + when enabled: + if not cond: + failedAssertImpl(loc & " `" & astToStr(cond) & "` " & msg) + template assert*(cond: bool, msg = "") = ## Raises ``AssertionError`` with `msg` if `cond` is false. Note ## that ``AssertionError`` is hidden from the effect system, so it doesn't @@ -3767,23 +3777,11 @@ template assert*(cond: bool, msg = "") = ## The compiler may not generate any code at all for ``assert`` if it is ## advised to do so through the ``-d:release`` or ``--assertions:off`` ## `command line switches <nimc.html#command-line-switches>`_. - # NOTE: `true` is correct here; --excessiveStackTrace:on will control whether - # or not to output full paths. - bind instantiationInfo - mixin failedAssertImpl - {.line: instantiationInfo(fullPaths = true).}: - when compileOption("assertions"): - if not cond: failedAssertImpl(astToStr(cond) & ' ' & msg) + assertImpl(cond, msg, compileOption("assertions")) template doAssert*(cond: bool, msg = "") = - ## same as `assert` but is always turned on and not affected by the - ## ``--assertions`` command line switch. - # NOTE: `true` is correct here; --excessiveStackTrace:on will control whether - # or not to output full paths. - bind instantiationInfo - mixin failedAssertImpl - {.line: instantiationInfo(fullPaths = true).}: - if not cond: failedAssertImpl(astToStr(cond) & ' ' & msg) + ## same as ``assert`` but is always turned on regardless of ``--assertions`` + assertImpl(cond, msg, true) iterator items*[T](a: seq[T]): T {.inline.} = ## iterates over each item of `a`. diff --git a/lib/system/helpers.nim b/lib/system/helpers.nim new file mode 100644 index 000000000..fb1218684 --- /dev/null +++ b/lib/system/helpers.nim @@ -0,0 +1,11 @@ +## helpers used system.nim and other modules, avoids code duplication while +## also minimizing symbols exposed in system.nim +# +# TODO: move other things here that should not be exposed in system.nim + +proc lineInfoToString(file: string, line, column: int): string = + file & "(" & $line & ", " & $column & ")" + +proc `$`(info: type(instantiationInfo(0))): string = + # The +1 is needed here + lineInfoToString(info.fileName, info.line, info.column+1) |