Skip to content

Commit

Permalink
ml-matches: using relaxed sorting when unlimited
Browse files Browse the repository at this point in the history
If we're attempting to apply limits to the results,
then it is generally better and faster to use more accurate sorting.

Otherwise, approximate sorting should be sufficient and much faster.
  • Loading branch information
vtjnash committed Oct 19, 2020
1 parent d65f41d commit 12328e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/src/manual/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ julia> g(2, 3.0)
julia> g(2.0, 3.0)
ERROR: MethodError: g(::Float64, ::Float64) is ambiguous. Candidates:
g(x, y::Float64) in Main at none:1
g(x::Float64, y) in Main at none:1
g(x, y::Float64) in Main at none:1
Possible fix, define
g(::Float64, ::Float64)
```
Expand Down
16 changes: 13 additions & 3 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2815,10 +2815,20 @@ static jl_value_t *ml_matches(jl_methtable_t *mt, int offs,
int subt2 = matc2->fully_covers != NOT_FULLY_COVERS;
if (!subt2 && subt)
break;
if (subt == subt2)
if (subt || !jl_has_empty_intersection(m->sig, m2->sig))
if (!jl_type_morespecific((jl_value_t*)m->sig, (jl_value_t*)m2->sig))
if (subt == subt2) {
if (lim >= 0) {
if (subt || !jl_has_empty_intersection(m->sig, m2->sig))
if (!jl_type_morespecific((jl_value_t*)m->sig, (jl_value_t*)m2->sig))
break;
}
else {
// if unlimited, use approximate sorting, with the only
// main downside being that it may be overly-
// conservative at reporting existence of ambiguities
if (jl_type_morespecific((jl_value_t*)m2->sig, (jl_value_t*)m->sig))
break;
}
}
jl_array_ptr_set(env.t, i - j, matc2);
}
jl_array_ptr_set(env.t, i - j, env.matc);
Expand Down
4 changes: 2 additions & 2 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ method_c2(x::Int32, y::Int32, z::Int32) = true
method_c2(x::T, y::T, z::T) where {T<:Real} = true

Base.show_method_candidates(buf, Base.MethodError(method_c2,(1., 1., 2)))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c2(!Matched::Int32, ::Float64, ::Any...)$cfile$(c2line+2)\n method_c2(::T, ::T, !Matched::T) where T<:Real$cfile$(c2line+5)\n method_c2(!Matched::Int32, ::Any...)$cfile$(c2line+1)\n ..."
@test String(take!(buf)) == "\nClosest candidates are:\n method_c2(!Matched::Int32, ::Float64, ::Any...)$cfile$(c2line+2)\n method_c2(!Matched::Int32, ::Any...)$cfile$(c2line+1)\n method_c2(::T, ::T, !Matched::T) where T<:Real$cfile$(c2line+5)\n ..."

c3line = @__LINE__() + 1
method_c3(x::Float64, y::Float64) = true
Expand Down Expand Up @@ -124,7 +124,7 @@ PR16155line2 = @__LINE__() + 1
(::Type{T})(arg::Any) where {T<:PR16155} = "replace call-to-convert method from sysimg"

Base.show_method_candidates(buf, MethodError(PR16155,(1.0, 2.0, Int64(3))))
@test String(take!(buf)) == "\nClosest candidates are:\n $(curmod_prefix)PR16155(::Any, ::Any)$cfile$PR16155line\n (::Type{T})(::Any) where T<:$(curmod_prefix)PR16155$cfile$PR16155line2\n $(curmod_prefix)PR16155(!Matched::Int64, ::Any)$cfile$PR16155line"
@test String(take!(buf)) == "\nClosest candidates are:\n $(curmod_prefix)PR16155(::Any, ::Any)$cfile$PR16155line\n $(curmod_prefix)PR16155(!Matched::Int64, ::Any)$cfile$PR16155line\n (::Type{T})(::Any) where T<:$(curmod_prefix)PR16155$cfile$PR16155line2"

Base.show_method_candidates(buf, MethodError(PR16155,(Int64(3), 2.0, Int64(3))))
@test String(take!(buf)) == "\nClosest candidates are:\n $(curmod_prefix)PR16155(::Int64, ::Any)$cfile$PR16155line\n $(curmod_prefix)PR16155(::Any, ::Any)$cfile$PR16155line\n (::Type{T})(::Any) where T<:$(curmod_prefix)PR16155$cfile$PR16155line2"
Expand Down

0 comments on commit 12328e2

Please sign in to comment.