From 3f7e1d7daadf4002da1a155d7b98ff7fcca9e2fa Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sat, 12 Aug 2023 00:24:46 +0800 Subject: replace `doAssert false` with `raiseAssert` in lib, which works better with strictdefs (#22458) --- lib/impure/re.nim | 2 +- lib/js/dom.nim | 6 +++--- lib/packages/docutils/rstgen.nim | 2 +- lib/pure/coro.nim | 4 ++-- lib/pure/hashes.nim | 8 ++++---- lib/pure/osproc.nim | 2 +- lib/pure/parseopt.nim | 2 +- lib/pure/streams.nim | 2 +- lib/pure/strformat.nim | 2 +- lib/pure/times.nim | 2 +- lib/pure/typetraits.nim | 2 +- lib/pure/unittest.nim | 2 +- lib/std/formatfloat.nim | 4 ++-- lib/std/genasts.nim | 2 +- lib/std/jsbigints.nim | 6 +++--- lib/std/jsonutils.nim | 6 +++--- lib/std/private/globs.nim | 2 +- lib/std/private/osfiles.nim | 2 +- lib/std/private/ospaths2.nim | 4 ++-- lib/std/sysrand.nim | 2 +- lib/system.nim | 2 +- testament/lib/stdtest/specialpaths.nim | 2 +- 22 files changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/impure/re.nim b/lib/impure/re.nim index f97a31d21..5e84091c7 100644 --- a/lib/impure/re.nim +++ b/lib/impure/re.nim @@ -460,7 +460,7 @@ template `=~` *(s: string, pattern: Regex): untyped = elif line =~ re"\s*(\#.*)": # matches a comment # note that the implicit `matches` array is different from 1st branch result = $(matches[0],) - else: doAssert false + else: raiseAssert "unreachable" doAssert not declared(matches) doAssert parse("NAME = LENA") == """("NAME", "LENA")""" doAssert parse(" # comment ... ") == """("# comment ... ",)""" diff --git a/lib/js/dom.nim b/lib/js/dom.nim index 0a825865b..ceb0375b7 100644 --- a/lib/js/dom.nim +++ b/lib/js/dom.nim @@ -1423,7 +1423,7 @@ when defined(nodejs): parent.childNodes[i] = newNode return inc i - doAssert false, "old node not in node list" + raiseAssert "old node not in node list" proc removeChild*(parent, child: Node) = child.parentNode = nil @@ -1433,7 +1433,7 @@ when defined(nodejs): parent.childNodes.delete(i) return inc i - doAssert false, "old node not in node list" + raiseAssert "old node not in node list" proc insertBefore*(parent, newNode, before: Node) = appendChild(parent, newNode) @@ -1445,7 +1445,7 @@ when defined(nodejs): parent.childNodes[i-1] = newNode return inc i - #doAssert false, "before not in node list" + #raiseAssert "before not in node list" proc createElement*(d: Document, identifier: cstring): Element = new(result) diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index 008dff60a..f06e11de2 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -1175,7 +1175,7 @@ proc renderRstToOut(d: PDoc, n: PRstNode, result: var string) = renderAux(d, n, "
$1
", " $1\n", result) of rnOption, rnOptionString, rnOptionArgument: - doAssert false, "renderRstToOut" + raiseAssert "renderRstToOut" of rnLiteralBlock: renderAux(d, n, "$1\n", "\n\n$2\\begin{rstpre}\n$1\n\\end{rstpre}\n\n", result) diff --git a/lib/pure/coro.nim b/lib/pure/coro.nim index 7f0f551e6..5cdcb75fe 100644 --- a/lib/pure/coro.nim +++ b/lib/pure/coro.nim @@ -224,7 +224,7 @@ proc switchTo(current, to: CoroutinePtr) = elif to.state == CORO_CREATED: # Coroutine is started. coroExecWithStack(runCurrentTask, to.stack.bottom) - #doAssert false + #raiseAssert "unreachable" else: {.error: "Invalid coroutine backend set.".} # Execution was just resumed. Restore frame information and set active stack. @@ -266,7 +266,7 @@ proc runCurrentTask() = current.state = CORO_FINISHED nimGC_setStackBottom(ctx.ncbottom) suspend(0) # Exit coroutine without returning from coroExecWithStack() - doAssert false + raiseAssert "unreachable" proc start*(c: proc(), stacksize: int = defaultStackSize): CoroutineRef {.discardable.} = ## Schedule coroutine for execution. It does not run immediately. diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index ad164d6d3..6d246d5b9 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -368,16 +368,16 @@ proc murmurHash(x: openArray[byte]): Hash = return cast[Hash](h1) proc hashVmImpl(x: cstring, sPos, ePos: int): Hash = - doAssert false, "implementation override in compiler/vmops.nim" + raiseAssert "implementation override in compiler/vmops.nim" proc hashVmImpl(x: string, sPos, ePos: int): Hash = - doAssert false, "implementation override in compiler/vmops.nim" + raiseAssert "implementation override in compiler/vmops.nim" proc hashVmImplChar(x: openArray[char], sPos, ePos: int): Hash = - doAssert false, "implementation override in compiler/vmops.nim" + raiseAssert "implementation override in compiler/vmops.nim" proc hashVmImplByte(x: openArray[byte], sPos, ePos: int): Hash = - doAssert false, "implementation override in compiler/vmops.nim" + raiseAssert "implementation override in compiler/vmops.nim" proc hash*(x: string): Hash = ## Efficient hashing of strings. diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index e30f1da73..91c45e053 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -1348,7 +1348,7 @@ elif not defined(useNimRtl): p.exitStatus = status break else: - doAssert false, "unreachable!" + raiseAssert "unreachable!" result = exitStatusLikeShell(p.exitStatus) diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim index 059f8e0f5..2d039f1e4 100644 --- a/lib/pure/parseopt.nim +++ b/lib/pure/parseopt.nim @@ -251,7 +251,7 @@ proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {}, else: # we cannot provide this for NimRtl creation on Posix, because we can't # access the command line arguments then! - doAssert false, "empty command line given but" & + raiseAssert "empty command line given but" & " real command line is not accessible" result.kind = cmdEnd result.key = "" diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 003f8ca4c..e18e2d43a 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -1530,7 +1530,7 @@ when false: of fmReadWrite: flags = O_RDWR or int(O_CREAT) of fmReadWriteExisting: flags = O_RDWR of fmAppend: flags = O_WRONLY or int(O_CREAT) or O_APPEND - static: doAssert false # handle bug #17888 + static: raiseAssert "unreachable" # handle bug #17888 var handle = open(filename, flags) if handle < 0: raise newEOS("posix.open() call failed") result = newFileHandleStream(handle) diff --git a/lib/pure/strformat.nim b/lib/pure/strformat.nim index 3fedff07b..1cebefee1 100644 --- a/lib/pure/strformat.nim +++ b/lib/pure/strformat.nim @@ -663,7 +663,7 @@ proc strformatImpl(f: string; openChar, closeChar: char, strlit.add closeChar inc i, 2 else: - doAssert false, "invalid format string: '$1' instead of '$1$1'" % $closeChar + raiseAssert "invalid format string: '$1' instead of '$1$1'" % $closeChar inc i else: strlit.add f[i] diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 4f7af657c..f5775e4d9 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -2089,7 +2089,7 @@ proc parsePattern(input: string, pattern: FormatPattern, i: var int, i.inc 2 else: result = false - of Lit: doAssert false, "Can't happen" + of Lit: raiseAssert "Can't happen" proc toDateTime(p: ParsedTime, zone: Timezone, f: TimeFormat, input: string): DateTime = diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index c20f9e645..70eb1b81c 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -225,7 +225,7 @@ macro genericParamsImpl(T: typedesc): untyped = case ai.typeKind of ntyTypeDesc: ret = ai - of ntyStatic: doAssert false + of ntyStatic: raiseAssert "unreachable" else: # getType from a resolved symbol might return a typedesc symbol. # If so, use it directly instead of wrapping it in StaticParam. diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index afe98ca4e..3b3684789 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -235,7 +235,7 @@ proc colorOutput(): bool = else: result = false of "on": result = true of "off": result = false - else: doAssert false, $color + else: raiseAssert $color when declared(stdout): if existsEnv("NIMTEST_COLOR"): diff --git a/lib/std/formatfloat.nim b/lib/std/formatfloat.nim index b216d1fd0..48973aa55 100644 --- a/lib/std/formatfloat.nim +++ b/lib/std/formatfloat.nim @@ -86,7 +86,7 @@ proc writeFloatToBuffer*(buf: var array[65, char]; value: BiggestFloat | float32 proc addFloatRoundtrip*(result: var string; x: float | float32) = when nimvm: - doAssert false + raiseAssert "unreachable" else: var buffer {.noinit.}: array[65, char] let n = writeFloatToBufferRoundtrip(buffer, x) @@ -94,7 +94,7 @@ proc addFloatRoundtrip*(result: var string; x: float | float32) = proc addFloatSprintf*(result: var string; x: float) = when nimvm: - doAssert false + raiseAssert "unreachable" else: var buffer {.noinit.}: array[65, char] let n = writeFloatToBufferSprintf(buffer, x) diff --git a/lib/std/genasts.nim b/lib/std/genasts.nim index 05b2823ef..04257533d 100644 --- a/lib/std/genasts.nim +++ b/lib/std/genasts.nim @@ -24,7 +24,7 @@ macro genAstOpt*(options: static set[GenAstOpt], args: varargs[untyped]): untype result = genAst(cond, s = repr(cond), lhs = cond[1], rhs = cond[2]): # each local symbol we access must be explicitly captured if not cond: - doAssert false, "'$#'' failed: lhs: '$#', rhs: '$#'" % [s, $lhs, $rhs] + raiseAssert "'$#'' failed: lhs: '$#', rhs: '$#'" % [s, $lhs, $rhs] let a = 3 check2 a*2 == a+3 if false: check2 a*2 < a+1 # would error with: 'a * 2 < a + 1'' failed: lhs: '6', rhs: '4' diff --git a/lib/std/jsbigints.nim b/lib/std/jsbigints.nim index 067de78b5..4e996ea7b 100644 --- a/lib/std/jsbigints.nim +++ b/lib/std/jsbigints.nim @@ -14,7 +14,7 @@ func big*(integer: SomeInteger): JsBigInt {.importjs: "BigInt(#)".} = runnableExamples: doAssert big(1234567890) == big"1234567890" doAssert 0b1111100111.big == 0o1747.big and 0o1747.big == 999.big - when nimvm: doAssert false, "JsBigInt can not be used at compile-time nor static context" else: discard + when nimvm: raiseAssert "JsBigInt can not be used at compile-time nor static context" else: discard func `'big`*(num: cstring): JsBigInt {.importjs: "BigInt(#)".} = ## Constructor for `JsBigInt`. @@ -28,11 +28,11 @@ func `'big`*(num: cstring): JsBigInt {.importjs: "BigInt(#)".} = doAssert 0xdeadbeaf'big == 0xdeadbeaf.big doAssert 0xffffffffffffffff'big == (1'big shl 64'big) - 1'big doAssert not compiles(static(12'big)) - when nimvm: doAssert false, "JsBigInt can not be used at compile-time nor static context" else: discard + when nimvm: raiseAssert "JsBigInt can not be used at compile-time nor static context" else: discard func big*(integer: cstring): JsBigInt {.importjs: "BigInt(#)".} = ## Alias for `'big` - when nimvm: doAssert false, "JsBigInt can not be used at compile-time nor static context" else: discard + when nimvm: raiseAssert "JsBigInt can not be used at compile-time nor static context" else: discard func toCstring*(this: JsBigInt; radix: 2..36): cstring {.importjs: "#.toString(#)".} = ## Converts from `JsBigInt` to `cstring` representation. diff --git a/lib/std/jsonutils.nim b/lib/std/jsonutils.nim index 847761e2f..b1025d24b 100644 --- a/lib/std/jsonutils.nim +++ b/lib/std/jsonutils.nim @@ -95,7 +95,7 @@ macro getDiscriminants(a: typedesc): seq[string] = result = quote do: seq[string].default else: - doAssert false, "unexpected kind: " & $t2.kind + raiseAssert "unexpected kind: " & $t2.kind macro initCaseObject(T: typedesc, fun: untyped): untyped = ## does the minimum to construct a valid case object, only initializing @@ -109,7 +109,7 @@ macro initCaseObject(T: typedesc, fun: untyped): untyped = case t.kind of nnkObjectTy: t2 = t[2] of nnkRefTy: t2 = t[0].getTypeImpl[2] - else: doAssert false, $t.kind # xxx `nnkPtrTy` could be handled too + else: raiseAssert $t.kind # xxx `nnkPtrTy` could be handled too doAssert t2.kind == nnkRecList result = newTree(nnkObjConstr) result.add sym @@ -289,7 +289,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) = i.inc else: # checkJson not appropriate here - static: doAssert false, "not yet implemented: " & $T + static: raiseAssert "not yet implemented: " & $T proc jsonTo*(b: JsonNode, T: typedesc, opt = Joptions()): T = ## reverse of `toJson` diff --git a/lib/std/private/globs.nim b/lib/std/private/globs.nim index 5e3e33cb4..64065aac8 100644 --- a/lib/std/private/globs.nim +++ b/lib/std/private/globs.nim @@ -60,7 +60,7 @@ proc nativeToUnixPath*(path: string): string = result[0] = '/' result[1] = path[0] if path.len > 2 and path[2] != '\\': - doAssert false, "paths like `C:foo` are currently unsupported, path: " & path + raiseAssert "paths like `C:foo` are currently unsupported, path: " & path when DirSep == '\\': result = replace(result, '\\', '/') diff --git a/lib/std/private/osfiles.nim b/lib/std/private/osfiles.nim index 78afd35da..f2e7bf11d 100644 --- a/lib/std/private/osfiles.nim +++ b/lib/std/private/osfiles.nim @@ -396,7 +396,7 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1", if not tryMoveFSObject(source, dest, isDir = false): when defined(windows): - doAssert false + raiseAssert "unreachable" else: # Fallback to copy & del copyFile(source, dest, {cfSymlinkAsIs}) diff --git a/lib/std/private/ospaths2.nim b/lib/std/private/ospaths2.nim index 18a01b104..421def62b 100644 --- a/lib/std/private/ospaths2.nim +++ b/lib/std/private/ospaths2.nim @@ -259,7 +259,7 @@ proc isAbsolute*(path: string): bool {.rtl, noSideEffect, extern: "nos$1", raise # This works around the problem for posix, but Windows is still broken with nim js -d:nodejs result = path[0] == '/' else: - doAssert false # if ever hits here, adapt as needed + raiseAssert "unreachable" # if ever hits here, adapt as needed when FileSystemCaseSensitive: template `!=?`(a, b: char): bool = a != b @@ -859,7 +859,7 @@ when not defined(nimscript): {.emit: "`ret` = process.cwd();".} return $ret elif defined(js): - doAssert false, "use -d:nodejs to have `getCurrentDir` defined" + raiseAssert "use -d:nodejs to have `getCurrentDir` defined" elif defined(windows): var bufsize = MAX_PATH.int32 var res = newWideCString("", bufsize) diff --git a/lib/std/sysrand.nim b/lib/std/sysrand.nim index 7943f2e1b..8526336ad 100644 --- a/lib/std/sysrand.nim +++ b/lib/std/sysrand.nim @@ -192,7 +192,7 @@ elif defined(linux) and not defined(nimNoGetRandom) and not defined(emscripten): while result < size: let readBytes = syscall(SYS_getrandom, addr dest[result], cint(size - result), 0).int if readBytes == 0: - doAssert false + raiseAssert "unreachable" elif readBytes > 0: inc(result, readBytes) else: diff --git a/lib/system.nim b/lib/system.nim index 3076fe2fd..521380a57 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2289,7 +2289,7 @@ elif defined(nimdoc): ## `quit(int(0x100000000))` is equal to `quit(127)` on Linux. ## ## .. danger:: In almost all cases, in particular in library code, prefer - ## alternatives, e.g. `doAssert false` or raise a `Defect`. + ## alternatives, e.g. `raiseAssert` or raise a `Defect`. ## `quit` bypasses regular control flow in particular `defer`, ## `try`, `catch`, `finally` and `destructors`, and exceptions that may have been ## raised by an `addExitProc` proc, as well as cleanup code in other threads. diff --git a/testament/lib/stdtest/specialpaths.nim b/testament/lib/stdtest/specialpaths.nim index 7df63666f..e214d113d 100644 --- a/testament/lib/stdtest/specialpaths.nim +++ b/testament/lib/stdtest/specialpaths.nim @@ -48,7 +48,7 @@ proc splitTestFile*(file: string): tuple[cat: string, path: string] = else: result.path = file return result - doAssert false, "file must match this pattern: '/pathto/tests/dir/**/tfile.nim', got: '" & file & "'" + raiseAssert "file must match this pattern: '/pathto/tests/dir/**/tfile.nim', got: '" & file & "'" static: # sanity check -- cgit 1.4.1-2-gfad0