Skip to content

Commit eecbf66

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 05b73cd commit eecbf66

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

base/reflection.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,9 +866,14 @@ function method_exists(f::ANY, t::ANY)
866866
typemax(UInt)) != 0
867867
end
868868

869-
function isambiguous(m1::Method, m2::Method)
869+
function isambiguous(m1::Method, m2::Method, allow_bottom_tparams::Bool=true)
870870
ti = typeintersect(m1.sig, m2.sig)
871871
ti === Bottom && return false
872+
if !allow_bottom_tparams
873+
(_, env) = ccall(:jl_match_method, Ref{SimpleVector}, (Any, Any),
874+
ti, m1.sig)
875+
any(x->x === Bottom, env) && return false
876+
end
872877
ml = _methods_by_ftype(ti, -1, typemax(UInt))
873878
isempty(ml) && return true
874879
for m in ml

base/test.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ defined in the specified modules. Use `imported=true` if you wish to
11371137
also test functions that were imported into these modules from
11381138
elsewhere.
11391139
"""
1140-
function detect_ambiguities(mods...; imported::Bool=false)
1140+
function detect_ambiguities(mods...; imported::Bool=false, allow_bottom::Bool=true)
11411141
function sortdefs(m1, m2)
11421142
ord12 = m1.file < m2.file
11431143
if !ord12 && (m1.file == m2.file)
@@ -1159,7 +1159,7 @@ function detect_ambiguities(mods...; imported::Bool=false)
11591159
for m in mt
11601160
if m.ambig !== nothing
11611161
for m2 in m.ambig
1162-
if Base.isambiguous(m, m2)
1162+
if Base.isambiguous(m, m2, allow_bottom)
11631163
push!(ambs, sortdefs(m, m2))
11641164
end
11651165
end

0 commit comments

Comments
 (0)