Skip to content

Commit 92f6dcd

Browse files
authored
Support MethodList (#15)
This is nice to collect MethodInstances from several methods.
1 parent 10fa78f commit 92f6dcd

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/MethodAnalysis.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ end
116116
methodinstances(mod::Module)
117117
methodinstances(f)
118118
119-
Collect all `MethodInstance`s, optionally restricting them to a particular module or function.
119+
Collect all `MethodInstance`s, optionally restricting them to a particular module, function, method, or methodlist.
120120
"""
121121
function methodinstances(top=())
122-
if isa(top, Module) || isa(top, Function) || isa(top, Type)
122+
if isa(top, Module) || isa(top, Function) || isa(top, Type) || isa(top, Method) || isa(top, Base.MethodList)
123123
top = (top,)
124124
end
125125
mis = Core.MethodInstance[]

src/visit.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ function _visit(@nospecialize(operation), @nospecialize(f::Callable), visited::I
100100
return nothing
101101
end
102102

103+
function _visit(@nospecialize(operation), ml::Base.MethodList, visited::IdSet{Any}, print::Bool)
104+
ml visited && return nothing
105+
push!(visited, ml)
106+
print && println("MethodList ", ml)
107+
_visit(operation, ml.mt, visited, print)
108+
for m in ml.ms
109+
_visit(operation, m, visited, print)
110+
end
111+
return nothing
112+
end
113+
114+
103115
function _visit(@nospecialize(operation), mt::MethodTable, visited::IdSet{Any}, print::Bool)
104116
mt visited && return nothing
105117
push!(visited, mt)

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ end
7979
@test mis isa Vector{Core.MethodInstance}
8080
@test mi mis
8181
@test length(mis) > 1
82+
83+
mi = methodinstance(convert, (Type{String}, String))
84+
mis = methodinstances(methods(convert, (Type{String}, Any)))
85+
@test length(mis) > 10 # in fact, there are many more
86+
@test mi mis
87+
mis = methodinstances(which(convert, (Type{String}, AbstractString)))
88+
@test length(mis) > 2
89+
@test mi mis # that's covered by a different Method
8290
end
8391

8492
@testset "Backedges" begin

0 commit comments

Comments
 (0)