From ba2aa30ec96ff7850a06166767344ca9fc6b0b84 Mon Sep 17 00:00:00 2001 From: Stephan Hilb Date: Fri, 26 May 2023 23:44:37 +0200 Subject: [PATCH] Enum: fix stackoverflow in `hash` for custom enum subtypes introduced in #49777 (#49964) --- base/Enums.jl | 2 +- test/enums.jl | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/base/Enums.jl b/base/Enums.jl index 2c18dbca72fcd..45a1b66753484 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -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)) diff --git a/test/enums.jl b/test/enums.jl index 757aa26a061be..6eb9360e08a23 100644 --- a/test/enums.jl +++ b/test/enums.jl @@ -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