Skip to content

Commit 710bf90

Browse files
Seelengrabaviatesk
andauthored
Make type hashing :total (#52427)
Since types can't really be deleted and the hash of a type is already being cached inside of the type object itself, it should be legal to mark the `ccall` retrieving that hash as `:total`, permitting hashing of types to be concretely evaluated. I'm unsure if the test I've added is good as written, or whether this should be done differently. --------- Co-authored-by: Sukera <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]>
1 parent 46ad1c1 commit 710bf90

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

base/hashing.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ See also: [`objectid`](@ref), [`Dict`](@ref), [`Set`](@ref).
2929
"""
3030
hash(x::Any) = hash(x, zero(UInt))
3131
hash(w::WeakRef, h::UInt) = hash(w.value, h)
32-
hash(T::Type, h::UInt) = hash_uint(3h - ccall(:jl_type_hash, UInt, (Any,), T))
32+
33+
# Types can't be deleted, so marking as total allows the compiler to look up the hash
34+
hash(T::Type, h::UInt) = hash_uint(3h - @assume_effects :total ccall(:jl_type_hash, UInt, (Any,), T))
3335

3436
## hashing general objects ##
3537

test/hashing.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,5 @@ struct AUnionParam{T<:Union{Nothing,Float32,Float64}} end
302302
# test hashing of rational with odd denominator
303303
@test hash(5//3) == hash(big(5)//3)
304304
end
305+
306+
@test Core.Compiler.is_foldable_nothrow(Base.infer_effects(hash, Tuple{Type{Int}, UInt}))

0 commit comments

Comments
 (0)