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
Table has inconsistent hash #20023
Comments
Workaround below. I'm not sure that this is a good idea (e.g. multiple parts of code may have conflicting hashes for the same object). I think this needs to be part of import std/[tables, hashes]
proc hash*[K,V](s: Table[K,V]): Hash =
for p in pairs(s):
result = result xor hash(p)
result = !$result |
Is there even a hash for Table? https://nim-lang.github.io/Nim/theindex.html#hash I think it picks the generic hash for objects. |
@planetis-m Yes, that does seem to be the case, which bit me quite badly. It would be nice if defining a new equality operator somehow undefined the hash function, so this would be caught at compile time. Oddly enough, there’s an order-invariant hash for ordered tables of JSON nodes (rather than correctly defining the hash on the Line 499 in 800cb00
|
* fix #20023 hash for generic tables * use default computation * Update lib/pure/collections/tables.nim Co-authored-by: Dan Rose <dan@digilabs.io> * Update lib/pure/collections/tables.nim Co-authored-by: Dan Rose <dan@digilabs.io> * Update lib/pure/collections/tables.nim * Update lib/pure/collections/tables.nim * Update t20023.nim --------- Co-authored-by: Dan Rose <dan@digilabs.io> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fix nim-lang#20023 hash for generic tables * use default computation * Update lib/pure/collections/tables.nim Co-authored-by: Dan Rose <dan@digilabs.io> * Update lib/pure/collections/tables.nim Co-authored-by: Dan Rose <dan@digilabs.io> * Update lib/pure/collections/tables.nim * Update lib/pure/collections/tables.nim * Update t20023.nim --------- Co-authored-by: Dan Rose <dan@digilabs.io> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fix #20023 hash for generic tables * use default computation * Update lib/pure/collections/tables.nim Co-authored-by: Dan Rose <dan@digilabs.io> * Update lib/pure/collections/tables.nim Co-authored-by: Dan Rose <dan@digilabs.io> * Update lib/pure/collections/tables.nim * Update lib/pure/collections/tables.nim * Update t20023.nim --------- Co-authored-by: Dan Rose <dan@digilabs.io> Co-authored-by: Andreas Rumpf <rumpf_a@web.de> (cherry picked from commit 3ad2e7d)
The hash of a
Table
depends on how thatTable
was derived.This violates the prime rule of hashes (If two values compare equal, their hashes must also be equal) and results in corrupt data structures when
Table
is used as a key in a hashed container.Example
Current Output
Expected Output
no error
Possible Solution
Additional Information
The text was updated successfully, but these errors were encountered: