You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix unsafe @pures via separate methods for BitInts
The current usages of `Base.@pure` are _unsafe if called with
user-defined types_, since their definitions may change arbitrarily.
We fix this by _only providing `@pure` methods for the built in
BitInteger types,_ and non-`@pure` methods for any types.
Consider the following demonstration of the broken behavior:
```julia
(@v1.4) pkg> add FixedPointDecimals#master
Updating git-repo `https://github.com/JuliaMath/FixedPointDecimals.jl.git`
julia> struct MyInt <: Integer
x::Int
end
julia> Base.typemax(::Type{MyInt}) = 10000000
julia> Base.widen(::Type{MyInt}) = Int128
julia> using FixedPointDecimals
julia> a = reinterpret(FixedPointDecimals.FD{MyInt,2}, MyInt(2));
julia> Base.typemax(::Type{MyInt}) = 10
julia> a = reinterpret(FixedPointDecimals.FD{MyInt,2}, MyInt(2)); # SHOULD BE AN ERROR!
julia>
```
This is fixed, after this commit:
```julia
julia> struct MyInt <: Integer
x::Int
end
julia> Base.typemax(::Type{MyInt}) = 10000000
julia> Base.widen(::Type{MyInt}) = Int128
julia> using FixedPointDecimals
[ Info: Precompiling FixedPointDecimals [fb4d412d-6eee-574d-9565-ede6634db7b0]
julia> a = reinterpret(FixedPointDecimals.FD{MyInt,2}, MyInt(2));
julia> Base.typemax(::Type{MyInt}) = 10
julia> a = reinterpret(FixedPointDecimals.FD{MyInt,2}, MyInt(2)); # SHOULD BE AN ERROR!
ERROR: ArgumentError: Requested number of decimal places 2 exceeds the max allowed for the storage type MyInt: [0, 0]
```
---------------------------------------------------------------
In order to reduce code-duplication, we add a small macro that
duplicates the function definition to provide two identical macros,
where one is restricted to BitIntegers.
0 commit comments