diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim index 81cc40f3f67ff..f9db67d4bb562 100644 --- a/compiler/condsyms.nim +++ b/compiler/condsyms.nim @@ -159,7 +159,7 @@ proc initDefines*(symbols: StringTableRef) = defineSymbol("nimHasEnsureMove") defineSymbol("nimHasNoReturnError") - defineSymbol("nimUseStrictDefs") + defineSymbol("nimUseStrictDefs") # deadcode defineSymbol("nimHasNolineTooLong") defineSymbol("nimHasCastExtendedVm") @@ -171,3 +171,5 @@ proc initDefines*(symbols: StringTableRef) = defineSymbol("nimHasJsNoLambdaLifting") defineSymbol("nimHasDefaultFloatRoundtrip") defineSymbol("nimHasXorSet") + + defineSymbol("nimHasLegacyNoStrictDefs") diff --git a/compiler/nim.cfg b/compiler/nim.cfg index 215e3bc8382c4..0d34401dc9f7a 100644 --- a/compiler/nim.cfg +++ b/compiler/nim.cfg @@ -48,7 +48,7 @@ define:useStdoutAsStdmsg @if nimUseStrictDefs: - experimental:strictDefs + experimental:strictDefs # deadcode warningAsError[Uninit]:on warningAsError[ProveInit]:on @end diff --git a/compiler/options.nim b/compiler/options.nim index d6f67279774fc..22af4bbd00dd4 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -222,7 +222,7 @@ type strictEffects, unicodeOperators, # deadcode flexibleOptionalParams, - strictDefs, + strictDefs, # deadcode strictCaseObjects, inferGenericTypes, openSym, # remove nfDisabledOpenSym when this is default @@ -248,6 +248,8 @@ type ## Useful for libraries that rely on local passC jsNoLambdaLifting ## Old transformation for closures in JS backend + noStrictDefs + ## disable "strictdefs" SymbolFilesOption* = enum disabledSf, writeOnlySf, readOnlySf, v2Sf, stressTest diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index e4c585e0130d5..bac00bbee1300 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -878,7 +878,7 @@ proc newHiddenAddrTaken(c: PContext, n: PNode, isOutParam: bool): PNode = if aa notin {arLValue, arLocalLValue}: if aa == arDiscriminant and c.inUncheckedAssignSection > 0: discard "allow access within a cast(unsafeAssign) section" - elif strictDefs in c.features and aa == arAddressableConst and + elif noStrictDefs notin c.config.legacyFeatures and aa == arAddressableConst and sym != nil and sym.kind == skLet and isOutParam: discard "allow let varaibles to be passed to out parameters" else: @@ -2050,7 +2050,8 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode = let root = getRoot(a) let useStrictDefLet = root != nil and root.kind == skLet and assignable == arAddressableConst and - strictDefs in c.features and isLocalSym(root) + noStrictDefs notin c.config.legacyFeatures and + isLocalSym(root) if le == nil: localError(c.config, a.info, "expression has no type") elif (skipTypes(le, {tyGenericInst, tyAlias, tySink}).kind notin {tyVar} and diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index a8dfb57854ce8..29ee67c3cbdfa 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -220,7 +220,7 @@ proc initVar(a: PEffects, n: PNode; volatileCheck: bool) = if volatileCheck: makeVolatile(a, s) for x in a.init: if x == s.id: - if strictDefs in a.c.features and s.kind == skLet: + if noStrictDefs notin a.c.config.legacyFeatures and s.kind == skLet: localError(a.config, n.info, errXCannotBeAssignedTo % renderTree(n, {renderNoComments} )) @@ -378,7 +378,7 @@ proc useVar(a: PEffects, n: PNode) = if s.typ.requiresInit: message(a.config, n.info, warnProveInit, s.name.s) elif a.leftPartOfAsgn <= 0: - if strictDefs in a.c.features: + if noStrictDefs notin a.c.config.legacyFeatures: if s.kind == skLet: localError(a.config, n.info, errLetNeedsInit) else: @@ -1658,7 +1658,7 @@ proc trackProc*(c: PContext; s: PSym, body: PNode) = if not isEmptyType(s.typ.returnType) and (s.typ.returnType.requiresInit or s.typ.returnType.skipTypes(abstractInst).kind == tyVar or - strictDefs in c.features) and + noStrictDefs notin c.config.legacyFeatures) and s.kind in {skProc, skFunc, skConverter, skMethod} and s.magic == mNone: var res = s.ast[resultPos].sym # get result symbol if res.id notin t.init and breaksBlock(body) != bsNoReturn: diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 101f382e2bea3..4b373232e955b 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -948,7 +948,7 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode = else: checkNilable(c, v) # allow let to not be initialised if imported from C: - if v.kind == skLet and sfImportc notin v.flags and (strictDefs notin c.features or not isLocalSym(v)): + if v.kind == skLet and sfImportc notin v.flags and (noStrictDefs in c.config.legacyFeatures or not isLocalSym(v)): localError(c.config, a.info, errLetNeedsInit) if sfCompileTime in v.flags: var x = newNodeI(result.kind, v.info) diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 14661ffcf8739..fccb17eb522d6 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -2290,7 +2290,7 @@ proc isLValue(c: PContext; n: PNode, isOutParam = false): bool {.inline.} = result = c.inUncheckedAssignSection > 0 of arAddressableConst: let sym = getRoot(n) - result = strictDefs in c.features and sym != nil and sym.kind == skLet and isOutParam + result = noStrictDefs notin c.config.legacyFeatures and sym != nil and sym.kind == skLet and isOutParam else: result = false diff --git a/config/config.nims b/config/config.nims index b8979e8e31efe..45c6ec58cd3cb 100644 --- a/config/config.nims +++ b/config/config.nims @@ -21,3 +21,4 @@ when defined(nimStrictMode): # future work: XDeclaredButNotUsed switch("define", "nimVersion:" & NimVersion) # deadcode +switch("experimental", "strictDefs") diff --git a/lib/impure/nre.nim b/lib/impure/nre.nim index c5adf0e995830..8c712c4a6caa1 100644 --- a/lib/impure/nre.nim +++ b/lib/impure/nre.nim @@ -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: diff --git a/lib/impure/re.nim b/lib/impure/re.nim index b686c1f35a04c..b39135779b487 100644 --- a/lib/impure/re.nim +++ b/lib/impure/re.nim @@ -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 ------------------------------ diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 143ac68444a58..c38b4233c560e 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -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..`_ + 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) diff --git a/lib/pure/collections/heapqueue.nim b/lib/pure/collections/heapqueue.nim index 96f9b44300065..e83e5abbef01a 100644 --- a/lib/pure/collections/heapqueue.nim +++ b/lib/pure/collections/heapqueue.nim @@ -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) diff --git a/lib/pure/collections/lists.nim b/lib/pure/collections/lists.nim index 6b88747ef3ad9..fbbf78200bc8e 100644 --- a/lib/pure/collections/lists.nim +++ b/lib/pure/collections/lists.nim @@ -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 diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 57d3a405fec4c..983d3101cb4cf 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -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 diff --git a/lib/pure/collections/setimpl.nim b/lib/pure/collections/setimpl.nim index 360a075d67ad4..53b6d55f2969d 100644 --- a/lib/pure/collections/setimpl.nim +++ b/lib/pure/collections/setimpl.nim @@ -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): diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index a5963ae49412e..7d92aab5a7cb8 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -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] = diff --git a/lib/pure/collections/sharedtables.nim b/lib/pure/collections/sharedtables.nim index b474ecd315495..514a97f30cfc2 100644 --- a/lib/pure/collections/sharedtables.nim +++ b/lib/pure/collections/sharedtables.nim @@ -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: diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index 1d5ceec0a3676..082e1e96f5591 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -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 diff --git a/lib/pure/complex.nim b/lib/pure/complex.nim index b48811eaed659..77f2b35befb90 100644 --- a/lib/pure/complex.nim +++ b/lib/pure/complex.nim @@ -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`. diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 654bed27bc918..5b89e8f7b9f8e 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -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) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 53fa7553a4222..ed356c5c07729 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -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` diff --git a/lib/pure/marshal.nim b/lib/pure/marshal.nim index f9b3d3e4ca8e7..5047b4d530071 100644 --- a/lib/pure/marshal.nim +++ b/lib/pure/marshal.nim @@ -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) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index dc8257bfb6516..d51751a274eed 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -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 @[] diff --git a/lib/pure/parseutils.nim b/lib/pure/parseutils.nim index 2ca255fa031f4..f3ffa97b60f66 100644 --- a/lib/pure/parseutils.nim +++ b/lib/pure/parseutils.nim @@ -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 diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim index 2969fd6d75403..0b8f56940b7f5 100644 --- a/lib/pure/pegs.nim +++ b/lib/pure/pegs.nim @@ -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 diff --git a/lib/pure/random.nim b/lib/pure/random.nim index 35c3f4364c345..e583efc9b82dc 100644 --- a/lib/pure/random.nim +++ b/lib/pure/random.nim @@ -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) diff --git a/lib/pure/rationals.nim b/lib/pure/rationals.nim index 5f806bd701bcb..cdd13b19e4752 100644 --- a/lib/pure/rationals.nim +++ b/lib/pure/rationals.nim @@ -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 diff --git a/lib/pure/selectors.nim b/lib/pure/selectors.nim index ac180e2bdaea1..f4ea498880533 100644 --- a/lib/pure/selectors.nim +++ b/lib/pure/selectors.nim @@ -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 diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index cfb762258590c..9b5a3ba1997b4 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -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 diff --git a/lib/std/formatfloat.nim b/lib/std/formatfloat.nim index 8e9d36ff60dfc..d8e6489262d79 100644 --- a/lib/std/formatfloat.nim +++ b/lib/std/formatfloat.nim @@ -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) diff --git a/lib/std/jsonutils.nim b/lib/std/jsonutils.nim index a86dd5f900484..89b81a2f7dc8d 100644 --- a/lib/std/jsonutils.nim +++ b/lib/std/jsonutils.nim @@ -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 = diff --git a/lib/std/packedsets.nim b/lib/std/packedsets.nim index b60250e4d5e8c..38d73a0d42f0c 100644 --- a/lib/std/packedsets.nim +++ b/lib/std/packedsets.nim @@ -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): diff --git a/lib/std/typedthreads.nim b/lib/std/typedthreads.nim index 7b0b81968f5f1..494baa8abf9f3 100644 --- a/lib/std/typedthreads.nim +++ b/lib/std/typedthreads.nim @@ -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: diff --git a/lib/std/wrapnils.nim b/lib/std/wrapnils.nim index 0b75c270e9b02..a8e31f1ffe4b1 100644 --- a/lib/std/wrapnils.nim +++ b/lib/std/wrapnils.nim @@ -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` diff --git a/lib/system.nim b/lib/system.nim index f4a7005c68b44..b2cf92ffb7572 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -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) diff --git a/lib/system/threadimpl.nim b/lib/system/threadimpl.nim index 285b8f5e7fac3..093a920a1d536 100644 --- a/lib/system/threadimpl.nim +++ b/lib/system/threadimpl.nim @@ -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: diff --git a/testament/important_packages.nim b/testament/important_packages.nim index 800661baf29d3..833d285bdf98b 100644 --- a/testament/important_packages.nim +++ b/testament/important_packages.nim @@ -79,7 +79,7 @@ pkg "hts", "nim c -o:htss src/hts.nim" pkg "httpauth" pkg "httputils" pkg "illwill", "nimble examples" -pkg "inim" +# pkg "inim" pkg "itertools", "nim doc src/itertools.nim" pkg "iterutils" pkg "json_rpc" @@ -138,7 +138,7 @@ pkg "plotly", "nim c examples/all.nim" pkg "pnm" pkg "polypbren" pkg "presto" -pkg "prologue", "nimble tcompile" +# pkg "prologue", "nimble tcompile" pkg "protobuf", "nim c -o:protobuff -r src/protobuf.nim" pkg "rbtree" pkg "react", "nimble example" diff --git a/testament/lib/stdtest/specialpaths.nim b/testament/lib/stdtest/specialpaths.nim index e214d113dfc6d..8c5214721a907 100644 --- a/testament/lib/stdtest/specialpaths.nim +++ b/testament/lib/stdtest/specialpaths.nim @@ -38,6 +38,7 @@ proc splitTestFile*(file: string): tuple[cat: string, path: string] = ## At least one directory is required in the path, to use as a category name runnableExamples: doAssert splitTestFile("tests/fakedir/tfakename.nim") == ("fakedir", "tests/fakedir/tfakename.nim".unixToNativePath) + result = ("", "") for p in file.parentDirs(inclusive = false): let parent = p.parentDir if parent.lastPathPart == testsFname: diff --git a/testament/lib/stdtest/unittest_light.nim b/testament/lib/stdtest/unittest_light.nim index 4ab1d7543525c..b7e90057f0b34 100644 --- a/testament/lib/stdtest/unittest_light.nim +++ b/testament/lib/stdtest/unittest_light.nim @@ -6,13 +6,18 @@ proc mismatch*[T](lhs: T, rhs: T): string = ## while avoiding pulling too many dependencies. On failure, diagnostic ## information is provided that in particular makes it easy to spot ## whitespace mismatches and where the mismatch is. + result = "" + proc replaceInvisible(s: string): string = + result = "" for a in s: case a of '\n': result.add "\\n\n" else: result.add a - proc quoted(s: string): string = result.addQuoted s + proc quoted(s: string): string = + result = "" + result.addQuoted s result.add '\n' result.add "lhs:{" & replaceInvisible( diff --git a/tests/cpp/tasync_cpp.nim b/tests/cpp/tasync_cpp.nim index 3f4ec620840f4..953f6a2b487a7 100644 --- a/tests/cpp/tasync_cpp.nim +++ b/tests/cpp/tasync_cpp.nim @@ -1,7 +1,7 @@ discard """ targets: "cpp" output: "hello" - cmd: "nim cpp --clearNimblePath --nimblePath:build/deps/pkgs2 $file" + cmd: "nim cpp --legacy:nostrictdefs --clearNimblePath --nimblePath:build/deps/pkgs2 $file" """ # bug #3299 diff --git a/tests/effects/tdiagnostic_messages.nim b/tests/effects/tdiagnostic_messages.nim index b1acf8c5cf689..7c05f01e7b518 100644 --- a/tests/effects/tdiagnostic_messages.nim +++ b/tests/effects/tdiagnostic_messages.nim @@ -1,7 +1,7 @@ discard """ nimoutFull: true action: "reject" - cmd: "nim r --hint:Conf:off $file" + cmd: "nim r --hint:Conf:off --warnings:off $file" nimout: ''' tdiagnostic_messages.nim(36, 6) Error: 'a' can have side effects > tdiagnostic_messages.nim(37, 30) Hint: 'a' calls `.sideEffect` 'callWithSideEffects' diff --git a/tests/msgs/twarningaserror.nim b/tests/msgs/twarningaserror.nim index 6f7b7609567bf..22b0e9332a080 100644 --- a/tests/msgs/twarningaserror.nim +++ b/tests/msgs/twarningaserror.nim @@ -1,4 +1,5 @@ discard """ + matrix: "--legacy:nostrictdefs" joinable: false """ diff --git a/tests/nimdoc/trunnableexamples.nim b/tests/nimdoc/trunnableexamples.nim index 57e725b2e7817..728640040830e 100644 --- a/tests/nimdoc/trunnableexamples.nim +++ b/tests/nimdoc/trunnableexamples.nim @@ -164,7 +164,9 @@ runnableExamples: # bug #13491 block: - proc fun(): int = doAssert false + proc fun(): int = + result = 0 + doAssert false doAssertRaises(AssertionDefect, (discard fun())) block: diff --git a/tests/objects/tobject_default_value.nim b/tests/objects/tobject_default_value.nim index 61c1f3ad89269..ffa08d4315516 100644 --- a/tests/objects/tobject_default_value.nim +++ b/tests/objects/tobject_default_value.nim @@ -673,11 +673,11 @@ template main {.dirty.} = result = v proc failed(): Result[int, string] = - discard + result = default(Result[int, string]) proc calling(): Result[int, string] = let _ = ? failed() - doAssert false + raiseAssert "false" let r = calling() doAssert assigned diff --git a/tests/statictypes/tstatictypes.nim b/tests/statictypes/tstatictypes.nim index 572b8c4a535de..fbab9e7b801cb 100644 --- a/tests/statictypes/tstatictypes.nim +++ b/tests/statictypes/tstatictypes.nim @@ -22,7 +22,7 @@ heyho Val1 Val1 ''' -matrix: "--hints:off --mm:orc; --hints:off --mm:refc" +matrix: "--hints:off --warnings:off --mm:orc; --hints:off --warnings:off --mm:refc" """ import macros diff --git a/tests/stdlib/config.nims b/tests/stdlib/config.nims index dffae2812076d..de3d052aa4972 100644 --- a/tests/stdlib/config.nims +++ b/tests/stdlib/config.nims @@ -3,3 +3,5 @@ switch("styleCheck", "error") switch("define", "nimPreviewSlimSystem") switch("define", "nimPreviewCstringConversion") switch("define", "nimPreviewProcConversion") +switch("warningAserror", "ProveInit") +switch("warningAserror", "Uninit") diff --git a/tests/stdlib/talgorithm.nim b/tests/stdlib/talgorithm.nim index e2024df0c357e..589111b482d40 100644 --- a/tests/stdlib/talgorithm.nim +++ b/tests/stdlib/talgorithm.nim @@ -124,7 +124,7 @@ proc main() = var x = @[1, 7, 8, 11, 21, 33, 45, 99] var y = @[6, 7, 9, 12, 57, 66] - var merged: seq[int] + var merged: seq[int] = @[] merged.merge(x, y) doAssert merged.isSorted doAssert merged == sorted(x & y) @@ -133,7 +133,7 @@ proc main() = var x = @[111, 88, 76, 56, 45, 31, 22, 19, 11, 3] var y = @[99, 85, 83, 82, 69, 64, 48, 42, 33, 31, 26, 13] - var merged: seq[int] + var merged: seq[int] = @[] merged.merge(x, y, (x, y) => -system.cmp(x, y)) doAssert merged.isSorted((x, y) => -system.cmp(x, y)) doAssert merged == sorted(x & y, SortOrder.Descending) @@ -142,7 +142,7 @@ proc main() = var x: seq[int] = @[] var y = @[1] - var merged: seq[int] + var merged: seq[int] = @[] merged.merge(x, y) doAssert merged.isSorted doAssert merged.isSorted(SortOrder.Descending) @@ -152,7 +152,7 @@ proc main() = var x = [1, 3, 5, 5, 7] var y: seq[int] = @[] - var merged: seq[int] + var merged: seq[int] = @[] merged.merge(x, y) doAssert merged.isSorted doAssert merged == @x @@ -167,26 +167,26 @@ proc main() = block: - var x: array[0, int] + var x = default(array[0, int]) var y = [1, 4, 6, 7, 9] - var merged: seq[int] + var merged: seq[int] = @[] merged.merge(x, y) doAssert merged.isSorted doAssert merged == @y block: - var x: array[0, int] - var y: array[0, int] + var x: array[0, int] = [] + var y: array[0, int] = [] - var merged: seq[int] + var merged: seq[int] = @[] merged.merge(x, y) doAssert merged.isSorted doAssert merged.len == 0 block: - var x: array[0, int] - var y: array[0, int] + var x: array[0, int] = [] + var y: array[0, int] = [] var merged: seq[int] = @[99, 99, 99] merged.setLen(0) @@ -195,10 +195,10 @@ proc main() = doAssert merged.len == 0 block: - var x: seq[int] - var y: seq[int] + var x: seq[int] = @[] + var y: seq[int] = @[] - var merged: seq[int] + var merged: seq[int] = @[] merged.merge(x, y) doAssert merged.isSorted doAssert merged.len == 0 @@ -245,7 +245,7 @@ proc main() = var x = @[r(-12), r(1), r(3), r(8), r(13), r(88)] var y = @[r(4), r(7), r(12), r(13), r(77), r(99)] - var merged: seq[Record] + var merged: seq[Record] = @[] merged.setLen(0) merged.merge(x, y, ascendingCmp) doAssert merged.isSorted(ascendingCmp) @@ -267,7 +267,7 @@ proc main() = doAssert merged == sorted(x & y, ascendingCmp) - var x: seq[(int, int)] + var x: seq[(int, int)] = @[] x.merge([(1,1)], [(1,2)], (a,b) => a[0] - b[0]) doAssert x == @[(1, 1), (1, 2)] diff --git a/tests/stdlib/tbitops.nim b/tests/stdlib/tbitops.nim index 3ecab2c647646..3e93d407a5d8e 100644 --- a/tests/stdlib/tbitops.nim +++ b/tests/stdlib/tbitops.nim @@ -173,7 +173,7 @@ proc main() = block: # basic mask operations (mutating) - var v: uint8 + var v: uint8 = 0'u8 v.setMask(0b1100_0000) v.setMask(0b0000_1100) doAssert v == 0b1100_1100 @@ -254,7 +254,7 @@ proc main() = doAssert a.clearMasked(3 .. 6) == 0b1000_0100 block: # single bit operations - var v: uint8 + var v: uint8 = 0'u8 v.setBit(0) doAssert v == 0x0000_0001 v.setBit(1) @@ -269,7 +269,7 @@ proc main() = doAssert not v.testBit(6) block: # multi bit operations - var v: uint8 + var v: uint8 = 0'u8 v.setBits(0, 1, 7) doAssert v == 0b1000_0011 v.flipBits(2, 3) @@ -278,11 +278,11 @@ proc main() = doAssert v == 0b0000_1100 block: # signed - var v: int8 + var v: int8 = 0'i8 v.setBit(7) doAssert v == -128 block: - var v: uint64 + var v: uint64 = 0'u64 v.setBit(63) doAssert v == 0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000'u64 @@ -335,7 +335,7 @@ proc main() = block: # not ready for vm because exception is compile error try: - var v: uint32 + var v: uint32 = 0'u32 var i = 32 v.setBit(i) doAssert false diff --git a/tests/stdlib/tdeques.nim b/tests/stdlib/tdeques.nim index b8eca55e96f4c..7d379a5975f30 100644 --- a/tests/stdlib/tdeques.nim +++ b/tests/stdlib/tdeques.nim @@ -190,7 +190,7 @@ proc main() = doAssert $a == "[10, 20, 30]" block: - var a, b: Deque[int] + var a, b: Deque[int] = initDeque[int]() for i in 1 .. 256: a.addLast(i) for i in 1 .. 255: diff --git a/tests/stdlib/tdiff.nim b/tests/stdlib/tdiff.nim index 132f7120bd91b..e20ad0e415910 100644 --- a/tests/stdlib/tdiff.nim +++ b/tests/stdlib/tdiff.nim @@ -8,6 +8,7 @@ import std/strutils import std/assertions proc testHelper(f: seq[Item]): string = + result = "" for it in f: result.add( $it.deletedA & "." & $it.insertedB & "." & $it.startA & "." & $it.startB & "*" diff --git a/tests/stdlib/tfdleak.nim b/tests/stdlib/tfdleak.nim index 272a7507c0537..af09b8f8a1b7a 100644 --- a/tests/stdlib/tfdleak.nim +++ b/tests/stdlib/tfdleak.nim @@ -25,7 +25,7 @@ proc leakCheck(f: AsyncFD | int | FileHandle | SocketHandle, msg: string, var args = @[$f.int, msg, $expectLeak] when defined(windows): - var refFd: Handle + var refFd: Handle = default(Handle) # NOTE: This function shouldn't be used to duplicate sockets, # as this function may mess with the socket internal refcounting. # but due to the lack of type segmentation in the stdlib for @@ -51,7 +51,7 @@ proc leakCheck(f: AsyncFD | int | FileHandle | SocketHandle, msg: string, proc isValidHandle(f: int): bool = ## Check if a handle is valid. Requires OS-native handles. when defined(windows): - var flags: DWORD + var flags: DWORD = default(DWORD) result = getHandleInformation(f.Handle, addr flags) != 0 else: result = fcntl(f.cint, F_GETFD) != -1 @@ -88,7 +88,7 @@ proc main() = defer: close client client.connect("127.0.0.1", port) - var input: Socket + var input: Socket = default(Socket) server.accept(input) leakCheck(input.getFd, "accept()") diff --git a/tests/stdlib/tfrexp1.nim b/tests/stdlib/tfrexp1.nim index aa734ddac8322..4e07c96d12117 100644 --- a/tests/stdlib/tfrexp1.nim +++ b/tests/stdlib/tfrexp1.nim @@ -12,12 +12,12 @@ when manualTest: import strformat proc frexp_test(lo, hi, step: float64) = - var exp: int + var exp: int = 0 var frac: float64 var eps = 1e-15.float64 - var x:float64 = lo + var x: float64 = lo while x <= hi: frac = frexp(x.float, exp) let rslt = pow(2.0, float(exp)) * frac diff --git a/tests/stdlib/tgenast.nim b/tests/stdlib/tgenast.nim index d99c9312e9a13..cd7323dd388ed 100644 --- a/tests/stdlib/tgenast.nim +++ b/tests/stdlib/tgenast.nim @@ -54,7 +54,7 @@ proc main = x.a = val m1() - var x0: Foo + var x0: Foo = Foo() x0.a1 = 10 doAssert x0 == Foo(a: 10) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 4555fbcb342cf..17ad88c745c1b 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -112,7 +112,7 @@ proc main() = when sizeof(int) == 8 or defined(js): block: - var s: seq[Hash] + var s: seq[Hash] = @[] for a in [0.0, 1.0, -1.0, 1000.0, -1000.0]: let b = hash(a) doAssert b notin s @@ -197,7 +197,7 @@ proc main() = doAssert hash(Obj5(t: false, x: 1)) != hash(Obj5(t: true, y: 2)) block: # hash(ref|ptr|pointer) - var a: array[10, uint8] + var a: array[10, uint8] = default(array[10, uint8]) # disableVm: whenVMorJs: # pending fix proposed in https://github.com/nim-lang/Nim/issues/15952#issuecomment-786312417 diff --git a/tests/stdlib/thttpclient_ssl.nim b/tests/stdlib/thttpclient_ssl.nim index 5ddf86bd277cb..4208767925410 100644 --- a/tests/stdlib/thttpclient_ssl.nim +++ b/tests/stdlib/thttpclient_ssl.nim @@ -45,7 +45,7 @@ when not defined(windows) and not disableSSLTesting(): proc runServer(port: Port): bool {.thread.} = ## Run a trivial HTTPS server in a {.thread.} ## Exit after serving one request - + result = false var socket = newSocket() socket.setSockOpt(OptReusePort, true) socket.bindAddr(port) @@ -55,7 +55,7 @@ when not defined(windows) and not disableSSLTesting(): ## Handle one connection socket.listen() - var client: Socket + var client: Socket = default(Socket) var address = "" log "server: ready" diff --git a/tests/stdlib/tisolation.nim b/tests/stdlib/tisolation.nim index 18b83ea2ef3ce..f5b4e0a1264ef 100644 --- a/tests/stdlib/tisolation.nim +++ b/tests/stdlib/tisolation.nim @@ -125,7 +125,7 @@ proc main(moveZeroesOut: static bool) = doAssert data.extract.id == 12 block: - var x: seq[Isolated[JsonNode]] + var x: seq[Isolated[JsonNode]] = @[] x.add isolate(newJString("1234")) doAssert $x == """@[(value: "1234")]""" diff --git a/tests/stdlib/tjsonutils.nim b/tests/stdlib/tjsonutils.nim index 9d9188d755a49..b57cb3e914752 100644 --- a/tests/stdlib/tjsonutils.nim +++ b/tests/stdlib/tjsonutils.nim @@ -15,7 +15,7 @@ proc testRoundtrip[T](t: T, expected: string) = let j = t.toJson doAssert $j == expected, "\n" & $j & "\n" & expected doAssert j.jsonTo(T).toJson == j - var t2: T + var t2: T = default(T) t2.fromJson(j) doAssert t2.toJson == j diff --git a/tests/stdlib/tmacros.nim b/tests/stdlib/tmacros.nim index 06a9a9c27ef3a..a4f13f951958a 100644 --- a/tests/stdlib/tmacros.nim +++ b/tests/stdlib/tmacros.nim @@ -25,6 +25,7 @@ block: # bug #17454 block: # unpackVarargs block: proc bar1(a: varargs[int]): string = + result = "" for ai in a: result.add " " & $ai proc bar2(a: varargs[int]) = let s1 = bar1(a) diff --git a/tests/stdlib/tnet.nim b/tests/stdlib/tnet.nim index 27a6ac49c9776..8ddbb2a7d177e 100644 --- a/tests/stdlib/tnet.nim +++ b/tests/stdlib/tnet.nim @@ -94,10 +94,10 @@ block: # "IpAddress/Sockaddr conversion" doAssert($ipaddrstr == $ipaddr_1) - var sockaddr: Sockaddr_storage - var socklen: SockLen - var ipaddr_2: IpAddress - var port_2: Port + var sockaddr: Sockaddr_storage = default(Sockaddr_storage) + var socklen: SockLen = default(SockLen) + var ipaddr_2: IpAddress = default(IpAddress) + var port_2: Port = default(Port) toSockAddr(ipaddr_1, Port(0), sockaddr, socklen) fromSockAddr(sockaddr, socklen, ipaddr_2, port_2) @@ -108,11 +108,11 @@ block: # "IpAddress/Sockaddr conversion" doAssert($ipaddr_1 == $ipaddr_2) if sockaddr.ss_family.cint == AF_INET.toInt: - var sockaddr4: Sockaddr_in + var sockaddr4: Sockaddr_in = default(Sockaddr_in) copyMem(addr sockaddr4, addr sockaddr, sizeof(sockaddr4)) fromSockAddr(sockaddr4, socklen, ipaddr_2, port_2) elif sockaddr.ss_family.cint == AF_INET6.toInt: - var sockaddr6: Sockaddr_in6 + var sockaddr6: Sockaddr_in6 = default(Sockaddr_in6) copyMem(addr sockaddr6, addr sockaddr, sizeof(sockaddr6)) fromSockAddr(sockaddr6, socklen, ipaddr_2, port_2) diff --git a/tests/stdlib/tnetdial.nim b/tests/stdlib/tnetdial.nim index a1e147ad5c6d2..8915a5d08fb41 100644 --- a/tests/stdlib/tnetdial.nim +++ b/tests/stdlib/tnetdial.nim @@ -40,7 +40,7 @@ proc testThread() {.thread.} = proc test() = let serverFd = initIPv6Server("::1", port) - var t: Thread[void] + var t: Thread[void] = default(Thread[void]) createThread(t, testThread) var done = false diff --git a/tests/stdlib/toptions.nim b/tests/stdlib/toptions.nim index 63a10e746db7d..8c4b9aca3385e 100644 --- a/tests/stdlib/toptions.nim +++ b/tests/stdlib/toptions.nim @@ -47,6 +47,7 @@ proc main() = block example: proc find(haystack: string, needle: char): Option[int] = + result = none(int) for i, c in haystack: if c == needle: return some i @@ -105,7 +106,7 @@ proc main() = block filter: doAssert(some(123).filter(proc (v: int): bool = v == 123) == some(123)) doAssert(some(456).filter(proc (v: int): bool = v == 123).isNone) - doAssert(intNone.filter(proc (v: int): bool = doAssert false).isNone) + doAssert(intNone.filter(proc (v: int): bool = raiseAssert "false").isNone) block flatMap: proc addOneIfNotZero(v: int): Option[int] = @@ -136,7 +137,7 @@ proc main() = doAssert(some(0).flatMap(maybeToString).flatMap(maybeExclaim) == none(string)) block SomePointer: - var intref: ref int + var intref: ref int = nil doAssert(option(intref).isNone) intref.new doAssert(option(intref).isSome) diff --git a/tests/stdlib/tparseutils.nim b/tests/stdlib/tparseutils.nim index b699008646118..10561f5022e9c 100644 --- a/tests/stdlib/tparseutils.nim +++ b/tests/stdlib/tparseutils.nim @@ -44,12 +44,12 @@ proc test() = doAssert(parseSaturatedNatural("1_000_000", value) == 9) doAssert value == 1_000_000 - var i64Value: int64 + var i64Value: int64 = 0'i64 discard parseBiggestInt("9223372036854775807", i64Value) doAssert i64Value == 9223372036854775807 block: - var f: float + var f: float = 0.0 let res = collect: for x in ["9.123456789012345+","11.123456789012345+","9.123456789012345-","8.123456789012345+","9.12345678901234-","9.123456789012345"]: (parseFloat(x, f, 0), $f) diff --git a/tests/stdlib/tpegs.nim b/tests/stdlib/tpegs.nim index da3fc14b7b8d2..32aab82f2bf74 100644 --- a/tests/stdlib/tpegs.nim +++ b/tests/stdlib/tpegs.nim @@ -192,7 +192,7 @@ block: expr.rule = sequence(capture(ident), *sequence( nonterminal(ws), term('+'), nonterminal(ws), nonterminal(expr))) - var c: Captures + var c: Captures = default(Captures) var s = "a+b + c +d+e+f" doAssert rawMatch(s, expr.rule, 0, c) == len(s) var a = "" @@ -208,7 +208,7 @@ block: doAssert match("_______ana", peg"A <- 'ana' / . A") doAssert match("abcs%%%", peg"A <- ..A / .A / '%'") - var matches: array[0..MaxSubpatterns-1, string] + var matches: array[0..MaxSubpatterns-1, string] = default(array[0..MaxSubpatterns-1, string]) if "abc" =~ peg"{'a'}'bc' 'xyz' / {\ident}": doAssert matches[0] == "abc" else: @@ -325,7 +325,7 @@ block: call() call() """ - var c: Captures + var c: Captures = default(Captures) doAssert program.len == program.rawMatch(grammar, 0, c) doAssert c.ml == 1 diff --git a/tests/stdlib/trandom.nim b/tests/stdlib/trandom.nim index 272e2e6ee2b47..a5db318c59173 100644 --- a/tests/stdlib/trandom.nim +++ b/tests/stdlib/trandom.nim @@ -11,7 +11,7 @@ when not defined(js): randomize(233) proc main() = - var occur: array[1000, int] + var occur: array[1000, int] = default(array[1000, int]) for i in 0..100_000: let x = rand(high(occur)) diff --git a/tests/stdlib/tre.nim b/tests/stdlib/tre.nim index 39637434dd38c..0f8a2ba42fed4 100644 --- a/tests/stdlib/tre.nim +++ b/tests/stdlib/tre.nim @@ -23,7 +23,7 @@ proc testAll() = doAssert find("_____abc_______", re"abc") == 5 doAssert findBounds("_____abc_______", re"abc") == (5,7) - var matches: array[6, string] + var matches: array[6, string] = default(array[6, string]) if match("abcdefg", re"c(d)ef(g)", matches, 2): doAssert matches[0] == "d" doAssert matches[1] == "g" @@ -85,7 +85,7 @@ proc testAll() = doAssert("XYZ".match(re"^\d*") == true) block: - var matches: array[16, string] + var matches: array[16, string] = default(array[16, string]) if match("abcdefghijklmnop", re"(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)", matches): for i in 0..matches.high: doAssert matches[i] == $chr(i + 'a'.ord) diff --git a/tests/stdlib/trst.nim b/tests/stdlib/trst.nim index ceab34bc9b058..215a051502b80 100644 --- a/tests/stdlib/trst.nim +++ b/tests/stdlib/trst.nim @@ -41,11 +41,13 @@ proc toAst(input: string, warnings: ref seq[string] = nil): string = ## If `error` is nil then no errors should be generated. ## The same goes for `warnings`. + result = "" + proc testMsgHandler(filename: string, line, col: int, msgkind: MsgKind, arg: string) = let mc = msgkind.whichMsgClass let a = $msgkind % arg - var message: string + var message: string = "" toLocation(message, filename, line, col + ColRstOffset) message.add " $1: $2" % [$mc, a] if mc == mcError: diff --git a/tests/stdlib/trstgen.nim b/tests/stdlib/trstgen.nim index 6253e7146d59c..f23291a188634 100644 --- a/tests/stdlib/trstgen.nim +++ b/tests/stdlib/trstgen.nim @@ -22,11 +22,13 @@ proc toHtml(input: string, warnings: ref seq[string] = nil): string = ## If `error` is nil then no errors should be generated. ## The same goes for `warnings`. + result = "" + proc testMsgHandler(filename: string, line, col: int, msgkind: MsgKind, arg: string) = let mc = msgkind.whichMsgClass let a = $msgkind % arg - var message: string + var message: string = "" toLocation(message, filename, line, col + ColRstOffset) message.add " $1: $2" % [$mc, a] if mc == mcError: diff --git a/tests/stdlib/tsequtils.nim b/tests/stdlib/tsequtils.nim index 1094ae2335eb6..df0fb1610a956 100644 --- a/tests/stdlib/tsequtils.nim +++ b/tests/stdlib/tsequtils.nim @@ -275,7 +275,10 @@ block: # toSeq test numeric = @[1, 2, 3, 4, 5, 6, 7, 8, 9] oddNumbers = toSeq(filter(numeric) do (x: int) -> bool: if x mod 2 == 1: - result = true) + result = true + else: + result = false + ) doAssert oddNumbers == @[1, 3, 5, 7, 9] block: diff --git a/tests/stdlib/tsortcall.nim b/tests/stdlib/tsortcall.nim index 32e004921477c..3d4889ee2efe2 100644 --- a/tests/stdlib/tsortcall.nim +++ b/tests/stdlib/tsortcall.nim @@ -15,6 +15,7 @@ suite "test sort, sorted, and isSorted procs": age: int func newUser(name: string, age: int): User = + result = default(User) result.name = name result.age = age diff --git a/tests/stdlib/tssl.nim b/tests/stdlib/tssl.nim index 1628b9326a253..ee750d5ef9720 100644 --- a/tests/stdlib/tssl.nim +++ b/tests/stdlib/tssl.nim @@ -40,7 +40,7 @@ proc main() = when defined(posix): var ignoreAction = Sigaction(sa_handler: SIG_IGN) - oldSigPipeHandler: Sigaction + oldSigPipeHandler: Sigaction = default(Sigaction) if sigemptyset(ignoreAction.sa_mask) == -1: raiseOSError(osLastError(), "Couldn't create an empty signal set") if sigaction(SIGPIPE, ignoreAction, oldSigPipeHandler) == -1: @@ -58,10 +58,10 @@ proc main() = let (_, port) = server.getLocalAddr() server.listen() - var clientThread: Thread[Port] + var clientThread: Thread[Port] = default(Thread[Port]) createThread(clientThread, abruptShutdown, port) - var peer: Socket + var peer: Socket = default(Socket) try: server.accept(peer) peer.send(DummyData) @@ -88,10 +88,10 @@ proc main() = let (_, port) = server.getLocalAddr() server.listen() - var clientThread: Thread[Port] + var clientThread: Thread[Port] = default(Thread[Port]) createThread(clientThread, abruptShutdown, port) - var peer: Socket + var peer: Socket = default(Socket) try: server.accept(peer) peer.send(DummyData) @@ -115,10 +115,10 @@ proc main() = let (_, port) = server.getLocalAddr() server.listen() - var clientThread: Thread[Port] + var clientThread: Thread[Port] = default(Thread[Port]) createThread(clientThread, notifiedShutdown, port) - var peer: Socket + var peer: Socket = default(Socket) try: server.accept(peer) peer.send(DummyData) diff --git a/tests/stdlib/tstdlib_various.nim b/tests/stdlib/tstdlib_various.nim index bac5018faeadd..5e6d46554155a 100644 --- a/tests/stdlib/tstdlib_various.nim +++ b/tests/stdlib/tstdlib_various.nim @@ -159,7 +159,7 @@ block tsegfaults: var crashes = 0 proc main = try: - var x: ptr int + var x: ptr int = nil echo x[] try: raise newException(ValueError, "not a crash") diff --git a/tests/stdlib/tstrbasics.nim b/tests/stdlib/tstrbasics.nim index a965ff15fbb6d..bfae55fde98e3 100644 --- a/tests/stdlib/tstrbasics.nim +++ b/tests/stdlib/tstrbasics.nim @@ -95,6 +95,7 @@ proc main() = a0.add b0.toOpenArray(1,3) doAssert a0 == "hioob" proc fn(c: openArray[char]): string = + result = "" result.add c doAssert fn("def") == "def" doAssert fn(['d','\0', 'f'])[2] == 'f' diff --git a/tests/stdlib/tstrformat.nim b/tests/stdlib/tstrformat.nim index ff406f8980177..74f23b953b046 100644 --- a/tests/stdlib/tstrformat.nim +++ b/tests/stdlib/tstrformat.nim @@ -24,7 +24,7 @@ proc main() = template formatValue(result: var string; value: Obj; specifier: string) = result.formatValue($value, specifier) - var o: Obj + var o: Obj = default(Obj) doAssert fmt"{o}" == "foobar" doAssert fmt"{o:10}" == "foobar " @@ -292,7 +292,7 @@ proc main() = let x = 3.14 doAssert fmt"{(if x!=0: 1.0/x else: 0):.5}" == "0.31847" doAssert fmt"""{(block: - var res: string + var res: string = "" for i in 1..15: res.add (if i mod 15 == 0: "FizzBuzz" elif i mod 5 == 0: "Buzz" @@ -564,7 +564,7 @@ proc main() = doAssert fmt"""{(if true: "'" & ')' else: "")}""" == "')" block: # issue #20381 - var ss: seq[string] + var ss: seq[string] = @[] template myTemplate(s: string) = ss.add s ss.add s diff --git a/tests/stdlib/tstrscans.nim b/tests/stdlib/tstrscans.nim index ae7fd98caca32..22384f5ce3612 100644 --- a/tests/stdlib/tstrscans.nim +++ b/tests/stdlib/tstrscans.nim @@ -113,6 +113,7 @@ block: while start+result < input.len and input[start+result] in seps: inc result proc demangle(s: string; res: var string; start: int): int = + result = 0 while result+start < s.len and s[result+start] in {'_', '@'}: inc result res = "" while result+start < s.len and s[result+start] > ' ' and s[result+start] != '_': diff --git a/tests/stdlib/tstrset.nim b/tests/stdlib/tstrset.nim index bbb6c2677f2f2..20e690f60e8f8 100644 --- a/tests/stdlib/tstrset.nim +++ b/tests/stdlib/tstrset.nim @@ -22,6 +22,7 @@ type PRadixNodeLeaf = ref TRadixNodeLeaf proc search(r: PRadixNode, s: string): PRadixNode = + result = default(PRadixNode) var r = r var i = 0 while r != nil: @@ -55,7 +56,7 @@ proc contains*(r: PRadixNode, s: string): bool = return search(r, s) != nil proc testOrIncl*(r: var PRadixNode, s: string): bool = - nil + result = false proc incl*(r: var PRadixNode, s: string) = discard testOrIncl(r, s) diff --git a/tests/stdlib/tsugar.nim b/tests/stdlib/tsugar.nim index 2ea96cfbbf6e7..74b992f902df4 100644 --- a/tests/stdlib/tsugar.nim +++ b/tests/stdlib/tsugar.nim @@ -291,7 +291,7 @@ template main() = block: # bug #20704 proc test() = - var xs, ys: seq[int] + var xs, ys: seq[int] = @[] for i in 0..5: xs.add(i) diff --git a/tests/stdlib/ttasks.nim b/tests/stdlib/ttasks.nim index ba65590d93c8d..21fe3b3b679bc 100644 --- a/tests/stdlib/ttasks.nim +++ b/tests/stdlib/ttasks.nim @@ -513,12 +513,12 @@ block: proc testReturnValues() = let t = toTask returnsSomething(2233, 11) - var res: int + var res: int = 0 t.invoke(addr res) doAssert res == 2233+11 let tb = toTask noArgsButReturnsSomething() - var resB: string + var resB: string = "" tb.invoke(addr resB) doAssert resB == "abcdef" diff --git a/tests/stdlib/ttimes.nim b/tests/stdlib/ttimes.nim index 0f04168dc6cdb..4bdd1fadffe01 100644 --- a/tests/stdlib/ttimes.nim +++ b/tests/stdlib/ttimes.nim @@ -12,11 +12,13 @@ proc staticTz(hours, minutes, seconds: int = 0): Timezone {.noSideEffect.} = let offset = hours * 3600 + minutes * 60 + seconds proc zonedTimeFromAdjTime(adjTime: Time): ZonedTime = + result = default(ZonedTime) result.isDst = false result.utcOffset = offset result.time = adjTime + initDuration(seconds = offset) proc zonedTimeFromTime(time: Time): ZonedTime = + result = default(ZonedTime) result.isDst = false result.utcOffset = offset result.time = time diff --git a/tests/stdlib/ttypeinfo.nim b/tests/stdlib/ttypeinfo.nim index 9bbc2e92c2eab..e25c6083d6479 100644 --- a/tests/stdlib/ttypeinfo.nim +++ b/tests/stdlib/ttypeinfo.nim @@ -78,7 +78,7 @@ block: block: # bug #23556 proc test = var - t: seq[int] + t: seq[int] = @[] aseq = toAny(t) invokeNewSeq(aseq, 0) diff --git a/tests/stdlib/tvarargs.nim b/tests/stdlib/tvarargs.nim index 2edc26264aae0..16f284d7d666e 100644 --- a/tests/stdlib/tvarargs.nim +++ b/tests/stdlib/tvarargs.nim @@ -6,7 +6,7 @@ import std/assertions template main = proc hello(x: varargs[string]): seq[string] = - var s: seq[string] + var s: seq[string] = @[] s.add x s diff --git a/tests/stdlib/twrapnils.nim b/tests/stdlib/twrapnils.nim index 3da230b5e4f11..4a5e3051bfce3 100644 --- a/tests/stdlib/twrapnils.nim +++ b/tests/stdlib/twrapnils.nim @@ -33,9 +33,9 @@ proc main() = proc fun(a: Bar): auto = a.b2 - var a: Foo + var a: Foo = nil - var x6: ptr Bar + var x6: ptr Bar = nil when nimvm: discard # pending https://github.com/timotheecour/Nim/issues/568 else: x6 = create(Bar) @@ -107,7 +107,7 @@ proc main() = d2: D proc identity[T](a: T): T = a proc identity2[T](a: T, ignore: int): T = a - var a: A + var a: A = default(A) doAssert ?.a.b.c.d.e.f == 0 doAssert ?.a.b.c.d.e.d2.d3[].d3.e.d2.e.f == 0 doAssert ?.a.b.c.d.d3[].e.f == 0 @@ -174,7 +174,7 @@ proc main() = doAssert ?.identity(d) == nil doAssert ?.identity(d[]) == default(typeof(d[])) doAssert ?.identity(d[]).i4 == 0 - var a: A + var a: A = default(A) doAssert ?.identity(a) == default(A) doAssert ?.identity(a.a0) == 0 doAssert ?.identity(a.d) == nil @@ -195,7 +195,7 @@ proc main() = b1: float block: - var a: A + var a: A = default(A) doAssert ?.a.a0.a1[0].a2.addr == nil a = A(a2: 3) doAssert ?.a.a0.a1[0].a2.addr == nil diff --git a/tests/stdlib/uselocks.nim b/tests/stdlib/uselocks.nim index f87623b5e405f..59e92ccd62b9f 100644 --- a/tests/stdlib/uselocks.nim +++ b/tests/stdlib/uselocks.nim @@ -5,6 +5,7 @@ type MyType* [T] = object lock: Lock proc createMyType*[T]: MyType[T] = + result = default(MyType[T]) initLock(result.lock) proc use* (m: var MyType): int =