From 526266db7899afe3a494e8e4bd4e0af5b7d78ab0 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Tue, 4 Feb 2025 08:28:48 +0100 Subject: [PATCH] fix hash of negative enums --- Project.toml | 2 +- src/StructHelpers.jl | 2 +- test/runtests.jl | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 4b39b5f..06aaf52 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StructHelpers" uuid = "4093c41a-2008-41fd-82b8-e3f9d02b504f" authors = ["Jan Weidner and contributors"] -version = "1.3.0" +version = "1.3.1" [deps] ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" diff --git a/src/StructHelpers.jl b/src/StructHelpers.jl index 553c286..da75779 100644 --- a/src/StructHelpers.jl +++ b/src/StructHelpers.jl @@ -461,7 +461,7 @@ macro enumbatteries(T, kw...) if nt.hash def = :(function Base.hash(o::$T, h::UInt) h = ($start_hash)(o, h, $(nt.typesalt)) - Base.hash(UInt(o), h) + Base.hash(Integer(o), h) end ) push!(ret.args, def) diff --git a/test/runtests.jl b/test/runtests.jl index a7df8a7..e8b7746 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -174,6 +174,9 @@ end @test_throws Exception convert(Shape, "Circle") end +@enum Negative MinusOne=-1 MinusTwo=-2 MinusThree=-3 +@enumbatteries Negative typesalt = 0xd11b6121f2b8cd22 + @testset "@enumbatteries hash" begin # hash with typesalt @test hash(Circle) == hash(7, hash(0x0578044908fb9846)) @@ -186,6 +189,11 @@ end # no hash by default @test hash(Red) != hash(0, hash(Color)) + + h = 0xed315b93bf264f3e + typesalt = 0xd11b6121f2b8cd22 + @test hash(MinusOne, h) == hash(-1, hash(typesalt, h)) + @test hash(MinusTwo, h) == hash(-2, hash(typesalt, h)) end struct Bad end