enable experimental:strictDefs (#24225)

This commit is contained in:
ringabout
2024-11-24 05:01:39 +08:00
committed by GitHub
parent 555191a3f0
commit 2df633180a
86 changed files with 276 additions and 255 deletions

View File

@@ -242,6 +242,7 @@ else:
`=destroy`(pattern.captureNameToId)
proc getinfo[T](pattern: Regex, opt: cint): T =
result = default(T)
let retcode = pcre.fullinfo(pattern.pcreObj, pattern.pcreExtra, opt, addr result)
if retcode < 0:
@@ -275,8 +276,8 @@ proc initRegex(pattern: string, flags: int, study = true): Regex =
new(result, destroyRegex)
result.pattern = pattern
var errorMsg: cstring
var errOffset: cint
var errorMsg: cstring = ""
var errOffset: cint = 0
result.pcreObj = pcre.compile(cstring(pattern),
# better hope int is at least 4 bytes..
@@ -288,7 +289,7 @@ proc initRegex(pattern: string, flags: int, study = true): Regex =
if study:
var options: cint = 0
var hasJit: cint
var hasJit: cint = cint(0)
if pcre.config(pcre.CONFIG_JIT, addr hasJit) == 0:
if hasJit == 1'i32:
options = pcre.STUDY_JIT_COMPILE
@@ -313,7 +314,7 @@ proc matchesCrLf(pattern: Regex): bool =
return true
# get flags from build config
var confFlags: cint
var confFlags: cint = cint(0)
if pcre.config(pcre.CONFIG_NEWLINE, addr confFlags) != 0:
assert(false, "CONFIG_NEWLINE apparently got screwed up")
@@ -573,7 +574,7 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = int.high): R
pcre.UTF8) > 0u32
let strlen = if endpos == int.high: str.len else: endpos+1
var offset = start
var match: Option[RegexMatch]
var match: Option[RegexMatch] = default(Option[RegexMatch])
var neverMatched = true
while true:
@@ -762,7 +763,7 @@ proc escapeRe*(str: string): string {.gcsafe.} =
const SpecialCharMatcher = {'\\', '+', '*', '?', '[', '^', ']', '$', '(',
')', '{', '}', '=', '!', '<', '>', '|', ':',
'-'}
result = ""
for c in items(str):
case c
of SpecialCharMatcher:

View File

@@ -466,7 +466,7 @@ template `=~` *(s: string, pattern: Regex): untyped =
doAssert parse(" # comment ... ") == """("# comment ... ",)"""
bind MaxSubpatterns
when not declaredInScope(matches):
var matches {.inject.}: array[MaxSubpatterns, string]
var matches {.inject.}: array[MaxSubpatterns, string] = default(array[MaxSubpatterns, string])
match(s, pattern, matches)
# ------------------------- more string handling ------------------------------

View File

@@ -143,6 +143,7 @@ proc reversed*[T](a: openArray[T]): seq[T] {.inline.} =
runnableExamples:
assert [10, 11, 12].reversed == @[12, 11, 10]
assert seq[string].default.reversed == @[]
result = @[]
let n = a.len
result.setLen(n)
for i in 0..<n: result[i] = a[n - (i + 1)]

View File

@@ -267,9 +267,9 @@ proc asyncSingleProc(prc: NimNode): NimNode =
procBody.insert(0): quote do:
{.push warning[resultshadowed]: off.}
when `subRetType` isnot void:
var `resultIdent`: `subRetType`
var `resultIdent`: `subRetType` = default(`subRetType`)
else:
var `resultIdent`: Future[void]
var `resultIdent`: Future[void] = nil
{.pop.}
var `needsCompletionSym` = false

View File

