diff --git a/src/EnumX.jl b/src/EnumX.jl index e167a2b..8224166 100644 --- a/src/EnumX.jl +++ b/src/EnumX.jl @@ -34,11 +34,12 @@ function enumx(_module_, name, args...) end name_value_map = Vector{Pair{Symbol, baseT}}() next = zero(baseT) + first = true for s in syms s isa LineNumberNode && continue local sym if s isa Symbol - if next == typemin(baseT) + if !first && next == typemin(baseT) panic("value overflow for Enum $(modname): $(modname).$(s) = $(next).") end sym = s @@ -71,6 +72,7 @@ function enumx(_module_, name, args...) push!(name_value_map, sym => next) next += oneunit(baseT) + first = false end value_name_map = Dict{baseT,Symbol}(v => k for (k, v) in reverse(name_value_map)) module_block = quote diff --git a/test/runtests.jl b/test/runtests.jl index 97fd280..6918ca9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -80,6 +80,10 @@ end @test Base.Enums.basetype(Fruit8.Type) === Int8 @test Integer(Fruit8.Apple) === Int8(0) +@enumx FruitU8::UInt8 Apple # no overflow even if first is typemin(T) +@test Base.Enums.basetype(FruitU8.Type) === UInt8 +@test FruitU8.Apple === FruitU8.Type(0) + @enumx Fruit16::T16 Apple @test Fruit16.Type <: EnumX.Enum{Int16} <: Base.Enum{Int16} @test Base.Enums.basetype(Fruit16.Type) === Int16