Skip to content

Commit 90e1f96

Browse files
committed
use onEnforceFail proc for smaller binaries
1 parent a681ec1 commit 90e1f96

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

lib/std/asserts.nim

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import system/assertions
22
import std/private/miscdollars
33

4-
template enforce*(cond: untyped, msg: string, typ: typedesc = CatchableError) =
5-
## similar to `doAssert` but defaults to raising `CatchableError` instead of
6-
## `AssertionDefect`, and allows customizing the raised exception type.
4+
proc onEnforceFail[T](typ: typedesc, prefix: string, arg: T) {.noreturn, noinline.} =
5+
## Making this a proc reduces size of binaries
6+
raise newException(typ, prefix & $arg)
7+
8+
template enforce*[T](cond: untyped, arg: T, typ: typedesc = EnforceError) =
9+
## similar to `doAssert` but defaults to raising catchable exception
10+
## instead of `AssertionDefect`, and allows customizing the raised exception type.
11+
# `-d:nimLeanMessages` further reduces size of binaries at expense of not
12+
# showing location information; in future we can avoid generating un-necessary
13+
# strings and forward `TLineInfo` directly (or some equivalent compact type),
14+
# and then defer the string rendering until needed in `onEnforceFail`,
15+
# reducing binary size while preserving location information. stacktraces
16+
# can benefit from the same optimization.
717
runnableExamples:
818
let a = 1
919
enforce a == 1, $(a,)
10-
doAssertRaises(CatchableError): enforce a == 2, $(a,)
20+
doAssertRaises(EnforceError): enforce a == 2, $(a,)
1121
doAssertRaises(ValueError): enforce a == 2, $(a,), ValueError
12-
13-
const
14-
loc = instantiationInfo(fullPaths = compileOption("excessiveStackTrace"))
15-
ploc = $loc
22+
const loc = instantiationInfo(fullPaths = compileOption("excessiveStackTrace"))
1623
{.line: loc.}:
1724
if not cond:
18-
const expr = astToStr(cond)
19-
raise newException(typ, ploc & " `" & expr & "` " & msg)
25+
const prefix =
26+
when defined(nimLeanMessages): ""
27+
else: $loc & " `" & astToStr(cond) & "` "
28+
onEnforceFail(typ, prefix, arg)

lib/system/assertions.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ template doAssertRaises*(exception: typedesc, code: untyped) =
8383
const begin = "expected raising '" & astToStr(exception) & "', instead"
8484
const msgEnd = " by: " & astToStr(code)
8585
template raisedForeign = raiseAssert(begin & " raised foreign exception" & msgEnd)
86+
mixin `$` # for `tests/test_nimscript.nims` (import assertions appears before dollars)
8687
when Exception is exception:
8788
try:
8889
if true:

lib/system/exceptions.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ type
5656

5757
CatchableError* = object of Exception ## \
5858
## Abstract class for all exceptions that are catchable.
59+
EnforceError* = object of Exception ## \
60+
## Default exception raised by `asserts.enforce`.
5961
IOError* = object of CatchableError ## \
6062
## Raised if an IO error occurred.
6163
EOFError* = object of IOError ## \

0 commit comments

Comments
 (0)