Skip to content

Commit d25c421

Browse files
authored
Eliminate ambiguity with Number constructors (#525)
The constructor for `T<:Number` given a vararg number of `AbstractZero` is ambiguous with any `Number` subtype having a varargs constructor: ``` julia> module M using ChainRulesCore struct X{R} <: Number a::R hasvalue::Bool function X{R}(a, hv=true) where {R} isa(hv, Bool) || error("must be bool") return new{R}(a, hv) end end end Main.M julia> using Test julia> detect_ambiguities(M) 5-element Vector{Tuple{Method, Method}}: (Main.M.X{R}(a) where R in Main.M at REPL[1]:8, (::Type{T})(x::Base.TwicePrecision) where T<:Number in Base at twiceprecision.jl:255) (Main.M.X{R}(a) where R in Main.M at REPL[1]:8, (::Type{T})(x::AbstractChar) where T<:Union{AbstractChar, Number} in Base at char.jl:50) ((::Type{T})(xs::ChainRulesCore.AbstractZero...) where T<:Number in ChainRulesCore at /home/tim/.julia/dev/ChainRulesCore/src/tangent_types/abstract_zero.jl:28, Main.M.X{R}(a, hv) where R in Main.M at REPL[1]:8) (Main.M.X{R}(a) where R in Main.M at REPL[1]:8, (::Type{T})(x::T) where T<:Number in Core at boot.jl:770) ((::Type{T})(xs::ChainRulesCore.AbstractZero...) where T<:Number in ChainRulesCore at /home/tim/.julia/dev/ChainRulesCore/src/tangent_types/abstract_zero.jl:28, Main.M.X{R}(a) where R in Main.M at REPL[1]:8) ``` I couldn't see any reason that method was needed at all, so I commented it out, with a fix in case someone should discover a need.
1 parent addf6d9 commit d25c421

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/tangent_types/abstract_zero.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Base.transpose(z::AbstractZero) = z
2525
Base.:/(z::AbstractZero, ::Any) = z
2626

2727
Base.convert(::Type{T}, x::AbstractZero) where {T<:Number} = zero(T)
28-
(::Type{T})(xs::AbstractZero...) where {T<:Number} = zero(T)
28+
# (::Type{T})(::AbstractZero, ::AbstractZero...) where {T<:Number} = zero(T)
2929

3030
(::Type{Complex})(x::AbstractZero, y::Real) = Complex(false, y)
3131
(::Type{Complex})(x::Real, y::AbstractZero) = Complex(x, false)

test/tangent_types/abstract_zero.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,22 @@
116116
@test convert(Int64, NoTangent()) == 0
117117
@test convert(Float64, NoTangent()) == 0.0
118118
end
119+
120+
@testset "ambiguities" begin
121+
M = @eval module M
122+
using ChainRulesCore
123+
124+
struct X{R,S} <: Number
125+
a::R
126+
b::S
127+
hasvalue::Bool
128+
129+
function X{R,S}(a, b, hv=true) where {R,S}
130+
isa(hv, Bool) || error("must be bool")
131+
return new{R,S}(a, b, hv)
132+
end
133+
end
134+
end
135+
@test isempty(detect_ambiguities(M))
136+
end
119137
end

0 commit comments

Comments
 (0)