Commit fe63195
committed
Fix unsafe
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.@pures via separate methods for BitInts1 parent 317c82b commit fe63195
1 file changed
+36
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
72 | 101 | | |
73 | 102 | | |
74 | 103 | | |
| |||
80 | 109 | | |
81 | 110 | | |
82 | 111 | | |
83 | | - | |
| 112 | + | |
84 | 113 | | |
85 | 114 | | |
86 | 115 | | |
| |||
115 | 144 | | |
116 | 145 | | |
117 | 146 | | |
118 | | - | |
| 147 | + | |
119 | 148 | | |
120 | 149 | | |
121 | 150 | | |
122 | | - | |
| 151 | + | |
123 | 152 | | |
124 | 153 | | |
125 | 154 | | |
126 | | - | |
| 155 | + | |
127 | 156 | | |
128 | 157 | | |
129 | 158 | | |
| |||
333 | 362 | | |
334 | 363 | | |
335 | 364 | | |
336 | | - | |
| 365 | + | |
337 | 366 | | |
338 | 367 | | |
339 | 368 | | |
| |||
480 | 509 | | |
481 | 510 | | |
482 | 511 | | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | 512 | | |
488 | 513 | | |
489 | 514 | | |
| |||
515 | 540 | | |
516 | 541 | | |
517 | 542 | | |
518 | | - | |
519 | | - | |
| 543 | + | |
| 544 | + | |
520 | 545 | | |
521 | 546 | | |
522 | 547 | | |
| |||
0 commit comments