Skip to content

Commit

Permalink
Merge pull request #49664 from JuliaLang/jn/ml-matches-rewritten
Browse files Browse the repository at this point in the history
reorder ml-matches to avoid catastrophic performance case
  • Loading branch information
vtjnash authored May 15, 2023
2 parents 9dd3090 + ac1cb1c commit fbbe9ed
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 310 deletions.
4 changes: 2 additions & 2 deletions doc/src/manual/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ julia> g(2.0, 3.0)
ERROR: MethodError: g(::Float64, ::Float64) is ambiguous.
Candidates:
g(x::Float64, y)
@ Main none:1
g(x, y::Float64)
@ Main none:1
g(x::Float64, y)
@ Main none:1
Possible fix, define
g(::Float64, ::Float64)
Expand Down
698 changes: 395 additions & 303 deletions src/gf.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ end
let s = "CompletionFoo.test11(3, 4,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 4
@test length(c) == 2
@test any(str->occursin("test11(x::$Int, y::$Int, z)", str), c)
@test any(str->occursin("test11(::Any, ::Any, s::String)", str), c)
end
Expand Down
11 changes: 11 additions & 0 deletions test/ambiguous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,17 @@ let ambig = Ref{Int32}(0)
@test ambig[] == 1
end

fnoambig(::Int,::Int) = 1
fnoambig(::Int,::Any) = 2
fnoambig(::Any,::Int) = 3
fnoambig(::Any,::Any) = 4
let has_ambig = Ref(Int32(0))
ms = Base._methods_by_ftype(Tuple{typeof(fnoambig), Any, Any}, nothing, 4, Base.get_world_counter(), false, Ref(typemin(UInt)), Ref(typemax(UInt)), has_ambig)
@test ms isa Vector
@test length(ms) == 4
@test has_ambig[] == 0
end

# issue #11407
f11407(::Dict{K,V}, ::Dict{Any,V}) where {K,V} = 1
f11407(::Dict{K,V}, ::Dict{K,Any}) where {K,V} = 2
Expand Down
2 changes: 1 addition & 1 deletion test/compiler/AbstractInterpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ end |> !Core.Compiler.is_nonoverlayed
end |> !Core.Compiler.is_nonoverlayed

# account for overlay possibility in unanalyzed matching method
callstrange(::Nothing) = Core.compilerbarrier(:type, nothing) # trigger inference bail out
callstrange(::Float64) = strangesin(x)
callstrange(::Nothing) = Core.compilerbarrier(:type, nothing) # trigger inference bail out
callstrange_entry(x) = callstrange(x) # needs to be defined here because of world age
let interp = MTOverlayInterp(Set{Any}())
matches = Core.Compiler.findall(Tuple{typeof(callstrange),Any}, Core.Compiler.method_table(interp)).matches
Expand Down
11 changes: 9 additions & 2 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,15 @@ method_c2(x::Int32, y::Float64) = true
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 occursin( "\n\nClosest candidates are:\n method_c2(!Matched::Int32, ::Float64, ::Any...)$cmod$cfile$(c2line+2)\n method_c2(::T, ::T, !Matched::T) where T<:Real$cmod$cfile$(c2line+5)\n method_c2(!Matched::Int32, ::Any...)$cmod$cfile$(c2line+1)\n ...\n", String(take!(buf)))
let s
Base.show_method_candidates(buf, Base.MethodError(method_c2, (1., 1., 2)))
s = String(take!(buf))
@test occursin("\n\nClosest candidates are:\n ", s)
@test occursin("\n method_c2(!Matched::Int32, ::Float64, ::Any...)$cmod$cfile$(c2line+2)\n ", s)
@test occursin("\n method_c2(::T, ::T, !Matched::T) where T<:Real$cmod$cfile$(c2line+5)\n ", s)
@test occursin("\n method_c2(!Matched::Int32, ::Any...)$cmod$cfile$(c2line+1)\n ", s)
@test occursin("\n ...\n", s)
end

c3line = @__LINE__() + 1
method_c3(x::Float64, y::Float64) = true
Expand Down
2 changes: 1 addition & 1 deletion test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ let
end

# code_typed_by_type
@test Base.code_typed_by_type(Tuple{Type{<:Val}})[1][2] == Val
@test Base.code_typed_by_type(Tuple{Type{<:Val}})[2][2] == Val
@test Base.code_typed_by_type(Tuple{typeof(sin), Float64})[1][2] === Float64

# New reflection methods in 0.6
Expand Down

0 comments on commit fbbe9ed

Please sign in to comment.