summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2018-10-13 15:17:10 -0700
committerAndreas Rumpf <rumpf_a@web.de>2018-10-14 00:17:10 +0200
commit3e2d8c1c535d75d3451ce016e7b54cb609c01e45 (patch)
tree0e6170c6d610b536c3e16115ccb5b07390f3f4fe /lib
parent166720bdf906c20d485b6baadf4c301432dda1de (diff)
downloadNim-3e2d8c1c535d75d3451ce016e7b54cb609c01e45.tar.gz
correctly render AST in doAssert/assert condition: fixes #8518; refs #9301 (#9332)
* fixes #8518; refs #9301; correctly render AST in doAssert condition
Diffstat (limited to 'lib')
-rw-r--r--lib/system.nim14
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 836ea8463..1f3000aa8 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -3880,7 +3880,7 @@ proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} =
 
 include "system/helpers" # for `lineInfoToString`
 
-template assertImpl(cond: bool, msg = "", enabled: static[bool]) =
+template assertImpl(cond: bool, msg: string, expr: string, enabled: static[bool]) =
   const loc = $instantiationInfo(-1, true)
   bind instantiationInfo
   mixin failedAssertImpl
@@ -3889,9 +3889,9 @@ template assertImpl(cond: bool, msg = "", enabled: static[bool]) =
     # here, regardless of --excessiveStackTrace
     {.line: instantiationInfo(fullPaths = true).}:
       if not cond:
-        failedAssertImpl(loc & " `" & astToStr(cond) & "` " & msg)
+        failedAssertImpl(loc & " `" & expr & "` " & msg)
 
-template assert*(cond: bool, msg = "") =
+template assert*(cond: untyped, msg = "") =
   ## Raises ``AssertionError`` with `msg` if `cond` is false. Note
   ## that ``AssertionError`` is hidden from the effect system, so it doesn't
   ## produce ``{.raises: [AssertionError].}``. This exception is only supposed
@@ -3900,11 +3900,13 @@ 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>`_.
-  assertImpl(cond, msg, compileOption("assertions"))
+  const expr = astToStr(cond)
+  assertImpl(cond, msg, expr, compileOption("assertions"))
 
-template doAssert*(cond: bool, msg = "") =
+template doAssert*(cond: untyped, msg = "") =
   ## same as ``assert`` but is always turned on regardless of ``--assertions``
-  assertImpl(cond, msg, true)
+  const expr = astToStr(cond)
+  assertImpl(cond, msg, expr, true)
 
 iterator items*[T](a: seq[T]): T {.inline.} =
   ## iterates over each item of `a`.