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

View file

@ -502,8 +502,7 @@ proc contains*[T](s: Selector[T], fd: SocketHandle|int): bool {.inline.} =
proc getData*[T](s: Selector[T], fd: SocketHandle|int): var T =
let fdi = int(fd)
s.checkFd(fdi)
if fdi in s:
result = s.fds[fdi].data
result = s.fds[fdi].data
proc setData*[T](s: Selector[T], fd: SocketHandle|int, data: T): bool =
let fdi = int(fd)

View file

@ -600,8 +600,7 @@ proc contains*[T](s: Selector[T], fd: SocketHandle|int): bool {.inline.} =
proc getData*[T](s: Selector[T], fd: SocketHandle|int): var T =
let fdi = int(fd)
s.checkFd(fdi)
if fdi in s:
result = s.fds[fdi].data
result = s.fds[fdi].data
proc setData*[T](s: Selector[T], fd: SocketHandle|int, data: T): bool =
let fdi = int(fd)

View file

@ -410,11 +410,17 @@ else:
body
proc getData*[T](s: Selector[T], fd: SocketHandle|int): var T =
# The compiler needs this to prove that all code paths return a value
result = (cast[ptr T](0'u))[]
s.withSelectLock():
let fdi = int(fd)
for i in 0..<FD_SETSIZE:
if s.fds[i].ident == fdi:
return s.fds[i].data
result = s.fds[i].data
break
assert cast[uint](addr(result)) != 0
proc setData*[T](s: Selector[T], fd: SocketHandle|int, data: T): bool =
s.withSelectLock():

View file

@ -338,8 +338,8 @@ proc rowEntry*(my: var CsvParser, entry: string): var string =
strm.close()
let index = my.headers.find(entry)
if index >= 0:
result = my.row[index]
assert index >= 0
result = my.row[index]
when not defined(testing) and isMainModule:
import os

View file

@ -80,7 +80,7 @@ block thashes:
# Test with range
block:
type
R = range[1..10]
R = range[0..9]
var t = initTable[R,int]() # causes warning, why?
t[1] = 42 # causes warning, why?
t[2] = t[1] + 1

View file

@ -8,7 +8,7 @@ type
Rgba8 = object
proc premultiply*(c: var Rgba8): var Rgba8 =
discard
return c
type
App = ref object