Skip to content

allow specializing Base.hash for enum types without overwriting method #49777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ Base.cconvert(::Type{T}, x::Enum{T2}) where {T<:Integer,T2<:Integer} = T(x)::T
Base.write(io::IO, x::Enum{T}) where {T<:Integer} = write(io, T(x))
Base.read(io::IO, ::Type{T}) where {T<:Enum} = T(read(io, basetype(T)))

"""
_enum_hash(x::Enum, h::UInt)

Compute hash for an enum value `x`. This internal method will be specialized
for every enum type created through [`@enum`](@ref).
"""
_enum_hash(x::Enum, h::UInt) = hash(x, h)
Base.hash(x::Enum, h::UInt) = _enum_hash(x, h)
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback for _enum_hash has to be something else, otherwise these two methods just call each other resulting in stack overflows (fredrikekre/EnumX.jl#5).

Copy link
Contributor Author

@stev47 stev47 May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the assumption was that using @enum would create specialized hash functions to dispatch to.
This should fix it:

Suggested change
_enum_hash(x::Enum, h::UInt) = hash(x, h)
Base.hash(x::Enum, h::UInt) = _enum_hash(x, h)
_enum_hash(x::Enum, h::UInt) = invoke(hash, Tuple{Any, UInt}, x, h)
Base.hash(x::Enum, h::UInt) = _enum_hash(x, h)

Could you test and open a pull request?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I can when I find the time, but I am not the one who caused this so it is more tempting for me to press the revert button.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opened #49964

Base.isless(x::T, y::T) where {T<:Enum} = isless(basetype(T)(x), basetype(T)(y))

Base.Symbol(x::Enum) = namemap(typeof(x))[Integer(x)]::Symbol
Expand Down Expand Up @@ -206,8 +214,12 @@ macro enum(T::Union{Symbol,Expr}, syms...)
Enums.namemap(::Type{$(esc(typename))}) = $(esc(namemap))
Base.typemin(x::Type{$(esc(typename))}) = $(esc(typename))($lo)
Base.typemax(x::Type{$(esc(typename))}) = $(esc(typename))($hi)
let enum_hash = hash($(esc(typename)))
Base.hash(x::$(esc(typename)), h::UInt) = hash(enum_hash, hash(Integer(x), h))
let type_hash = hash($(esc(typename)))
# Use internal `_enum_hash` to allow users to specialize
# `Base.hash` for their own enum types without overwriting the
# method we would define here. This avoids a warning for
# precompilation.
Enums._enum_hash(x::$(esc(typename)), h::UInt) = hash(type_hash, hash(Integer(x), h))
end
let insts = (Any[ $(esc(typename))(v) for v in $values ]...,)
Base.instances(::Type{$(esc(typename))}) = insts
Expand Down
5 changes: 5 additions & 0 deletions test/enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ end
@enum HashEnum2 Enum2_a=1
@test hash(Enum1_a) != hash(Enum2_a)

# PR #49777: Check that `Base.hash` can be specialized by the user without
# overwriting a method definition.
@enum HashEnum3 Enum3_a=1
@test which(hash, (HashEnum3, UInt)).sig != Tuple{typeof(hash), HashEnum3, UInt64}

@test (Vector{Fruit}(undef, 3) .= apple) == [apple, apple, apple]

# long, discongruous
Expand Down