Browse Source

Fix overflow bug with unsigned integer types.

pull/8/head
Fredrik Ekre 4 years ago
parent
commit
5410ec9f23
  1. 4
      src/EnumX.jl
  2. 4
      test/runtests.jl

4
src/EnumX.jl

@ -34,11 +34,12 @@ function enumx(_module_, name, args...)
end end
name_value_map = Vector{Pair{Symbol, baseT}}() name_value_map = Vector{Pair{Symbol, baseT}}()
next = zero(baseT) next = zero(baseT)
first = true
for s in syms for s in syms
s isa LineNumberNode && continue s isa LineNumberNode && continue
local sym local sym
if s isa Symbol if s isa Symbol
if next == typemin(baseT) if !first && next == typemin(baseT)
panic("value overflow for Enum $(modname): $(modname).$(s) = $(next).") panic("value overflow for Enum $(modname): $(modname).$(s) = $(next).")
end end
sym = s sym = s
@ -71,6 +72,7 @@ function enumx(_module_, name, args...)
push!(name_value_map, sym => next) push!(name_value_map, sym => next)
next += oneunit(baseT) next += oneunit(baseT)
first = false
end end
value_name_map = Dict{baseT,Symbol}(v => k for (k, v) in reverse(name_value_map)) value_name_map = Dict{baseT,Symbol}(v => k for (k, v) in reverse(name_value_map))
module_block = quote module_block = quote

4
test/runtests.jl

@ -80,6 +80,10 @@ end
@test Base.Enums.basetype(Fruit8.Type) === Int8 @test Base.Enums.basetype(Fruit8.Type) === Int8
@test Integer(Fruit8.Apple) === Int8(0) @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 @enumx Fruit16::T16 Apple
@test Fruit16.Type <: EnumX.Enum{Int16} <: Base.Enum{Int16} @test Fruit16.Type <: EnumX.Enum{Int16} <: Base.Enum{Int16}
@test Base.Enums.basetype(Fruit16.Type) === Int16 @test Base.Enums.basetype(Fruit16.Type) === Int16

Loading…
Cancel
Save