mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-17 13:02:47 +00:00
enable experimental:strictDefs (#24225)
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -48,7 +48,7 @@ define:useStdoutAsStdmsg
|
||||
|
||||
|
||||
@if nimUseStrictDefs:
|
||||
experimental:strictDefs
|
||||
experimental:strictDefs # deadcode
|
||||
warningAsError[Uninit]:on
|
||||
warningAsError[ProveInit]:on
|
||||
@end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -21,3 +21,4 @@ when defined(nimStrictMode):
|
||||
# future work: XDeclaredButNotUsed
|
||||
|
||||
switch("define", "nimVersion:" & NimVersion) # deadcode
|
||||
switch("experimental", "strictDefs")
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 ------------------------------
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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] =
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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`.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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`
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 @[]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 =
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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`
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
discard """
|
||||
matrix: "--legacy:nostrictdefs"
|
||||
joinable: false
|
||||
"""
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -3,3 +3,5 @@ switch("styleCheck", "error")
|
||||
switch("define", "nimPreviewSlimSystem")
|
||||
switch("define", "nimPreviewCstringConversion")
|
||||
switch("define", "nimPreviewProcConversion")
|
||||
switch("warningAserror", "ProveInit")
|
||||
switch("warningAserror", "Uninit")
|
||||
|
||||
@@ -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)]
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 & "*"
|
||||
|
||||
@@ -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()")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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")]"""
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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] != '_':
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -78,7 +78,7 @@ block:
|
||||
block: # bug #23556
|
||||
proc test =
|
||||
var
|
||||
t: seq[int]
|
||||
t: seq[int] = @[]
|
||||
aseq = toAny(t)
|
||||
|
||||
invokeNewSeq(aseq, 0)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 =
|
||||
|
||||
Reference in New Issue
Block a user