Skip to content

Commit

Permalink
more strictdef fixes for stdlibs (#24535)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Dec 13, 2024
1 parent be4d19e commit d31cce5
Show file tree
Hide file tree
Showing 21 changed files with 140 additions and 97 deletions.
2 changes: 2 additions & 0 deletions lib/pure/cstrutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func startsWith*(s, prefix: cstring): bool {.rtl, extern: "csuStartsWith".} =
assert startsWith(cstring"Hello", cstring"")

when nimvm:
result = false
startsWithImpl(s, prefix)
else:
when defined(js):
Expand All @@ -56,6 +57,7 @@ func endsWith*(s, suffix: cstring): bool {.rtl, extern: "csuEndsWith".} =
assert endsWith(cstring"Hello", cstring"")

when nimvm:
result = false
endsWithImpl(s, suffix)
else:
when defined(js):
Expand Down
15 changes: 8 additions & 7 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ proc getLastModificationTime*(file: string): times.Time {.rtl, extern: "nos$1",
## * `getCreationTime proc`_
## * `fileNewer proc`_
when defined(posix):
var res: Stat
var res: Stat = default(Stat)
if stat(file, res) < 0'i32: raiseOSError(osLastError(), file)
result = res.st_mtim.toTime
else:
Expand All @@ -305,7 +305,7 @@ proc getLastAccessTime*(file: string): times.Time {.rtl, extern: "nos$1", noWeir
## * `getCreationTime proc`_
## * `fileNewer proc`_
when defined(posix):
var res: Stat
var res: Stat = default(Stat)
if stat(file, res) < 0'i32: raiseOSError(osLastError(), file)
result = res.st_atim.toTime
else:
Expand All @@ -327,7 +327,7 @@ proc getCreationTime*(file: string): times.Time {.rtl, extern: "nos$1", noWeirdT
## * `getLastAccessTime proc`_
## * `fileNewer proc`_
when defined(posix):
var res: Stat
var res: Stat = default(Stat)
if stat(file, res) < 0'i32: raiseOSError(osLastError(), file)
result = res.st_ctim.toTime
else:
Expand Down Expand Up @@ -698,7 +698,7 @@ proc sleep*(milsecs: int) {.rtl, extern: "nos$1", tags: [TimeEffect], noWeirdTar
return # fixes #23732
winlean.sleep(int32(milsecs))
else:
var a, b: Timespec
var a, b: Timespec = default(Timespec)
a.tv_sec = posix.Time(milsecs div 1000)
a.tv_nsec = (milsecs mod 1000) * 1000 * 1000
discard posix.nanosleep(a, b)
Expand All @@ -714,7 +714,7 @@ proc getFileSize*(file: string): BiggestInt {.rtl, extern: "nos$1",
result = rdFileSize(a)
findClose(resA)
else:
var rawInfo: Stat
var rawInfo: Stat = default(Stat)
if stat(file, rawInfo) < 0'i32:
raiseOSError(osLastError(), file)
rawInfo.st_size
Expand Down Expand Up @@ -881,6 +881,7 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo {.noWeirdTarget.
## See also:
## * `getFileInfo(handle) proc`_
## * `getFileInfo(file) proc`_
result = default(FileInfo)
when defined(windows):
var
handle = openHandle(path, followSymlink)
Expand All @@ -892,7 +893,7 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo {.noWeirdTarget.
rawToFormalFileInfo(rawInfo, path, result)
discard closeHandle(handle)
else:
var rawInfo: Stat
var rawInfo: Stat = default(Stat)
if followSymlink:
if stat(path, rawInfo) < 0'i32:
raiseOSError(osLastError(), path)
Expand All @@ -909,7 +910,7 @@ proc sameFileContent*(path1, path2: string): bool {.rtl, extern: "nos$1",
## See also:
## * `sameFile proc`_
var
a, b: File
a, b: File = default(File)
if not open(a, path1): return false
if not open(b, path2):
close(a)
Expand Down
6 changes: 3 additions & 3 deletions lib/pure/parsecfg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ proc getKeyValPair(c: var CfgParser, kind: CfgEventKind): CfgEvent =
case kind
of cfgOption, cfgKeyValuePair:
result = CfgEvent(kind: kind, key: c.tok.literal.move, value: "")
else: discard
else: result = CfgEvent()
rawGetTok(c, c.tok)
if c.tok.kind in {tkEquals, tkColon}:
rawGetTok(c, c.tok)
Expand Down Expand Up @@ -512,7 +512,7 @@ proc loadConfig*(stream: Stream, filename: string = "[stream]"): Config =
var curSection = "" ## Current section,
## the default value of the current section is "",
## which means that the current section is a common
var p: CfgParser
var p: CfgParser = default(CfgParser)
open(p, stream, filename)
while true:
var e = next(p)
Expand Down Expand Up @@ -574,7 +574,7 @@ proc writeConfig*(dict: Config, stream: Stream) =
else:
stream.writeLine("[" & section & "]")
for key, value in sectionData.pairs():
var kv, segmentChar: string
var kv, segmentChar: string = ""
if key.len > 1 and key[0] == '-' and key[1] == '-': ## If it is a command key
segmentChar = ":"
if not allCharsInSet(key[2..key.len()-1], SymChars):
Expand Down
11 changes: 4 additions & 7 deletions lib/pure/parseopt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,10 @@ proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
p = initOptParser(@["--left", "--debug:3", "-l", "-r:2"])
p = initOptParser(@["--left", "--debug:3", "-l", "-r:2"],
shortNoVal = {'l'}, longNoVal = @["left"])

result.pos = 0
result.idx = 0
result.inShortState = false
result.shortNoVal = shortNoVal
result.longNoVal = longNoVal
result.allowWhitespaceAfterColon = allowWhitespaceAfterColon
result = OptParser(pos: 0, idx: 0, inShortState: false,
shortNoVal: shortNoVal, longNoVal: longNoVal,
allowWhitespaceAfterColon: allowWhitespaceAfterColon
)
if cmdline.len != 0:
result.cmds = newSeq[string](cmdline.len)
for i in 0..<cmdline.len:
Expand Down
21 changes: 18 additions & 3 deletions lib/pure/parseutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ proc parseBin*[T: SomeInteger](s: openArray[char], number: var T, maxLen = 0): i
if foundDigit:
number = output
result = i
else:
result = 0

proc parseOct*[T: SomeInteger](s: openArray[char], number: var T, maxLen = 0): int {.noSideEffect.} =
## Parses an octal number and stores its value in ``number``.
Expand Down Expand Up @@ -151,6 +153,8 @@ proc parseOct*[T: SomeInteger](s: openArray[char], number: var T, maxLen = 0): i
if foundDigit:
number = output
result = i
else:
result = 0

proc parseHex*[T: SomeInteger](s: openArray[char], number: var T, maxLen = 0): int {.noSideEffect.} =
## Parses a hexadecimal number and stores its value in ``number``.
Expand Down Expand Up @@ -225,6 +229,8 @@ proc parseIdent*(s: openArray[char], ident: var string): int =
while i < s.len and s[i] in IdentChars: inc(i)
ident = substr(s.toOpenArray(0, i-1))
result = i
else:
result = 0

proc parseIdent*(s: openArray[char]): string =
## Parses an identifier and returns it or an empty string in
Expand All @@ -234,12 +240,13 @@ proc parseIdent*(s: openArray[char]): string =
doAssert parseIdent("Hello World", 1) == "ello"
doAssert parseIdent("Hello World", 5) == ""
doAssert parseIdent("Hello World", 6) == "World"
result = ""
var i = 0
if i < s.len and s[i] in IdentStartChars:
inc(i)
while i < s.len and s[i] in IdentChars: inc(i)
result = substr(s.toOpenArray(0, i - 1))
else:
result = ""

proc parseChar*(s: openArray[char], c: var char): int =
## Parses a single character, stores it in `c` and returns 1.
Expand All @@ -254,6 +261,8 @@ proc parseChar*(s: openArray[char], c: var char): int =
if s.len > 0:
c = s[0]
result = 1
else:
result = 0

proc skipWhitespace*(s: openArray[char]): int {.inline.} =
## Skips the whitespace starting at ``s[start]``. Returns the number of
Expand Down Expand Up @@ -417,7 +426,7 @@ proc captureBetween*(s: openArray[char], first: char, second = '\0'): string =
result = ""
discard parseUntil(s.toOpenArray(i, s.high), result, if second == '\0': first else: second)

proc integerOutOfRangeError() {.noinline.} =
proc integerOutOfRangeError() {.noinline, noreturn.} =
raise newException(ValueError, "Parsed integer outside of valid range")

# See #6752
Expand Down Expand Up @@ -448,6 +457,8 @@ proc rawParseInt(s: openArray[char], b: var BiggestInt): int =
else:
b = b * sign
result = i
else:
result = 0

when defined(js):
{.pop.} # overflowChecks: off
Expand Down Expand Up @@ -510,6 +521,8 @@ proc parseSaturatedNatural*(s: openArray[char], b: var int): int {.
inc(i)
while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored
result = i
else:
result = 0

proc rawParseUInt(s: openArray[char], b: var BiggestUInt): int =
var
Expand All @@ -530,6 +543,8 @@ proc rawParseUInt(s: openArray[char], b: var BiggestUInt): int =
while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored
b = res
result = i
else:
result = 0

proc parseBiggestUInt*(s: openArray[char], number: var BiggestUInt): int {.
rtl, extern: "npuParseBiggestUInt", noSideEffect, raises: [ValueError].} =
Expand Down Expand Up @@ -632,7 +647,7 @@ func parseSize*(s: openArray[char], size: var int64, alwaysBin=false): int =
const scaleB = [1.0, 1024, 1048576, 1073741824, 1099511627776.0, # 2^(10*idx)
1125899906842624.0, 1152921504606846976.0, # ldexp?
1.180591620717411303424e21, 1.208925819614629174706176e24]
var number: float
var number: float = 0.0
var scale = 1.0
result = parseFloat(s, number)
if number < 0: # While parseFloat accepts negatives ..
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/parsexml.nim
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ proc parseEntity(my: var XmlParser, dest: var string) =
var pos = my.bufpos+1
my.kind = xmlCharData
if my.buf[pos] == '#':
var r: int
var r: int = 0
inc(pos)
if my.buf[pos] == 'x':
inc(pos)
Expand Down
3 changes: 2 additions & 1 deletion lib/pure/pathnorm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ proc hasNext*(it: PathIter; x: string): bool =
it.i < x.len

proc next*(it: var PathIter; x: string): (int, int) =
result = (0, 0)
it.prev = it.i
if not it.notFirst and x[it.i] in {DirSep, AltSep}:
# absolute path:
Expand Down Expand Up @@ -71,7 +72,7 @@ proc addNormalizePath*(x: string; result: var string; state: var int;

# state: 0th bit set if isAbsolute path. Other bits count
# the number of path components.
var it: PathIter
var it: PathIter = default(PathIter)
it.notFirst = (state shr 1) > 0
if it.notFirst:
while it.i < x.len and x[it.i] in {DirSep, AltSep}: inc it.i
Expand Down
6 changes: 3 additions & 3 deletions lib/pure/random.nim
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,7 @@ proc initRand*(seed: int64): Rand =
var r2 = initRand(now.toUnix * 1_000_000_000 + now.nanosecond)
const seedFallback0 = int32.high # arbitrary
let seed = if seed != 0: seed else: seedFallback0 # because 0 is a fixed point
result.a0 = Ui(seed shr 16)
result.a1 = Ui(seed and 0xffff)
result = Rand(a0: Ui(seed shr 16), a1: Ui(seed and 0xffff))
when not defined(nimLegacyRandomInitRand):
# calling `discard next(result)` (even a few times) would still produce
# skewed numbers for the 1st call to `rand()`.
Expand Down Expand Up @@ -708,7 +707,8 @@ when not defined(standalone):
if not result.isValid:
result = DefaultRandSeed
else:
var urand: array[sizeof(Rand), byte]
result = default(Rand)
var urand = default(array[sizeof(Rand), byte])

for i in 0 .. 7:
if sysrand.urandom(urand):
Expand Down
10 changes: 5 additions & 5 deletions lib/pure/ropes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ proc insertInCache(s: string, tree: Rope): Rope =
result = newRope(s)
when countCacheMisses: inc(misses)
return
var cmp: int
var cmp: int = 0
t = splay(s, t, cmp)
if cmp == 0:
# We get here if it's already in the Tree
Expand Down Expand Up @@ -209,7 +209,7 @@ proc `&`*(a: openArray[Rope]): Rope {.rtl, extern: "nroConcOpenArray".} =
runnableExamples:
let r = &[rope("Hello, "), rope("Nim"), rope("!")]
doAssert $r == "Hello, Nim!"

result = nil
for item in a: result = result & item

proc add*(a: var Rope, b: Rope) {.rtl, extern: "nro$1Rope".} =
Expand Down Expand Up @@ -239,7 +239,7 @@ proc `[]`*(r: Rope, i: int): char {.rtl, extern: "nroCharAt".} =
doAssert r[0] == 'H'
doAssert r[7] == 'N'
doAssert r[22] == '\0'

result = '\0'
var x = r
var j = i
if x == nil or i < 0 or i >= r.len: return
Expand Down Expand Up @@ -362,7 +362,7 @@ when not defined(js) and not defined(nimscript):
proc equalsFile*(r: Rope, f: File): bool {.rtl, extern: "nro$1File".} =
## Returns true if the contents of the file `f` equal `r`.
var
buf: array[bufSize, char]
buf: array[bufSize, char] = default(array[bufSize, char])
bpos = buf.len
blen = buf.len

Expand All @@ -389,7 +389,7 @@ when not defined(js) and not defined(nimscript):
proc equalsFile*(r: Rope, filename: string): bool {.rtl, extern: "nro$1Str".} =
## Returns true if the contents of the file `f` equal `r`. If `f` does not
## exist, false is returned.
var f: File
var f: File = default(File)
result = open(f, filename)
if result:
result = equalsFile(r, f)
Expand Down
3 changes: 3 additions & 0 deletions lib/pure/stats.nim
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ proc variance*(s: RunningStat): float =
proc varianceS*(s: RunningStat): float =
## Computes the current sample variance of `s`.
if s.n > 1: result = s.mom2 / toFloat(s.n - 1)
else: result = 0.0

proc standardDeviation*(s: RunningStat): float =
## Computes the current population standard deviation of `s`.
Expand Down Expand Up @@ -167,6 +168,7 @@ proc `+`*(a, b: RunningStat): RunningStat =
##
## Useful when performing parallel analysis of data series
## and needing to re-combine parallel result sets.
result = default(RunningStat)
result.clear()
result.n = a.n + b.n

Expand Down Expand Up @@ -317,6 +319,7 @@ proc `+`*(a, b: RunningRegress): RunningRegress =
##
## Useful when performing parallel analysis of data series
## and needing to re-combine parallel result sets
result = default(RunningRegress)
result.clear()
result.x_stats = a.x_stats + b.x_stats
result.y_stats = a.y_stats + b.y_stats
Expand Down
Loading

0 comments on commit d31cce5

Please sign in to comment.