Skip to content

Commit

Permalink
Enum: fix stackoverflow in hash for custom enum subtypes introduced…
Browse files Browse the repository at this point in the history
… in #49777 (#49964)
  • Loading branch information
stev47 authored May 26, 2023
1 parent f8dd16e commit ba2aa30
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Base.read(io::IO, ::Type{T}) where {T<:Enum} = T(read(io, basetype(T)))
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)
_enum_hash(x::Enum, h::UInt) = invoke(hash, Tuple{Any, UInt}, x, h)
Base.hash(x::Enum, h::UInt) = _enum_hash(x, h)
Base.isless(x::T, y::T) where {T<:Enum} = isless(basetype(T)(x), basetype(T)(y))

Expand Down
4 changes: 4 additions & 0 deletions test/enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ end
@enum HashEnum3 Enum3_a=1
@test which(hash, (HashEnum3, UInt)).sig != Tuple{typeof(hash), HashEnum3, UInt64}

# Check that generic `hash` on custom enum subtypes works.
struct HashEnum4 <: Enum{Int} end
@test hash(HashEnum4(), zero(UInt)) == invoke(hash, Tuple{Any, UInt}, HashEnum4(), zero(UInt))

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

# long, discongruous
Expand Down

0 comments on commit ba2aa30

Please sign in to comment.