fixes #21393 and misc style changes (#21419)

* fixes #21393 and misc style changes

* progress

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
Andreas Rumpf
2023-02-24 09:02:15 +01:00
committed by GitHub
parent 7dc80a7fef
commit 8a19ac2070
11 changed files with 70 additions and 61 deletions

View File

@@ -33,26 +33,27 @@ template high*(t: typedesc[Int128]): Int128 = Max
proc `$`*(a: Int128): string
proc toInt128*[T: SomeInteger | bool](arg: T): Int128 =
when T is bool: result.sdata(0) = int32(arg)
elif T is SomeUnsignedInt:
when sizeof(arg) <= 4:
result.udata[0] = uint32(arg)
{.noSideEffect.}:
when T is bool: result.sdata(0) = int32(arg)
elif T is SomeUnsignedInt:
when sizeof(arg) <= 4:
result.udata[0] = uint32(arg)
else:
result.udata[0] = uint32(arg and T(0xffffffff))
result.udata[1] = uint32(arg shr 32)
elif sizeof(arg) <= 4:
result.sdata(0) = int32(arg)
if arg < 0: # sign extend
result.sdata(1) = -1
result.sdata(2) = -1
result.sdata(3) = -1
else:
result.udata[0] = uint32(arg and T(0xffffffff))
result.udata[1] = uint32(arg shr 32)
elif sizeof(arg) <= 4:
result.sdata(0) = int32(arg)
if arg < 0: # sign extend
result.sdata(1) = -1
result.sdata(2) = -1
result.sdata(3) = -1
else:
let tmp = int64(arg)
result.udata[0] = uint32(tmp and 0xffffffff)
result.sdata(1) = int32(tmp shr 32)
if arg < 0: # sign extend
result.sdata(2) = -1
result.sdata(3) = -1
let tmp = int64(arg)
result.udata[0] = uint32(tmp and 0xffffffff)
result.sdata(1) = int32(tmp shr 32)
if arg < 0: # sign extend
result.sdata(2) = -1
result.sdata(3) = -1
template isNegative(arg: Int128): bool =
arg.sdata(3) < 0

View File

@@ -822,9 +822,6 @@ proc checkForSink(tracked: PEffects; n: PNode) =
if tracked.inIfStmt == 0 and optSinkInference in tracked.config.options:
checkForSink(tracked.config, tracked.c.idgen, tracked.owner, n)
proc strictFuncsActive(tracked: PEffects): bool {.inline.} =
sfNoSideEffect in tracked.owner.flags and strictFuncs in tracked.c.features and not tracked.inEnforcedNoSideEffects
proc trackCall(tracked: PEffects; n: PNode) =
template gcsafeAndSideeffectCheck() =
if notGcSafe(op) and not importedFromC(a):
@@ -934,9 +931,11 @@ proc trackCall(tracked: PEffects; n: PNode) =
# initialized until after the call. Since we do this after we analysed the
# call, this is fine.
initVar(tracked, n[i].skipAddr, false)
if tracked.strictFuncsActive and isDangerousLocation(n[i].skipAddr, tracked.owner):
localError(tracked.config, n[i].info,
"cannot pass $1 to `var T` parameter within a strict func" % renderTree(n[i]))
if strictFuncs in tracked.c.features and not tracked.inEnforcedNoSideEffects and
isDangerousLocation(n[i].skipAddr, tracked.owner):
if sfNoSideEffect in tracked.owner.flags:
localError(tracked.config, n[i].info,
"cannot pass $1 to `var T` parameter within a strict func" % renderTree(n[i]))
tracked.hasSideEffect = true
else: discard
@@ -1090,10 +1089,12 @@ proc track(tracked: PEffects, n: PNode) =
createTypeBoundOps(tracked, n[0].typ, n.info)
if n[0].kind != nkSym or not isLocalSym(tracked, n[0].sym):
checkForSink(tracked, n[1])
if tracked.strictFuncsActive and isDangerousLocation(n[0], tracked.owner):
if strictFuncs in tracked.c.features and not tracked.inEnforcedNoSideEffects and
isDangerousLocation(n[0], tracked.owner):
tracked.hasSideEffect = true
localError(tracked.config, n[0].info,
"cannot mutate location $1 within a strict func" % renderTree(n[0]))
if sfNoSideEffect in tracked.owner.flags:
localError(tracked.config, n[0].info,
"cannot mutate location $1 within a strict func" % renderTree(n[0]))
of nkVarSection, nkLetSection:
for child in n:
let last = lastSon(child)

View File