@@ -151,10 +151,12 @@ proc encode*[T: byte|char](s: openArray[T], safe = false): string =
assert encode(['n', 'i', 'm']) == "bmlt"
assert encode(@['n', 'i', 'm']) == "bmlt"
assert encode([1'u8, 2, 3, 4, 5]) == "AQIDBAU="
result = ""
encodeImpl()
proc encode*[T: SomeInteger and not byte](s: openArray[T], safe = false): string
{.deprecated: "use `byte` or `char` instead".} =
result = ""
encodeImpl()
proc encodeMime*(s: string, lineLen = 75.Positive, newLine = "\r\n",

View File

@@ -67,6 +67,7 @@ func len*[T](c: CritBitTree[T]): int {.inline.} =
result = c.count
proc rawGet[T](c: CritBitTree[T], key: string): Node[T] =
result = nil
var it = c.root
while it != nil:
if not it.isLeaf:
@@ -145,7 +146,7 @@ func exclImpl[T](c: var CritBitTree[T], key: string): int =
var whereq: ptr Node[T] = nil
if p == nil: return c.count
var dir = 0
var q: Node[T]
var q: Node[T] = nil
while not p.isLeaf:
whereq = wherep
q = p
@@ -394,6 +395,7 @@ iterator mpairs*[T](c: var CritBitTree[T]): tuple[key: string, val: var T] =
for x in leaves(c.root): yield (x.key, x.val)
proc allprefixedAux[T](c: CritBitTree[T], key: string): Node[T] =
result = nil
var p = c.root
var top = p
if p != nil:

View File

@@ -92,6 +92,7 @@ proc initDeque*[T](initialSize: int = defaultInitialSize): Deque[T] =
##
## **See also:**
## * `toDeque proc <#toDeque,openArray[T]>`_
result = Deque[T]()
result.initImpl(initialSize)
func len*[T](deq: Deque[T]): int {.inline.} =
@@ -297,7 +298,7 @@ proc toDeque*[T](x: openArray[T]): Deque[T] {.since: (1, 3).} =
let a = toDeque([7, 8, 9])
assert len(a) == 3
assert $a == "[7, 8, 9]"
result = Deque[T]()
result.initImpl(x.len)
for item in items(x):
result.addLast(item)

View File

@@ -157,7 +157,7 @@ proc toHeapQueue*[T](x: openArray[T]): HeapQueue[T] {.since: (1, 3).} =
assert heap[0] == 8
# see https://en.wikipedia.org/wiki/Binary_heap#Building_a_heap
result.data = @x
result = HeapQueue[T](data: @x)
for i in countdown(x.len div 2 - 1, 0):
siftdown(result, i)

View File

@@ -115,7 +115,7 @@ proc initSinglyLinkedList*[T](): SinglyLinkedList[T] =
runnableExamples:
let a = initSinglyLinkedList[int]()
discard
result = default(SinglyLinkedList[T])
proc initDoublyLinkedList*[T](): DoublyLinkedList[T] =
## Creates a new doubly linked list that is empty.
@@ -125,7 +125,7 @@ proc initDoublyLinkedList*[T](): DoublyLinkedList[T] =
runnableExamples:
let a = initDoublyLinkedList[int]()
discard
result = default(DoublyLinkedList[T])
proc initSinglyLinkedRing*[T](): SinglyLinkedRing[T] =
## Creates a new singly linked ring that is empty.
@@ -135,7 +135,7 @@ proc initSinglyLinkedRing*[T](): SinglyLinkedRing[T] =
runnableExamples:
let a = initSinglyLinkedRing[int]()
discard
result = default(SinglyLinkedRing[T])
proc initDoublyLinkedRing*[T](): DoublyLinkedRing[T] =
## Creates a new doubly linked ring that is empty.
@@ -145,7 +145,7 @@ proc initDoublyLinkedRing*[T](): DoublyLinkedRing[T] =
runnableExamples:
let a = initDoublyLinkedRing[int]()
discard
result = default(DoublyLinkedRing[T])
proc newDoublyLinkedNode*[T](value: T): DoublyLinkedNode[T] =
## Creates a new doubly linked node with the given `value`.
@@ -319,6 +319,10 @@ proc find*[T](L: SomeLinkedCollection[T], value: T): SomeLinkedNode[T] =
let a = [9, 8].toSinglyLinkedList
assert a.find(9).value == 9
assert a.find(1) == nil
when typeof(nodes(L)) is SinglyLinkedNode[T]:
result = SinglyLinkedNode[T](nil)
else:
result = DoublyLinkedNode[T](nil)
for x in nodes(L):
if x.value == value: return x

View File

@@ -167,7 +167,7 @@ func count*[T](s: openArray[T], x: T): int =
assert count(a, 2) == 4
assert count(a, 99) == 0
assert count(b, 'r') == 2
result = 0
for itm in items(s):
if itm == x:
inc result
@@ -244,7 +244,7 @@ func minIndex*[T](s: openArray[T]): int {.since: (1, 1).} =
assert minIndex(b) == 3
assert minIndex(c) == 1
assert minIndex(d) == 2
result = 0
for i in 1..high(s):
if s[i] < s[result]: result = i
@@ -261,7 +261,7 @@ func maxIndex*[T](s: openArray[T]): int {.since: (1, 1).} =
assert maxIndex(b) == 0
assert maxIndex(c) == 2
assert maxIndex(d) == 0
result = 0
for i in 1..high(s):
if s[i] > s[result]: result = i

View File

@@ -45,7 +45,7 @@ proc enlarge[A](s: var HashSet[A]) =
template inclImpl() {.dirty.} =
if s.data.len == 0:
initImpl(s, defaultInitialSize)
var hc: Hash
var hc: Hash = default(Hash)
var index = rawGet(s, key, hc)
if index < 0:
if mustRehash(s):

View File

@@ -670,7 +670,7 @@ proc initOrderedSet*[A](initialSize = defaultInitialSize): OrderedSet[A] =
var a = initOrderedSet[int]()
a.incl(3)
assert len(a) == 1
result = OrderedSet[A]()
result.init(initialSize)
proc toOrderedSet*[A](keys: openArray[A]): OrderedSet[A] =

View File

@@ -139,12 +139,15 @@ template withValue*[A, B](t: var SharedTable[A, B], key: A,
proc mget*[A, B](t: var SharedTable[A, B], key: A): var B =
## Retrieves the value at `t[key]`. The value can be modified.
## If `key` is not in `t`, the `KeyError` exception is raised.
withLock t:
var hc: Hash
var index = rawGet(t, key, hc)
let hasKey = index >= 0
if hasKey: result = t.data[index].val
if not hasKey:
acquire(t.lock)
var hc: Hash = Hash(0)
var index = rawGet(t, key, hc)
let hasKey = index >= 0
if hasKey:
result = t.data[index].val
release(t.lock)
else:
release(t.lock)
when compiles($key):
raise newException(KeyError, "key not found: " & $key)
else:

View File

@@ -2413,8 +2413,7 @@ proc smallest*[A](t: CountTable[A]): tuple[key: A, val: int] =
for h in 0 .. high(t.data):
if t.data[h].val > 0 and (minIdx == -1 or t.data[minIdx].val > t.data[h].val):
minIdx = h
result.key = t.data[minIdx].key
result.val = t.data[minIdx].val
result = (t.data[minIdx].key, t.data[minIdx].val)
proc largest*[A](t: CountTable[A]): tuple[key: A, val: int] =
## Returns the `(key, value)` pair with the largest `val`. Efficiency: O(n)
@@ -2425,8 +2424,7 @@ proc largest*[A](t: CountTable[A]): tuple[key: A, val: int] =
var maxIdx = 0
for h in 1 .. high(t.data):
if t.data[maxIdx].val < t.data[h].val: maxIdx = h
result.key = t.data[maxIdx].key
result.val = t.data[maxIdx].val
result = (t.data[maxIdx].key, t.data[maxIdx].val)
proc hasKey*[A](t: CountTable[A], key: A): bool =
## Returns true if `key` is in the table `t`.
@@ -2955,16 +2953,19 @@ iterator mvalues*[A](t: CountTableRef[A]): var int =
assert(len(t) == L, "the length of the table changed while iterating over it")
proc hash*[K,V](s: Table[K,V]): Hash =
result = Hash(0)
for p in pairs(s):
result = result xor hash(p)
result = !$result
proc hash*[K,V](s: OrderedTable[K,V]): Hash =
result = Hash(0)
for p in pairs(s):
result = result !& hash(p)
result = !$result
proc hash*[V](s: CountTable[V]): Hash =
result = Hash(0)
for p in pairs(s):
result = result xor hash(p)
result = !$result

View File

@@ -46,18 +46,15 @@ type
func complex*[T: SomeFloat](re: T; im: T = 0.0): Complex[T] =
## Returns a `Complex[T]` with real part `re` and imaginary part `im`.
result.re = re
result.im = im
result = Complex[T](re: re, im: im)
func complex32*(re: float32; im: float32 = 0.0): Complex32 =
## Returns a `Complex32` with real part `re` and imaginary part `im`.
result.re = re
result.im = im
result = Complex32(re: re, im: im)
func complex64*(re: float64; im: float64 = 0.0): Complex64 =
## Returns a `Complex64` with real part `re` and imaginary part `im`.
result.re = re
result.im = im
result = Complex64(re: re, im: im)
template im*(arg: typedesc[float32]): Complex32 = complex32(0, 1)
## Returns the imaginary unit (`complex32(0, 1)`).
@@ -85,15 +82,16 @@ func sgn*[T](z: Complex[T]): Complex[T] =
let a = abs(z)
if a != 0:
result = z / a
else:
result = Complex[T]()
func conjugate*[T](z: Complex[T]): Complex[T] =
## Returns the complex conjugate of `z` (`complex(z.re, -z.im)`).
result.re = z.re
result.im = -z.im
result = Complex[T](re: z.re, im: -z.im)
func inv*[T](z: Complex[T]): Complex[T] =
## Returns the multiplicative inverse of `z` (`1/z`).
conjugate(z) / abs2(z)
result = conjugate(z) / abs2(z)
func `==`*[T](x, y: Complex[T]): bool =
## Compares two complex numbers for equality.
@@ -101,58 +99,48 @@ func `==`*[T](x, y: Complex[T]): bool =
func `+`*[T](x: T; y: Complex[T]): Complex[T] =
## Adds a real number to a complex number.
result.re = x + y.re
result.im = y.im
result = Complex[T](re: x + y.re, im: y.im)
func `+`*[T](x: Complex[T]; y: T): Complex[T] =
## Adds a complex number to a real number.
result.re = x.re + y
result.im = x.im
result = Complex[T](re: x.re + y, im: x.im)
func `+`*[T](x, y: Complex[T]): Complex[T] =
## Adds two complex numbers.
result.re = x.re + y.re
result.im = x.im + y.im
result = Complex[T](re: x.re + y.re, im: x.im + y.im)
func `-`*[T](z: Complex[T]): Complex[T] =
## Unary minus for complex numbers.
result.re = -z.re
result.im = -z.im
result = Complex[T](re: -z.re, im: -z.im)
func `-`*[T](x: T; y: Complex[T]): Complex[T] =
## Subtracts a complex number from a real number.
result.re = x - y.re
result.im = -y.im
result = Complex[T](re: x - y.re, im: -y.im)
func `-`*[T](x: Complex[T]; y: T): Complex[T] =
## Subtracts a real number from a complex number.
result.re = x.re - y
result.im = x.im
result = Complex[T](re: x.re - y, im: x.im)
func `-`*[T](x, y: Complex[T]): Complex[T] =
## Subtracts two complex numbers.
result.re = x.re - y.re
result.im = x.im - y.im
result = Complex[T](re: x.re - y.re, im: x.im - y.im)
func `*`*[T](x: T; y: Complex[T]): Complex[T] =
## Multiplies a real number with a complex number.
result.re = x * y.re
result.im = x * y.im
result = Complex[T](re: x * y.re, im: x * y.im)
func `*`*[T](x: Complex[T]; y: T): Complex[T] =
## Multiplies a complex number with a real number.
result.re = x.re * y
result.im = x.im * y
result = Complex[T](re: x.re * y, im: x.im * y)
func `*`*[T](x, y: Complex[T]): Complex[T] =
## Multiplies two complex numbers.
result.re = x.re * y.re - x.im * y.im
result.im = x.im * y.re + x.re * y.im
result = Complex[T](re: x.re * y.re - x.im * y.im,
im: x.im * y.re + x.re * y.im)
func `/`*[T](x: Complex[T]; y: T): Complex[T] =
## Divides a complex number by a real number.
result.re = x.re / y
result.im = x.im / y
result = Complex[T](re: x.re / y, im: x.im / y)
func `/`*[T](x: T; y: Complex[T]): Complex[T] =
## Divides a real number by a complex number.
@@ -202,10 +190,9 @@ func sqrt*[T](z: Complex[T]): Complex[T] =
w = sqrt(y) * sqrt(0.5 * (r + sqrt(1.0 + r * r)))
if z.re >= 0.0:
result.re = w
result.im = z.im / (w * 2.0)
result = Complex[T](re: w, im: z.im / (w * 2.0))
else:
result.im = if z.im >= 0.0: w else: -w
result = Complex[T](im: if z.im >= 0.0: w else: -w)
result.re = z.im / (result.im + result.im)
func exp*[T](z: Complex[T]): Complex[T] =
@@ -213,15 +200,13 @@ func exp*[T](z: Complex[T]): Complex[T] =
let
rho = exp(z.re)
theta = z.im
result.re = rho * cos(theta)
result.im = rho * sin(theta)
result = Complex[T](re: rho * cos(theta), im: rho * sin(theta))
func ln*[T](z: Complex[T]): Complex[T] =
## Returns the
## ([principal value](https://en.wikipedia.org/wiki/Complex_logarithm#Principal_value)
## of the) natural logarithm of `z`.
result.re = ln(abs(z))
result.im = arctan2(z.im, z.re)
result = Complex[T](re: ln(abs(z)), im: arctan2(z.im, z.re))
func log10*[T](z: Complex[T]): Complex[T] =
## Returns the logarithm base 10 of `z`.
@@ -241,11 +226,9 @@ func pow*[T](x, y: Complex[T]): Complex[T] =
## `x` raised to the power of `y`.
if x.re == 0.0 and x.im == 0.0:
if y.re == 0.0 and y.im == 0.0:
result.re = 1.0
result.im = 0.0
result = Complex[T](re: 1.0, im: 0.0)
else:
result.re = 0.0
result.im = 0.0
result = Complex[T](re: 0.0, im: 0.0)
elif y.im == 0.0:
if y.re == 1.0:
result = x
@@ -257,8 +240,7 @@ func pow*[T](x, y: Complex[T]): Complex[T] =
result = sqrt(x)
elif x.im == 0.0:
# Revert to real pow when both base and exponent are real
result.re = pow(x.re, y.re)
result.im = 0.0
result = Complex[T](re: pow(x.re, y.re), im: 0.0)
else:
# Special case when the exponent is real
let
@@ -266,8 +248,7 @@ func pow*[T](x, y: Complex[T]): Complex[T] =
theta = arctan2(x.im, x.re)
s = pow(rho, y.re)
r = y.re * theta
result.re = s * cos(r)
result.im = s * sin(r)
result = Complex[T](re: s * cos(r), im: s * sin(r))
elif x.im == 0.0 and x.re == E:
# Special case Euler's formula
result = exp(y)
@@ -277,18 +258,15 @@ func pow*[T](x, y: Complex[T]): Complex[T] =
theta = arctan2(x.im, x.re)
s = pow(rho, y.re) * exp(-y.im * theta)
r = y.re * theta + y.im * ln(rho)
result.re = s * cos(r)
result.im = s * sin(r)
result = Complex[T](re: s * cos(r), im: s * sin(r))
func pow*[T](x: Complex[T]; y: T): Complex[T] =
## The complex number `x` raised to the power of the real number `y`.
pow(x, complex[T](y))
func sin*[T](z: Complex[T]): Complex[T] =
## Returns the sine of `z`.
result.re = sin(z.re) * cosh(z.im)
result.im = cos(z.re) * sinh(z.im)
result = Complex[T](re: sin(z.re) * cosh(z.im), im: cos(z.re) * sinh(z.im))
func arcsin*[T](z: Complex[T]): Complex[T] =
## Returns the inverse sine of `z`.
@@ -296,8 +274,9 @@ func arcsin*[T](z: Complex[T]): Complex[T] =
func cos*[T](z: Complex[T]): Complex[T] =
## Returns the cosine of `z`.
result.re = cos(z.re) * cosh(z.im)
result.im = -sin(z.re) * sinh(z.im)
result = Complex[T](re: cos(z.re) * cosh(z.im),
im: -sin(z.re) * sinh(z.im)
)
func arccos*[T](z: Complex[T]): Complex[T] =
## Returns the inverse cosine of `z`.

View File

@@ -1211,7 +1211,7 @@ proc request*(client: HttpClient | AsyncHttpClient, url: Uri | string,
redirectBody = body
else:
# Unreachable
doAssert(false)
raiseAssert "unreachable"
# Check if the redirection is to the same domain or a sub-domain (foo.com
# -> sub.foo.com)

View File

@@ -1253,7 +1253,7 @@ proc foldObjectBody(dst, typeNode, tmpSym, jsonNode, jsonPath, originalJsonPathL
when nimvm:
when isRefSkipDistinct(`tmpSym`.`fieldSym`):
# workaround #12489
var tmp: `fieldType`
var tmp: `fieldType` = default(typeof(`fieldType`))
initFromJson(tmp, getOrDefault(`jsonNode`,`fieldNameLit`), `jsonPath`)
`tmpSym`.`fieldSym` = tmp
else:
@@ -1269,7 +1269,7 @@ proc foldObjectBody(dst, typeNode, tmpSym, jsonNode, jsonPath, originalJsonPathL
let kindType = typeNode[0][1]
let kindOffsetLit = newLit(uint(getOffset(kindSym)))
dst.add quote do:
var kindTmp: `kindType`
var kindTmp: `kindType` = default(typeof(`kindType`))
jsonPath.add `kindPathLit`
initFromJson(kindTmp, `jsonNode`[`kindNameLit`], `jsonPath`)
jsonPath.setLen `originalJsonPathLen`

View File

@@ -312,7 +312,7 @@ proc store*[T](s: Stream, data: sink T) =
storeAny(s, toAny(d), stored)
proc loadVM[T](typ: typedesc[T], x: T): string =
discard "the implementation is in the compiler/vmops"
raiseAssert "the implementation is in the compiler/vmops"
proc `$$`*[T](x: sink T): string =
## Returns a string representation of `x` (serialization, marshalling).
@@ -343,7 +343,7 @@ proc `$$`*[T](x: sink T): string =
result = s.data
proc toVM[T](typ: typedesc[T], data: string): T =
discard "the implementation is in the compiler/vmops"
raiseAssert "the implementation is in the compiler/vmops"
proc to*[T](data: string): T =
## Reads data and transforms it to a type `T` (deserialization, unmarshalling).
@@ -363,5 +363,6 @@ proc to*[T](data: string): T =
when nimvm:
result = toVM(T, data)
else:
result = default(T)
var tab = initTable[BiggestInt, pointer]()
loadAny(newStringStream(data), toAny(result), tab)

View File

@@ -989,8 +989,9 @@ func frexp*[T: float32|float64](x: T): tuple[frac: T, exp: int] {.inline.} =
doAssert frexp(Inf).frac == Inf # +- Inf preserved
doAssert frexp(NaN).frac.isNaN
result = default(tuple[frac: T, exp: int])
when not defined(js):
var exp: cint
var exp: cint = cint(0)
result.frac = c_frexp2(x, exp)
result.exp = exp
else:
@@ -1068,10 +1069,8 @@ func splitDecimal*[T: float32|float64](x: T): tuple[intpart: T, floatpart: T] =
runnableExamples:
doAssert splitDecimal(5.25) == (intpart: 5.0, floatpart: 0.25)
doAssert splitDecimal(-2.73) == (intpart: -2.0, floatpart: -0.73)
var
absolute: T
absolute = abs(x)
result = default(tuple[intpart: T, floatpart: T])
var absolute: T = abs(x)
result.intpart = floor(absolute)
result.floatpart = absolute - result.intpart
if x < 0:
@@ -1128,7 +1127,7 @@ func sum*[T](x: openArray[T]): T =
runnableExamples:
doAssert sum([1, 2, 3, 4]) == 10
doAssert sum([-4, 3, 5]) == 4
result = default(T)
for i in items(x): result = result + i
func prod*[T](x: openArray[T]): T =
@@ -1156,7 +1155,7 @@ func cumsummed*[T](x: openArray[T]): seq[T] =
## * `cumsum func <#cumsum,openArray[T]>`_ for the in-place version
runnableExamples:
doAssert cumsummed([1, 2, 3, 4]) == @[1, 3, 6, 10]
result = @[]
let xLen = x.len
if xLen == 0:
return @[]

View File

@@ -182,6 +182,7 @@ proc parseHex*[T: SomeInteger](s: openArray[char], number: var T, maxLen = 0): i
var num64: int64
doAssert parseHex("4E69ED4E69ED", num64) == 12
doAssert num64 == 86216859871725
result = 0
var i = 0
var output = T(0)
var foundDigit = false

View File

@@ -563,6 +563,8 @@ template matchOrParse(mopProc: untyped) =
# are provided which just return *discard*.
proc mopProc(s: string, p: Peg, start: int, c: var Captures): int {.gcsafe, raises: [].} =
result = 0
proc matchBackRef(s: string, p: Peg, start: int, c: var Captures): int =
# Parse handler code must run in an *of* clause of its own for each
# *PegKind*, so we encapsulate the identical clause body for
@@ -579,7 +581,7 @@ template matchOrParse(mopProc: untyped) =
n = Peg(kind: pkTerminalIgnoreStyle, term: s.substr(a, b))
of pkBackRefIgnoreCase:
n = Peg(kind: pkTerminalIgnoreCase, term: s.substr(a, b))
else: assert(false, "impossible case")
else: raiseAssert "impossible case"
mopProc(s, n, start, c)
case p.kind
@@ -695,7 +697,7 @@ template matchOrParse(mopProc: untyped) =
enter(pkTerminalIgnoreStyle, s, p, start)
var
i = 0
a, b: Rune
a, b: Rune = default(Rune)
result = start
while i < len(p.term):
while i < len(p.term):
@@ -1067,7 +1069,7 @@ template eventParser*(pegAst, handlers: untyped): (proc(s: string): int) =
proc parser(s: string): int {.gensym.} =
# the proc to be returned
var
ms: array[MaxSubpatterns, (int, int)]
ms: array[MaxSubpatterns, (int, int)] = default(array[MaxSubpatterns, (int, int)])
cs = Captures(matches: ms, ml: 0, origStart: 0)
rawParse(s, pegAst, 0, cs)
parser

View File

@@ -228,6 +228,7 @@ proc skipRandomNumbers*(s: var Rand) =
proc rand[T: uint | uint64](r: var Rand; max: T): T =
# xxx export in future work
result = default(T)
if max == 0: return
else:
let max = uint64(max)

View File

@@ -57,8 +57,7 @@ func initRational*[T: SomeInteger](num, den: T): Rational[T] =
##
## **Note:** `den != 0` is not checked when assertions are turned off.
assert(den != 0, "a denominator of zero is invalid")
result.num = num
result.den = den
result = Rational[T](num: num, den: den)
reduce(result)
func `//`*[T](num, den: T): Rational[T] =
@@ -80,9 +79,7 @@ func toRational*[T: SomeInteger](x: T): Rational[T] =
## Converts some integer `x` to a rational number.
runnableExamples:
doAssert toRational(42) == 42 // 1
result.num = x
result.den = 1
result = Rational[T](num: x, den: 1)
func toRational*(x: float,
n: int = high(int) shr (sizeof(int) div 2 * 8)): Rational[int] =
@@ -125,19 +122,18 @@ func toInt*[T](x: Rational[T]): int =
func `+`*[T](x, y: Rational[T]): Rational[T] =
## Adds two rational numbers.
let common = lcm(x.den, y.den)
result.num = common div x.den * x.num + common div y.den * y.num
result.den = common
result = Rational[T](num: common div x.den * x.num + common div y.den * y.num,
den: common
)
reduce(result)
func `+`*[T](x: Rational[T], y: T): Rational[T] =
## Adds the rational `x` to the int `y`.
result.num = x.num + y * x.den
result.den = x.den
result = Rational[T](num: x.num + y * x.den, den: x.den)
func `+`*[T](x: T, y: Rational[T]): Rational[T] =
## Adds the int `x` to the rational `y`.
result.num = x * y.den + y.num
result.den = y.den
result = Rational[T](num: x * y.den + y.num, den: y.den)
func `+=`*[T](x: var Rational[T], y: Rational[T]) =
## Adds the rational `y` to the rational `x` in-place.
@@ -152,25 +148,23 @@ func `+=`*[T](x: var Rational[T], y: T) =
func `-`*[T](x: Rational[T]): Rational[T] =
## Unary minus for rational numbers.
result.num = -x.num
result.den = x.den
result = Rational[T](num: -x.num, den: x.den)
func `-`*[T](x, y: Rational[T]): Rational[T] =
## Subtracts two rational numbers.
let common = lcm(x.den, y.den)
result.num = common div x.den * x.num - common div y.den * y.num
result.den = common
result = Rational[T](num: common div x.den * x.num - common div y.den * y.num,
den: common
)
reduce(result)
func `-`*[T](x: Rational[T], y: T): Rational[T] =
## Subtracts the int `y` from the rational `x`.
result.num = x.num - y * x.den
result.den = x.den
result = Rational[T](num: x.num - y * x.den, den: x.den)
func `-`*[T](x: T, y: Rational[T]): Rational[T] =
## Subtracts the rational `y` from the int `x`.
result.num = x * y.den - y.num
result.den = y.den
result = Rational[T](num: x * y.den - y.num, den: y.den)
func `-=`*[T](x: var Rational[T], y: Rational[T]) =
## Subtracts the rational `y` from the rational `x` in-place.
@@ -185,20 +179,17 @@ func `-=`*[T](x: var Rational[T], y: T) =
func `*`*[T](x, y: Rational[T]): Rational[T] =
## Multiplies two rational numbers.
result.num = x.num * y.num
result.den = x.den * y.den
result = Rational[T](num: x.num * y.num, den: x.den * y.den)
reduce(result)
func `*`*[T](x: Rational[T], y: T): Rational[T] =
## Multiplies the rational `x` with the int `y`.
result.num = x.num * y
result.den = x.den
result = Rational[T](num: x.num * y, den: x.den)
reduce(result)
func `*`*[T](x: T, y: Rational[T]): Rational[T] =
## Multiplies the int `x` with the rational `y`.
result.num = x * y.num
result.den = y.den
result = Rational[T](num: x * y.num, den: y.den)
reduce(result)
func `*=`*[T](x: var Rational[T], y: Rational[T]) =
@@ -216,30 +207,25 @@ func reciprocal*[T](x: Rational[T]): Rational[T] =
## Calculates the reciprocal of `x` (`1/x`).
## If `x` is 0, raises `DivByZeroDefect`.
if x.num > 0:
result.num = x.den
result.den = x.num
result = Rational[T](num: x.den, den: x.num)
elif x.num < 0:
result.num = -x.den
result.den = -x.num
result = Rational[T](num: -x.den, den: -x.num)
else:
raise newException(DivByZeroDefect, "division by zero")
func `/`*[T](x, y: Rational[T]): Rational[T] =
## Divides the rational `x` by the rational `y`.
result.num = x.num * y.den
result.den = x.den * y.num
result = Rational[T](num: x.num * y.den, den: x.den * y.num)
reduce(result)
func `/`*[T](x: Rational[T], y: T): Rational[T] =
## Divides the rational `x` by the int `y`.
result.num = x.num
result.den = x.den * y
result = Rational[T](num: x.num, den: x.den * y)
reduce(result)
func `/`*[T](x: T, y: Rational[T]): Rational[T] =
## Divides the int `x` by the rational `y`.
result.num = x * y.den
result.den = y.num
result = Rational[T](num: x * y.den, den: y.num)
reduce(result)
func `/=`*[T](x: var Rational[T], y: Rational[T]) =
@@ -277,9 +263,7 @@ func abs*[T](x: Rational[T]): Rational[T] =
runnableExamples:
doAssert abs(1 // 2) == 1 // 2
doAssert abs(-1 // 2) == 1 // 2
result.num = abs x.num
result.den = abs x.den
result = Rational[T](num: abs x.num, den: abs x.den)
func `div`*[T: SomeInteger](x, y: Rational[T]): T =
## Computes the rational truncated division.
@@ -311,6 +295,7 @@ func floorMod*[T: SomeInteger](x, y: Rational[T]): Rational[T] =
func hash*[T](x: Rational[T]): Hash =
## Computes the hash for the rational `x`.
# reduce first so that hash(x) == hash(y) for x == y
result = Hash(0)
var copy = x
reduce(copy)
@@ -331,10 +316,8 @@ func `^`*[T: SomeInteger](x: Rational[T], y: T): Rational[T] =
doAssert (-3 // 5) ^ -2 == (25 // 9)
if y >= 0:
result.num = x.num ^ y
result.den = x.den ^ y
result = Rational[T](num: x.num ^ y, den: x.den ^ y)
else:
result.num = x.den ^ -y
result.den = x.num ^ -y
result = Rational[T](num: x.den ^ -y, den: x.num ^ -y)
# Note that all powers of reduced rationals are already reduced,
# so we don't need to call reduce() here

View File

@@ -336,7 +336,7 @@ else:
elif defined(zephyr) or defined(freertos):
FD_MAX
else:
var fdLim: RLimit
var fdLim: RLimit = default(RLimit)
var res = int(getrlimit(RLIMIT_NOFILE, fdLim))
if res >= 0:
res = int(fdLim.rlim_cur) - 1

View File

@@ -665,9 +665,7 @@ macro check*(conditions: untyped): untyped =
checkpoint(name & " was " & $value)
proc inspectArgs(exp: NimNode): tuple[assigns, check, printOuts: NimNode] =
result.check = copyNimTree(exp)
result.assigns = newNimNode(nnkStmtList)
result.printOuts = newNimNode(nnkStmtList)
result = (newNimNode(nnkStmtList), copyNimTree(exp), newNimNode(nnkStmtList))
var counter = 0

View File

@@ -140,4 +140,5 @@ proc addFloat*(result: var string; x: float | float32) {.inline.} =
when defined(nimPreviewSlimSystem):
func `$`*(x: float | float32): string =
## Outplace version of `addFloat`.
result = ""
result.addFloat(x)

View File

@@ -186,7 +186,7 @@ proc discKeyMatch[T](obj: T, json: JsonNode, key: static string): bool =
if not json.hasKey key:
return true
let field = accessField(obj, key)
var jsonVal: typeof(field)
var jsonVal: typeof(field) = default(typeof(field))
fromJson(jsonVal, json[key])
if jsonVal != field:
return false
@@ -293,6 +293,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
proc jsonTo*(b: JsonNode, T: typedesc, opt = Joptions()): T =
## reverse of `toJson`
result = default(T)
fromJson(result, b, opt)
proc toJson*[T](a: T, opt = initToJsonOptions()): JsonNode =

View File

@@ -462,7 +462,7 @@ proc union*[A](s1, s2: PackedSet[A]): PackedSet[A] =
c = union(a, b)
assert c.len == 5
assert c == [1, 2, 3, 4, 5].toPackedSet
result = default(PackedSet[A])
result.assign(s1)
incl(result, s2)
@@ -509,7 +509,7 @@ proc symmetricDifference*[A](s1, s2: PackedSet[A]): PackedSet[A] =
c = symmetricDifference(a, b)
assert c.len == 4
assert c == [1, 2, 4, 5].toPackedSet
result = default(PackedSet[A])
result.assign(s1)
for item in s2.items:
if containsOrIncl(result, item):

View File

@@ -63,7 +63,7 @@ proc threadFunc(obj: ptr seq[int]) {.thread.} =
proc threadHandler() =
var thr: array[0..4, Thread[ptr seq[int]]]
var s = newSeq[int]()
for i in 0..high(thr):
createThread(thr[i], threadFunc, s.addr)
joinThreads(thr)
@@ -137,6 +137,7 @@ when defined(zephyr):
{.push stack_trace:off.}
when defined(windows):
proc threadProcWrapper[TArg](closure: pointer): int32 {.stdcall.} =
result = 0'i32
nimThreadProcWrapperBody(closure)
# implicitly return 0
elif defined(genode):
@@ -144,6 +145,7 @@ elif defined(genode):
nimThreadProcWrapperBody(closure)
else:
proc threadProcWrapper[TArg](closure: pointer): pointer {.noconv.} =
result = nil
nimThreadProcWrapperBody(closure)
{.pop.}
@@ -164,7 +166,7 @@ when hostOS == "windows":
proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
## Waits for every thread in `t` to finish.
var a: array[MAXIMUM_WAIT_OBJECTS, SysThread]
var a: array[MAXIMUM_WAIT_OBJECTS, SysThread] = default(array[MAXIMUM_WAIT_OBJECTS, SysThread])
var k = 0
while k < len(t):
var count = min(len(t) - k, MAXIMUM_WAIT_OBJECTS)
@@ -220,7 +222,7 @@ when hostOS == "windows":
when TArg isnot void: t.data = param
t.dataFn = tp
when hasSharedHeap: t.core.stackSize = ThreadStackSize
var dummyThreadId: int32
var dummyThreadId: int32 = 0'i32
t.sys = createThread(nil, ThreadStackSize, threadProcWrapper[TArg],
addr(t), 0'i32, dummyThreadId)
if t.sys <= 0:

View File

@@ -58,7 +58,7 @@ proc finalize(n: NimNode, lhs: NimNode, level: int): NimNode =
if level == 0:
result = quote: `lhs` = `n`
else:
result = quote: (let `lhs` = `n`)
result = quote: (var `lhs` = `n`)
proc process(n: NimNode, lhs: NimNode, label: NimNode, level: int): NimNode =
var n = n.copyNimTree
@@ -77,7 +77,7 @@ proc process(n: NimNode, lhs: NimNode, label: NimNode, level: int): NimNode =
let check = it[1]
let okSet = check[1]
let kind1 = check[2]
let tmp = genSym(nskLet, "tmpCase")
let tmp = genSym(nskVar, "tmpCase")
let body = process(objRef, tmp, label, level + 1)
let tmp3 = nnkDerefExpr.newTree(tmp)
it[0][0] = tmp3
@@ -91,7 +91,7 @@ proc process(n: NimNode, lhs: NimNode, label: NimNode, level: int): NimNode =
`assgn`
break
elif it.kind in {nnkHiddenDeref, nnkDerefExpr}:
let tmp = genSym(nskLet, "tmp")
let tmp = genSym(nskVar, "tmp")
let body = process(it[0], tmp, label, level + 1)
it[0] = tmp
let assgn = finalize(n, lhs, level)
@@ -116,7 +116,7 @@ macro `?.`*(a: typed): auto =
let label = genSym(nskLabel, "label")
let body = process(a, lhs, label, 0)
result = quote do:
var `lhs`: type(`a`)
var `lhs`: type(`a`) = default(type(`a`))
block `label`:
`body`
`lhs`
@@ -148,9 +148,9 @@ macro `??.`*(a: typed): Option =
let label = genSym(nskLabel, "label")
let body = process(a, lhs2, label, 0)
result = quote do:
var `lhs`: Option[type(`a`)]
var `lhs`: Option[type(`a`)] = default(Option[type(`a`)])
block `label`:
var `lhs2`: type(`a`)
var `lhs2`: type(`a`) = default(type(`a`))
`body`
`lhs` = option(`lhs2`)
`lhs`

View File

@@ -2357,7 +2357,7 @@ when notJSnotNims:
else:
let c3 = cast[proc(y: int; env: pointer): int {.nimcall.}](p)
echo c3(3, e)
result = nil
{.emit: """
`result` = (void*)`x`.ClP_0;
""".}
@@ -2365,12 +2365,14 @@ when notJSnotNims:
proc rawEnv*[T: proc {.closure.} | iterator {.closure.}](x: T): pointer {.noSideEffect, inline.} =
## Retrieves the raw environment pointer of the closure `x`. See also `rawProc`.
## This is not available for the JS target.
result = nil
{.emit: """
`result` = `x`.ClE_0;
""".}
proc finished*[T: iterator {.closure.}](x: T): bool {.noSideEffect, inline, magic: "Finished".} =
## It can be used to determine if a first class iterator has finished.
result = false
when defined(js):
# TODO: mangle `:state`
{.emit: """
@@ -2965,10 +2967,12 @@ when notJSnotNims and not defined(nimSeqsV2):
proc arrayWith*[T](y: T, size: static int): array[size, T] {.raises: [].} =
## Creates a new array filled with `y`.
result = zeroDefault(array[size, T])
for i in 0..size-1:
result[i] = y
proc arrayWithDefault*[T](size: static int): array[size, T] {.raises: [].} =
## Creates a new array filled with `default(T)`.
result = zeroDefault(array[size, T])
for i in 0..size-1:
result[i] = default(T)

View File

@@ -68,7 +68,7 @@ else:
when defined(nimV2):
thrd.dataFn(thrd.data)
else:
var x: TArg
var x: TArg = default(TArg)
deepCopy(x, thrd.data)
thrd.dataFn(x)
except: