Skip to content

Commit 9bae9f6

Browse files
committed
Fix type ambiguities on master.
1 parent b68a8a6 commit 9bae9f6

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/static.jl

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,42 @@ struct Static{N} <: Integer
77
Static{N}() where {N} = new{N::Int}()
88
end
99
Base.@pure Static(N::Int) = Static{N}()
10-
Static(N) = Static(convert(Int, N))
10+
Static(N::Integer) = Static(convert(Int, N))
11+
Static(::Static{N}) where {N} = Static{N}()
1112
Static(::Val{N}) where {N} = Static{N}()
1213
Base.Val(::Static{N}) where {N} = Val{N}()
1314
Base.convert(::Type{T}, ::Static{N}) where {T<:Number,N} = convert(T, N)
1415
Base.convert(::Type{Static{N}}, ::Static{N}) where {N} = Static{N}()
15-
Base.promote_rule(::Type{<:Static}, ::Type{T}) where {T} = promote_rule(Int, T)
16-
Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T} = promote_rule(T, Int)
17-
Base.promote_rule(::Type{<:Static}, ::Type{<:Static}) where {T} = Int
16+
for S [:Any, :AbstractIrrational]#, :(Complex{<:Real})]
17+
# let S = :Any
18+
@eval begin
19+
Base.promote_rule(::Type{<:Static}, ::Type{T}) where {T <: $S} = promote_rule(Int, T)
20+
Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T <: $S} = promote_rule(T, Int)
21+
end
22+
end
23+
for (S,T) [(:Complex,:Real), (:Rational, :Integer), (:(Base.TwicePrecision),:Any)]
24+
@eval Base.promote_rule(::Type{$S{T}}, ::Type{<:Static}) where {T <: $T} = promote_rule($S{T}, Int)
25+
end
26+
Base.promote_rule(::Type{Union{Nothing,Missing}}, ::Type{<:Static}) = Union{Nothing, Missing, Int}
27+
Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T >: Union{Missing,Nothing}} = promote_rule(T, Int)
28+
Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T >: Nothing} = promote_rule(T, Int)
29+
Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T >: Missing} = promote_rule(T, Int)
30+
for T [:Bool, :Missing, :BigFloat, :BigInt, :Nothing]
31+
# let S = :Any
32+
@eval begin
33+
Base.promote_rule(::Type{<:Static}, ::Type{$T}) = promote_rule(Int, $T)
34+
Base.promote_rule(::Type{$T}, ::Type{<:Static}) = promote_rule($T, Int)
35+
end
36+
end
37+
Base.promote_rule(::Type{<:Static}, ::Type{<:Static}) = Int
1838
Base.:(%)(::Static{N}, ::Type{Integer}) where {N} = N
1939

2040
Base.iszero(::Static{0}) = true
2141
Base.iszero(::Static) = false
2242
Base.isone(::Static{1}) = true
2343
Base.isone(::Static) = false
2444

25-
for T [:Any, :Number, :Integer]
45+
for T = [:Real, :Rational, :Integer]
2646
@eval begin
2747
@inline Base.:(+)(i::$T, ::Static{0}) = i
2848
@inline Base.:(+)(i::$T, ::Static{M}) where {M} = i + M
@@ -38,18 +58,28 @@ for T ∈ [:Any, :Number, :Integer]
3858
@inline Base.:(*)(::Static{M}, i::$T) where {M} = M * i
3959
end
4060
end
61+
@inline Base.:(+)(::Static{0}, ::Static{0}) = Static{0}()
62+
@inline Base.:(+)(::Static{0}, ::Static{M}) where {M} = Static{M}()
63+
@inline Base.:(+)(::Static{M}, ::Static{0}) where {M} = Static{M}()
64+
65+
@inline Base.:(-)(::Static{M}, ::Static{0}) where {M} = Static{M}()
66+
4167
@inline Base.:(*)(::Static{0}, ::Static{0}) = Static{0}()
4268
@inline Base.:(*)(::Static{1}, ::Static{0}) = Static{0}()
4369
@inline Base.:(*)(::Static{0}, ::Static{1}) = Static{0}()
4470
@inline Base.:(*)(::Static{1}, ::Static{1}) = Static{1}()
71+
@inline Base.:(*)(::Static{M}, ::Static{0}) where {M} = Static{0}()
72+
@inline Base.:(*)(::Static{0}, ::Static{M}) where {M} = Static{0}()
73+
@inline Base.:(*)(::Static{M}, ::Static{1}) where {M} = Static{M}()
74+
@inline Base.:(*)(::Static{1}, ::Static{M}) where {M} = Static{M}()
4575
for f [:(+), :(-), :(*), :(/), :(÷), :(%), :(<<), :(>>), :(>>>), :(&), :(|), :()]
4676
@eval @generated Base.$f(::Static{M}, ::Static{N}) where {M,N} = Expr(:call, Expr(:curly, :Static, $f(M, N)))
4777
end
4878
for f [:(==), :(!=), :(<), :(), :(>), :()]
4979
@eval begin
5080
@inline Base.$f(::Static{M}, ::Static{N}) where {M,N} = $f(M, N)
51-
@inline Base.$f(::Static{M}, x::Integer) where {M} = $f(M, x)
52-
@inline Base.$f(x::Integer, ::Static{M}) where {M} = $f(x, M)
81+
@inline Base.$f(::Static{M}, x::Int) where {M} = $f(M, x)
82+
@inline Base.$f(x::Int, ::Static{M}) where {M} = $f(x, M)
5383
end
5484
end
5585

test/runtests.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,5 @@ end
269269
@test convert(typeof(y), @inferred(f(1.4, i))) === y # if f is division and i === Static(0), returns `NaN`; hence use of ==== in check.
270270
end
271271
end
272-
@test @inferred("Hello world!" * Static(0)) === Static(0)
273-
@test @inferred("Hello world!" * Static(1)) === "Hello world!"
274-
@test @inferred(Static(0) * "Hello world!") === Static(0)
275-
@test @inferred(Static(1) * "Hello world!") === "Hello world!"
276272
end
277273

0 commit comments

Comments
 (0)