add -d:nimLegacyNoHashRef for a transition period which avoids defining hash(ref) (#17858)

This commit is contained in:
Timothee Cour 2021-04-26 23:23:01 -07:00 • committed by GitHub
commit 3f58b7face
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 17 deletions

View file

@ -51,6 +51,7 @@
for previous behavior. for previous behavior.
- `hashes.hash` now supports `object` and `ref` (can be overloaded in user code). - `hashes.hash` now supports `object` and `ref` (can be overloaded in user code).
For a transition period, use `-d:nimLegacyNoHashRef` to avoid defining `hash(ref)`.
- `hashes.hash(proc|ptr|ref|pointer)` now calls `hash(int)` and honors `-d:nimIntHash1`, - `hashes.hash(proc|ptr|ref|pointer)` now calls `hash(int)` and honors `-d:nimIntHash1`,
`hashes.hash(closure)` has also been improved. `hashes.hash(closure)` has also been improved.

View file

@ -228,12 +228,17 @@ proc hash*(x: pointer): Hash {.inline.} =
let y = cast[int](x) let y = cast[int](x)
hash(y) # consistent with code expecting scrambled hashes depending on `nimIntHash1`. hash(y) # consistent with code expecting scrambled hashes depending on `nimIntHash1`.
proc hash*[T](x: ref[T] | ptr[T]): Hash {.inline.} = proc hash*[T](x: ptr[T]): Hash {.inline.} =
## Efficient `hash` overload. ## Efficient `hash` overload.
runnableExamples: runnableExamples:
var a: array[10, uint8] var a: array[10, uint8]
assert a[0].addr.hash != a[1].addr.hash assert a[0].addr.hash != a[1].addr.hash
assert cast[pointer](a[0].addr).hash == a[0].addr.hash assert cast[pointer](a[0].addr).hash == a[0].addr.hash
hash(cast[pointer](x))
when not defined(nimLegacyNoHashRef):
proc hash*[T](x: ref[T]): Hash {.inline.} =
## Efficient `hash` overload.
runnableExamples: runnableExamples:
type A = ref object type A = ref object
x: int x: int