From 1ca25ef6c50aa091ea90297cd92fea6d42df3c53 Mon Sep 17 00:00:00 2001 From: Andy Dienes Date: Thu, 19 Mar 2026 16:20:44 -0400 Subject: [PATCH] Fix Base.hash to use only the two-arg method Generated as part of an ecosystem-wide audit for one-arg hash methods. `Base.hash` should only be extended via the two-arg method `hash(x, h::UInt)`. Defining a one-arg `hash(x)` method, or giving the second argument a default value, can lead to correctness bugs (hash contract violations when the seed differs from the hard-coded default) and invalidation-related performance issues. This is particularly necessary for Julia 1.13+ where the default hash seed value will change. Co-Authored-By: Claude Opus 4.6 --- src/DitStr.jl | 2 +- src/longlonguint.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/DitStr.jl b/src/DitStr.jl index ded4369..6750788 100644 --- a/src/DitStr.jl +++ b/src/DitStr.jl @@ -64,7 +64,7 @@ Base.zero(::Type{DitStr{D,N,T}}) where {D,N,T} = DitStr{D,N,T}(zero(T)) Base.zero(::DitStr{D,N,T}) where {D,N,T} = DitStr{D,N,T}(zero(T)) buffer(b::DitStr) = b.buf -Base.hash(d::DitStr) = hash(buffer(d)) +Base.hash(d::DitStr, h::UInt) = hash(buffer(d), h) Base.reinterpret(::Type{DitStr{D,N,T}}, x::Integer) where {D,N,T} = DitStr{D,N,T}(reinterpret(T, x)) Base.reinterpret(::Type{T}, x::DitStr) where {T} = reinterpret(T, buffer(x)) Base.reinterpret(::Type{DitStr{D,N,T}}, x::DitStr) where {D,N,T} = DitStr{D,N,T}(x) diff --git a/src/longlonguint.jl b/src/longlonguint.jl index 14113c6..ad19b2f 100644 --- a/src/longlonguint.jl +++ b/src/longlonguint.jl @@ -212,8 +212,8 @@ function longinttype(n::Int, D::Int) return LongLongUInt{C} end -Base.hash(x::LongLongUInt{1}) = hash(x.content[1]) -Base.hash(x::LongLongUInt{C}) where{C} = hash(x.content) +Base.hash(x::LongLongUInt{1}, h::UInt) = hash(x.content[1], h) +Base.hash(x::LongLongUInt{C}, h::UInt) where{C} = hash(x.content, h) # these APIs will are used in SparseTN BitBasis.log2i(x::LongLongUInt{C}) where C = floor(Int, log2(Float64(BigInt(x))))