From 6d0983398be35a98aa92650db3c70a577ec03a9f Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 3 Oct 2024 19:42:14 +0800 Subject: [PATCH] `-d:nimPreviewHashRef` becomes the default --- changelog.md | 1 + lib/pure/hashes.nim | 46 +++++++++++++++++++-------------------------- tests/config.nims | 1 - 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/changelog.md b/changelog.md index 8acd2120ae..a234e26810 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## Changes affecting backward compatibility +- `-d:nimPreviewHashRef` becomes the default. `hashes.hash` can now support `object` and `ref` (can be overloaded in user code). ## Standard library additions and changes diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 1038d55a1e..f94949eb82 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -51,9 +51,6 @@ runnableExamples: h = h !& hash(x.bar) result = !$h -## .. important:: Use `-d:nimPreviewHashRef` to -## enable hashing `ref`s. It is expected that this behavior -## becomes the new default in upcoming versions. ## ## .. note:: If the type has a `==` operator, the following must hold: ## If two values compare equal, their hashes must also be equal. @@ -244,30 +241,25 @@ proc hash*[T](x: ptr[T]): Hash {.inline.} = assert cast[pointer](a[0].addr).hash == a[0].addr.hash hash(cast[pointer](x)) -when defined(nimPreviewHashRef) or defined(nimdoc): - proc hash*[T](x: ref[T]): Hash {.inline.} = - ## Efficient `hash` overload. - ## - ## .. important:: Use `-d:nimPreviewHashRef` to - ## enable hashing `ref`s. It is expected that this behavior - ## becomes the new default in upcoming versions. - runnableExamples("-d:nimPreviewHashRef"): - type A = ref object - x: int - let a = A(x: 3) - let ha = a.hash - assert ha != A(x: 3).hash # A(x: 3) is a different ref object from `a`. - a.x = 4 - assert ha == a.hash # the hash only depends on the address - runnableExamples("-d:nimPreviewHashRef"): - # you can overload `hash` if you want to customize semantics - type A[T] = ref object - x, y: T - proc hash(a: A): Hash = hash(a.x) - assert A[int](x: 3, y: 4).hash == A[int](x: 3, y: 5).hash - # xxx pending bug #17733, merge as `proc hash*(pointer | ref | ptr): Hash` - # or `proc hash*[T: ref | ptr](x: T): Hash` - hash(cast[pointer](x)) +proc hash*[T](x: ref[T]): Hash {.inline.} = + ## Efficient `hash` overload. + runnableExamples: + type A = ref object + x: int + let a = A(x: 3) + let ha = a.hash + assert ha != A(x: 3).hash # A(x: 3) is a different ref object from `a`. + a.x = 4 + assert ha == a.hash # the hash only depends on the address + runnableExamples: + # you can overload `hash` if you want to customize semantics + type A[T] = ref object + x, y: T + proc hash(a: A): Hash = hash(a.x) + assert A[int](x: 3, y: 4).hash == A[int](x: 3, y: 5).hash + # xxx pending bug #17733, merge as `proc hash*(pointer | ref | ptr): Hash` + # or `proc hash*[T: ref | ptr](x: T): Hash` + hash(cast[pointer](x)) proc hash*(x: float): Hash {.inline.} = ## Efficient hashing of floats. diff --git a/tests/config.nims b/tests/config.nims index 0b2b66d81b..ec2c906199 100644 --- a/tests/config.nims +++ b/tests/config.nims @@ -38,7 +38,6 @@ switch("define", "nimExperimentalLinenoiseExtra") switch("define", "nimPreviewFloatRoundtrip") #switch("define", "nimPreviewDotLikeOps") # deprecated? switch("define", "nimPreviewJsonutilsHoleyEnum") -switch("define", "nimPreviewHashRef") switch("define", "nimPreviewRangeDefault") switch("define", "nimPreviewNonVarDestructor")