@@ -447,7 +447,7 @@ proc mgetOrPut*[A, B](t: var Table[A, B], key: A, val: B): var B =
##
##
## Note that while the value returned is of type `var B`,
## it is easy to accidentally create an copy of the value at `t[key]`.
## it is easy to accidentally create a copy of the value at `t[key]`.
## Remember that seqs and strings are value types, and therefore
## cannot be copied into a separate variable for modification.
## See the example below.
@@ -828,7 +828,8 @@ proc newTable*[A, B](initialSize = defaultInitialSize): TableRef[A, B] =
b = newTable[char, seq[int]]()
new(result)
result[] = initTable[A, B](initialSize)
{.noSideEffect.}:
result[] = initTable[A, B](initialSize)
proc newTable*[A, B](pairs: openArray[(A, B)]): TableRef[A, B] =
## Creates a new ref hash table that contains the given `pairs`.
@@ -844,14 +845,16 @@ proc newTable*[A, B](pairs: openArray[(A, B)]): TableRef[A, B] =
assert b == {'a': 5, 'b': 9}.newTable
new(result)
result[] = toTable[A, B](pairs)
{.noSideEffect.}:
result[] = toTable[A, B](pairs)
proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): TableRef[C, B] =
## Index the collection with the proc provided.
# TODO: As soon as supported, change collection: A to collection: A[B]
result = newTable[C, B]()
for item in collection:
result[index(item)] = item
{.noSideEffect.}:
for item in collection:
result[index(item)] = item
proc `[]`*[A, B](t: TableRef[A, B], key: A): var B =
## Retrieves the value at `t[key]`.
@@ -1825,7 +1828,8 @@ proc newOrderedTable*[A, B](initialSize = defaultInitialSize): OrderedTableRef[A
a = newOrderedTable[int, string]()
b = newOrderedTable[char, seq[int]]()
new(result)
result[] = initOrderedTable[A, B](initialSize)
{.noSideEffect.}:
result[] = initOrderedTable[A, B](initialSize)
proc newOrderedTable*[A, B](pairs: openArray[(A, B)]): OrderedTableRef[A, B] =
## Creates a new ordered ref hash table that contains the given `pairs`.
@@ -1842,7 +1846,8 @@ proc newOrderedTable*[A, B](pairs: openArray[(A, B)]): OrderedTableRef[A, B] =
assert b == {'a': 5, 'b': 9}.newOrderedTable
result = newOrderedTable[A, B](pairs.len)
for key, val in items(pairs): result[key] = val
{.noSideEffect.}:
for key, val in items(pairs): result[key] = val
proc `[]`*[A, B](t: OrderedTableRef[A, B], key: A): var B =
@@ -2641,13 +2646,15 @@ proc newCountTable*[A](initialSize = defaultInitialSize): CountTableRef[A] =
## * `initCountTable proc<#initCountTable>`_ for creating a
## `CountTable`
new(result)
result[] = initCountTable[A](initialSize)
{.noSideEffect.}:
result[] = initCountTable[A](initialSize)
proc newCountTable*[A](keys: openArray[A]): CountTableRef[A] =
## Creates a new ref count table with every member of a container `keys`
## having a count of how many times it occurs in that container.
result = newCountTable[A](keys.len)
for key in items(keys): result.inc(key)
{.noSideEffect.}:
for key in items(keys): result.inc(key)
proc `[]`*[A](t: CountTableRef[A], key: A): int =
## Retrieves the value at `t[key]` if `key` is in `t`.
@@ -2671,7 +2678,8 @@ proc `[]=`*[A](t: CountTableRef[A], key: A, val: int) =
## * `inc proc<#inc,CountTableRef[A],A,int>`_ for incrementing a
## value of a key
assert val > 0
t[][key] = val
{.noSideEffect.}:
t[][key] = val
proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
## Increments `t[key]` by `val` (default: 1).
@@ -2680,7 +2688,8 @@ proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
a.inc('a')
a.inc('b', 10)
doAssert a == newCountTable("aaabbbbbbbbbbb")
t[].inc(key, val)
{.noSideEffect.}:
t[].inc(key, val)
proc smallest*[A](t: CountTableRef[A]): tuple[key: A, val: int] =
## Returns the `(key, value)` pair with the smallest `val`. Efficiency: O(n)

View File

@@ -247,7 +247,7 @@ proc hash*[T](x: ptr[T]): Hash {.inline.} =
when defined(nimPreviewHashRef) or defined(nimdoc):
proc hash*[T](x: ref[T]): Hash {.inline.} =
## Efficient `hash` overload.
##
##
## .. important:: Use `-d:nimPreviewHashRef` to
## enable hashing `ref`s. It is expected that this behavior
## becomes the new default in upcoming versions.
@@ -536,6 +536,7 @@ proc hash*[T: tuple | object | proc](x: T): Hash =
elif T is (proc):
result = hash(pointer(x))
else:
result = 0
for f in fields(x):
result = result !& hash(f)
result = !$result
@@ -551,6 +552,7 @@ proc hash*[A](x: openArray[A]): Hash =
else:
result = murmurHash(toOpenArrayByte(x, 0, x.high))
else:
result = 0
for a in x:
result = result !& hash(a)
result = !$result
@@ -583,6 +585,7 @@ proc hash*[A](aBuf: openArray[A], sPos, ePos: int): Hash =
proc hash*[A](x: set[A]): Hash =
## Efficient hashing of sets.
## There must be a `hash` proc defined for the element type `A`.
result = 0
for it in items(x):
result = result !& hash(it)
result = !$result

View File

@@ -570,12 +570,8 @@ when defined(windows) and not defined(useNimRtl):
if a == 0: raiseOSError(osLastError())
proc newFileHandleStream(handle: Handle): owned FileHandleStream =
new(result)
result.handle = handle
result.closeImpl = hsClose
result.atEndImpl = hsAtEnd
result.readDataImpl = hsReadData
result.writeDataImpl = hsWriteData
result = FileHandleStream(handle: handle, closeImpl: hsClose, atEndImpl: hsAtEnd,
readDataImpl: hsReadData, writeDataImpl: hsWriteData)
proc buildCommandLine(a: string, args: openArray[string]): string =
result = quoteShell(a)

View File

@@ -101,9 +101,7 @@ proc addInt*(result: var string; x: int64) {.enforceNoRaises.} =
num = cast[uint64](x)
else:
num = uint64(-x)
let base = result.len
setLen(result, base + 1)
result[base] = '-'
result.add '-'
else:
num = uint64(x)
addInt(result, num)

View File

@@ -571,7 +571,7 @@ proc normExt(ext: string): string =
proc searchExtPos*(path: string): int =
## Returns index of the `'.'` char in `path` if it signifies the beginning
## of extension. Returns -1 otherwise.
## of the file extension. Returns -1 otherwise.
##
## See also:
## * `splitFile proc`_

View File

@@ -83,7 +83,8 @@ proc shrink*[T](x: var seq[T]; newLen: Natural) {.tags: [], raises: [].} =
for i in countdown(x.len - 1, newLen):
reset x[i]
# XXX This is wrong for const seqs that were moved into 'x'!
cast[ptr NimSeqV2[T]](addr x).len = newLen
{.noSideEffect.}:
cast[ptr NimSeqV2[T]](addr x).len = newLen
proc grow*[T](x: var seq[T]; newLen: Natural; value: T) =
let oldLen = x.len

View File

@@ -33,7 +33,7 @@ proc eqStrings(a, b: string): bool {.inline, compilerproc.} =
proc hashString(s: string): int {.compilerproc.} =
# the compiler needs exactly the same hash function!
# this used to be used for efficient generation of string case statements
var h : uint = 0
var h = 0'u
for i in 0..len(s)-1:
h = h + uint(s[i])
h = h + h shl 10

View File

@@ -48,12 +48,12 @@ type
HGLRC* = Handle
BYTE* = uint8
SECURITY_ATTRIBUTES* {.final, pure.} = object
SECURITY_ATTRIBUTES* = object
nLength*: int32
lpSecurityDescriptor*: pointer
bInheritHandle*: WINBOOL
STARTUPINFO* {.final, pure.} = object
STARTUPINFO* = object
cb*: int32
lpReserved*: cstring
lpDesktop*: cstring
@@ -73,17 +73,17 @@ type
hStdOutput*: Handle
hStdError*: Handle
PROCESS_INFORMATION* {.final, pure.} = object
PROCESS_INFORMATION* = object
hProcess*: Handle
hThread*: Handle
dwProcessId*: int32
dwThreadId*: int32
FILETIME* {.final, pure.} = object ## CANNOT BE int64 BECAUSE OF ALIGNMENT
FILETIME* = object ## CANNOT BE int64 BECAUSE OF ALIGNMENT
dwLowDateTime*: DWORD
dwHighDateTime*: DWORD
BY_HANDLE_FILE_INFORMATION* {.final, pure.} = object
BY_HANDLE_FILE_INFORMATION* = object
dwFileAttributes*: DWORD
ftCreationTime*: FILETIME
ftLastAccessTime*: FILETIME
@@ -95,7 +95,7 @@ type
nFileIndexHigh*: DWORD
nFileIndexLow*: DWORD
OSVERSIONINFO* {.final, pure.} = object
OSVERSIONINFO* = object
dwOSVersionInfoSize*: DWORD
dwMajorVersion*: DWORD
dwMinorVersion*: DWORD
@@ -820,7 +820,7 @@ type
POVERLAPPED_COMPLETION_ROUTINE* = proc (para1: DWORD, para2: DWORD,
para3: POVERLAPPED){.stdcall.}
GUID* {.final, pure.} = object
GUID* = object
D1*: int32
D2*: int16
D3*: int16
@@ -1101,7 +1101,7 @@ proc wsaResetEvent*(hEvent: Handle): bool
{.stdcall, importc: "WSAResetEvent", dynlib: "ws2_32.dll".}
type
KEY_EVENT_RECORD* {.final, pure.} = object
KEY_EVENT_RECORD* = object
eventType*: int16
bKeyDown*: WINBOOL
wRepeatCount*: int16

View File

@@ -53,7 +53,7 @@ type
JsonNode3 = ref object
fields: MyTable
proc `[]`(t: var MyTable, key: string): var int =
proc `[]`(t: MyTable, key: string): int =
result = t.data[0]
proc `[]`(x: JsonNode3, key: string): int =