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
commit 8a19ac2070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 70 additions and 61 deletions

View file

@ -33,6 +33,7 @@ template high*(t: typedesc[Int128]): Int128 = Max
proc `$`*(a: Int128): string proc `$`*(a: Int128): string
proc toInt128*[T: SomeInteger | bool](arg: T): Int128 = proc toInt128*[T: SomeInteger | bool](arg: T): Int128 =
{.noSideEffect.}:
when T is bool: result.sdata(0) = int32(arg) when T is bool: result.sdata(0) = int32(arg)
elif T is SomeUnsignedInt: elif T is SomeUnsignedInt:
when sizeof(arg) <= 4: when sizeof(arg) <= 4:

View file

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

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`, ## 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 ## Remember that seqs and strings are value types, and therefore
## cannot be copied into a separate variable for modification. ## cannot be copied into a separate variable for modification.
## See the example below. ## See the example below.
@ -828,6 +828,7 @@ proc newTable*[A, B](initialSize = defaultInitialSize): TableRef[A, B] =
b = newTable[char, seq[int]]() b = newTable[char, seq[int]]()
new(result) new(result)
{.noSideEffect.}:
result[] = initTable[A, B](initialSize) result[] = initTable[A, B](initialSize)
proc newTable*[A, B](pairs: openArray[(A, B)]): TableRef[A, B] = proc newTable*[A, B](pairs: openArray[(A, B)]): TableRef[A, B] =
@ -844,12 +845,14 @@ proc newTable*[A, B](pairs: openArray[(A, B)]): TableRef[A, B] =
assert b == {'a': 5, 'b': 9}.newTable assert b == {'a': 5, 'b': 9}.newTable
new(result) new(result)
{.noSideEffect.}:
result[] = toTable[A, B](pairs) result[] = toTable[A, B](pairs)
proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): TableRef[C, B] = proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): TableRef[C, B] =
## Index the collection with the proc provided. ## Index the collection with the proc provided.
# TODO: As soon as supported, change collection: A to collection: A[B] # TODO: As soon as supported, change collection: A to collection: A[B]
result = newTable[C, B]() result = newTable[C, B]()
{.noSideEffect.}:
for item in collection: for item in collection:
result[index(item)] = item result[index(item)] = item
@ -1825,6 +1828,7 @@ proc newOrderedTable*[A, B](initialSize = defaultInitialSize): OrderedTableRef[A
a = newOrderedTable[int, string]() a = newOrderedTable[int, string]()
b = newOrderedTable[char, seq[int]]() b = newOrderedTable[char, seq[int]]()
new(result) new(result)
{.noSideEffect.}:
result[] = initOrderedTable[A, B](initialSize) result[] = initOrderedTable[A, B](initialSize)
proc newOrderedTable*[A, B](pairs: openArray[(A, B)]): OrderedTableRef[A, B] = proc newOrderedTable*[A, B](pairs: openArray[(A, B)]): OrderedTableRef[A, B] =
@ -1842,6 +1846,7 @@ proc newOrderedTable*[A, B](pairs: openArray[(A, B)]): OrderedTableRef[A, B] =
assert b == {'a': 5, 'b': 9}.newOrderedTable assert b == {'a': 5, 'b': 9}.newOrderedTable
result = newOrderedTable[A, B](pairs.len) result = newOrderedTable[A, B](pairs.len)
{.noSideEffect.}:
for key, val in items(pairs): result[key] = val for key, val in items(pairs): result[key] = val
@ -2641,12 +2646,14 @@ proc newCountTable*[A](initialSize = defaultInitialSize): CountTableRef[A] =
## * `initCountTable proc<#initCountTable>`_ for creating a ## * `initCountTable proc<#initCountTable>`_ for creating a
## `CountTable` ## `CountTable`
new(result) new(result)
{.noSideEffect.}:
result[] = initCountTable[A](initialSize) result[] = initCountTable[A](initialSize)
proc newCountTable*[A](keys: openArray[A]): CountTableRef[A] = proc newCountTable*[A](keys: openArray[A]): CountTableRef[A] =
## Creates a new ref count table with every member of a container `keys` ## 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. ## having a count of how many times it occurs in that container.
result = newCountTable[A](keys.len) result = newCountTable[A](keys.len)
{.noSideEffect.}:
for key in items(keys): result.inc(key) for key in items(keys): result.inc(key)
proc `[]`*[A](t: CountTableRef[A], key: A): int = proc `[]`*[A](t: CountTableRef[A], key: A): int =
@ -2671,6 +2678,7 @@ proc `[]=`*[A](t: CountTableRef[A], key: A, val: int) =
## * `inc proc<#inc,CountTableRef[A],A,int>`_ for incrementing a ## * `inc proc<#inc,CountTableRef[A],A,int>`_ for incrementing a
## value of a key ## value of a key
assert val > 0 assert val > 0
{.noSideEffect.}:
t[][key] = val t[][key] = val
proc inc*[A](t: CountTableRef[A], key: A, val = 1) = proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
@ -2680,6 +2688,7 @@ proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
a.inc('a') a.inc('a')
a.inc('b', 10) a.inc('b', 10)
doAssert a == newCountTable("aaabbbbbbbbbbb") doAssert a == newCountTable("aaabbbbbbbbbbb")
{.noSideEffect.}:
t[].inc(key, val) t[].inc(key, val)
proc smallest*[A](t: CountTableRef[A]): tuple[key: A, val: int] = proc smallest*[A](t: CountTableRef[A]): tuple[key: A, val: int] =

View file

@ -536,6 +536,7 @@ proc hash*[T: tuple | object | proc](x: T): Hash =
elif T is (proc): elif T is (proc):
result = hash(pointer(x)) result = hash(pointer(x))
else: else:
result = 0
for f in fields(x): for f in fields(x):
result = result !& hash(f) result = result !& hash(f)
result = !$result result = !$result
@ -551,6 +552,7 @@ proc hash*[A](x: openArray[A]): Hash =
else: else:
result = murmurHash(toOpenArrayByte(x, 0, x.high)) result = murmurHash(toOpenArrayByte(x, 0, x.high))
else: else:
result = 0
for a in x: for a in x:
result = result !& hash(a) result = result !& hash(a)
result = !$result result = !$result
@ -583,6 +585,7 @@ proc hash*[A](aBuf: openArray[A], sPos, ePos: int): Hash =
proc hash*[A](x: set[A]): Hash = proc hash*[A](x: set[A]): Hash =
## Efficient hashing of sets. ## Efficient hashing of sets.
## There must be a `hash` proc defined for the element type `A`. ## There must be a `hash` proc defined for the element type `A`.
result = 0
for it in items(x): for it in items(x):
result = result !& hash(it) result = result !& hash(it)
result = !$result result = !$result

View file

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

View file

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

View file

@ -571,7 +571,7 @@ proc normExt(ext: string): string =
proc searchExtPos*(path: string): int = proc searchExtPos*(path: string): int =
## Returns index of the `'.'` char in `path` if it signifies the beginning ## 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: ## See also:
## * `splitFile proc`_ ## * `splitFile proc`_

View file

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

View file

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

View file

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

View file

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