-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Unexpected behaviour when modifying/adding data to a table #13454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
#13430 ? |
This seems the same type of issue as #13430 |
Basically, use a TableRef, or avoid modifiying tables while also assigning to them |
It's much trickier to fix/report than #13430 though. |
Workaround for now: import tables
type my_object = object
id: int
for l in 1..90:
var table = newTable[int, my_object]()
for id in 0..<l:
table[id] = my_object(id: 0)
proc returns_123(): int =
table[l] = my_object()
return 123
let tmp = returns_123()
table[0].id = tmp
echo l, " ", table[0].id
|
here's a reduced example without any import nor Table: type Foo = object
id: int
type Tab = ref object
s: seq[Foo]
proc `[]`(a: Tab, index: int): var Foo = a.s[index]
for i in 1..<100:
var t = Tab()
proc bar(): int =
t.s.add Foo(id: -2)
10
for id in 0..<i:
t.s.add Foo(id: -1)
t[0].id = bar()
if t[0].id != 10:
echo i, " ", t[0].id prints:
notethe closure bar() is equivalent to calling the following non closure proc: proc bar2(t: Tab): int =
t.s.add Foo(id: -2)
10 in the expression So https://nim-lang.org/docs/manual_experimental.html#aliasing-restrictions-in-parameter-passing should be amended ; maybe it should consider a full statement; but I'mnot sure that's possible without being overly restrictive |
Apologies for the bad subject, but this is hard to explain in one line.
Reported on #Nim by Stuffe with some modifications by me. The following snippet shows flakey behaviour when modifying a table in a specific way. My hunch is that this has to do with Nim messing up the internal Table bookkeeping when the internal data structures are resized.
The expected output of this program would be
N 123
for every line, but for N==43 and N==86 the output isN 0
The text was updated successfully, but these errors were encountered: