Skip to content

Commit cf385fe

Browse files
committed
Ambiguity test that ignores matches due to Union{} tparams
Because of the way methods are specified, there is often spurious ambiguities due to a type parameter being able to take the value Union{}, (e.g. Type{T} becomes Type{Union{}}). Since detect_ambiguities reports these, it can drown out more serious ambiguities among non-Union{} types. Add a keyword argument to detect_ambiguities, that ignores all ambiguities that only occur if one of the type parameters has to be Union{}.
1 parent 9e88a95 commit cf385fe

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

base/reflection.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,14 @@ function method_exists(f::ANY, t::ANY)
850850
typemax(UInt)) != 0
851851
end
852852

853-
function isambiguous(m1::Method, m2::Method)
853+
function isambiguous(m1::Method, m2::Method, allow_bottom_tparams::Bool=true)
854854
ti = typeintersect(m1.sig, m2.sig)
855855
ti === Bottom && return false
856+
if !allow_bottom_tparams
857+
(_, env) = ccall(:jl_match_method, Ref{SimpleVector}, (Any, Any),
858+
ti, m1.sig)
859+
any(x->x === Bottom, env) && return false
860+
end
856861
ml = _methods_by_ftype(ti, -1, typemax(UInt))
857862
isempty(ml) && return true
858863
for m in ml

base/test.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ defined in the specified modules. Use `imported=true` if you wish to
11151115
also test functions that were imported into these modules from
11161116
elsewhere.
11171117
"""
1118-
function detect_ambiguities(mods...; imported::Bool=false)
1118+
function detect_ambiguities(mods...; imported::Bool=false, allow_bottom::Bool=true)
11191119
function sortdefs(m1, m2)
11201120
ord12 = m1.file < m2.file
11211121
if !ord12 && (m1.file == m2.file)
@@ -1137,7 +1137,7 @@ function detect_ambiguities(mods...; imported::Bool=false)
11371137
for m in mt
11381138
if m.ambig !== nothing
11391139
for m2 in m.ambig
1140-
if Base.isambiguous(m, m2)
1140+
if Base.isambiguous(m, m2, allow_bottom)
11411141
push!(ambs, sortdefs(m, m2))
11421142
end
11431143
end

0 commit comments

Comments
 (0)