Skip to content

Commit

Permalink
Make type hashing :total (#52427)
Browse files Browse the repository at this point in the history
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 <Seelengrab@users.noreply.github.com>
Co-authored-by: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 7, 2023
1 parent 46ad1c1 commit 710bf90
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion base/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ See also: [`objectid`](@ref), [`Dict`](@ref), [`Set`](@ref).
"""
hash(x::Any) = hash(x, zero(UInt))
hash(w::WeakRef, h::UInt) = hash(w.value, h)
hash(T::Type, h::UInt) = hash_uint(3h - ccall(:jl_type_hash, UInt, (Any,), T))

# Types can't be deleted, so marking as total allows the compiler to look up the hash
hash(T::Type, h::UInt) = hash_uint(3h - @assume_effects :total ccall(:jl_type_hash, UInt, (Any,), T))

## hashing general objects ##

Expand Down
2 changes: 2 additions & 0 deletions test/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,5 @@ struct AUnionParam{T<:Union{Nothing,Float32,Float64}} end
# test hashing of rational with odd denominator
@test hash(5//3) == hash(big(5)//3)
end

@test Core.Compiler.is_foldable_nothrow(Base.infer_effects(hash, Tuple{Type{Int}, UInt}))

0 comments on commit 710bf90

Please sign in to comment.