Hrm, the new errors highlighted some code that seems to be broken

New issue: since `Table[A, B]` allocates its backing storage with
`newSeq[KeyValuePair[A, B]]`, it's no longer legal to create a table
with `not nil` types used as either keys or values.
This commit is contained in:
Zahary Karadjov 2020-03-29 02:17:47 +02:00 • committed by Andreas Rumpf
commit 0521f98486
8 changed files with 23 additions and 22 deletions

View file

@ -371,9 +371,8 @@ proc map*[T, S](s: openArray[T], op: proc (x: T): S {.closure.}):
b = map(a, proc(x: int): string = $x)
assert b == @["1", "2", "3", "4"]
newSeq(result, s.len)
for i in 0 ..< s.len:
result[i] = op(s[i])
result = newSeqOfCap[S](s.len)
for elem in s: result.add op(elem)
proc apply*[T](s: var openArray[T], op: proc (x: var T) {.closure.})
{.inline.} =

View file

@ -77,8 +77,7 @@ template withValue*[A, B](t: var SharedTable[A, B], key: A,
try:
var hc: Hash
var index = rawGet(t, key, hc)
let hasKey = index >= 0
if hasKey:
if index >= 0:
var value {.inject.} = addr(t.data[index].val)
body
finally:
@ -104,8 +103,7 @@ template withValue*[A, B](t: var SharedTable[A, B], key: A,
try:
var hc: Hash
var index = rawGet(t, key, hc)
let hasKey = index >= 0
if hasKey:
if index >= 0:
var value {.inject.} = addr(t.data[index].val)
body1
else:
@ -119,13 +117,13 @@ proc mget*[A, B](t: var SharedTable[A, B], key: A): var B =
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:
when compiles($key):
raise newException(KeyError, "key not found: " & $key)
if index >= 0:
result = t.data[index].val
else:
raise newException(KeyError, "key not found")
when compiles($key):
raise newException(KeyError, "key not found: " & $key)
else:
raise newException(KeyError, "key not found")
proc mgetOrPut*[A, B](t: var SharedTable[A, B], key: A, val: B): var B =
## retrieves value at ``t[key]`` or puts ``val`` if not present, either way