Skip to content

Commit 42537ee

Browse files
stev47KristofferC
authored and
KristofferC
committed
Enum: fix stackoverflow in hash for custom enum subtypes introduced in #49777 (#49964)
(cherry picked from commit ba2aa30)
1 parent 51e4ccd commit 42537ee

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

base/Enums.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Base.read(io::IO, ::Type{T}) where {T<:Enum} = T(read(io, basetype(T)))
2727
Compute hash for an enum value `x`. This internal method will be specialized
2828
for every enum type created through [`@enum`](@ref).
2929
"""
30-
_enum_hash(x::Enum, h::UInt) = hash(x, h)
30+
_enum_hash(x::Enum, h::UInt) = invoke(hash, Tuple{Any, UInt}, x, h)
3131
Base.hash(x::Enum, h::UInt) = _enum_hash(x, h)
3232
Base.isless(x::T, y::T) where {T<:Enum} = isless(basetype(T)(x), basetype(T)(y))
3333

test/enums.jl

+4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ end
184184
@enum HashEnum3 Enum3_a=1
185185
@test which(hash, (HashEnum3, UInt)).sig != Tuple{typeof(hash), HashEnum3, UInt64}
186186

187+
# Check that generic `hash` on custom enum subtypes works.
188+
struct HashEnum4 <: Enum{Int} end
189+
@test hash(HashEnum4(), zero(UInt)) == invoke(hash, Tuple{Any, UInt}, HashEnum4(), zero(UInt))
190+
187191
@test (Vector{Fruit}(undef, 3) .= apple) == [apple, apple, apple]
188192

189193
# long, discongruous

0 commit comments

Comments
 (0)