@@ -1348,31 +1348,44 @@ function MethodList(mt::Core.MethodTable)
13481348 return MethodList (ms, mt)
13491349end
13501350
1351+ function matches_to_methods (ms:: Array{Any,1} , mt:: Core.MethodTable , mod)
1352+ # Lack of specialization => a comprehension triggers too many invalidations via _collect, so collect the methods manually
1353+ ms = Method[(ms[i]:: Core.MethodMatch ). method for i in 1 : length (ms)]
1354+ # Remove shadowed methods with identical type signatures
1355+ prev = nothing
1356+ filter! (ms) do m
1357+ l = prev
1358+ repeated = (l isa Method && m. sig == l. sig)
1359+ prev = m
1360+ return ! repeated
1361+ end
1362+ # Remove methods not part of module (after removing shadowed methods)
1363+ mod === nothing || filter! (ms) do m
1364+ return parentmodule (m) ∈ mod
1365+ end
1366+ return MethodList (ms, mt)
1367+ end
1368+
13511369"""
13521370 methods(f, [types], [module])
13531371
13541372Return the method table for `f`.
13551373
13561374If `types` is specified, return an array of methods whose types match.
13571375If `module` is specified, return an array of methods defined in that module.
1358- A list of modules can also be specified as an array.
1376+ A list of modules can also be specified as an array or set .
13591377
13601378!!! compat "Julia 1.4"
13611379 At least Julia 1.4 is required for specifying a module.
13621380
13631381See also: [`which`](@ref), [`@which`](@ref Main.InteractiveUtils.@which) and [`methodswith`](@ref Main.InteractiveUtils.methodswith).
13641382"""
13651383function methods (@nospecialize (f), @nospecialize (t),
1366- mod:: Union{Tuple{Module},AbstractArray{Module},Nothing} = nothing )
1384+ mod:: Union{Tuple{Module},AbstractArray{Module},AbstractSet{Module}, Nothing} = nothing )
13671385 world = get_world_counter ()
13681386 world == typemax (UInt) && error (" code reflection cannot be used from generated functions" )
1369- # Lack of specialization => a comprehension triggers too many invalidations via _collect, so collect the methods manually
1370- ms = Method[]
1371- for m in _methods (f, t, - 1 , world):: Vector
1372- m = m:: Core.MethodMatch
1373- (mod === nothing || parentmodule (m. method) ∈ mod) && push! (ms, m. method)
1374- end
1375- MethodList (ms, typeof (f). name. mt)
1387+ ms = _methods (f, t, - 1 , world):: Vector{Any}
1388+ return matches_to_methods (ms, typeof (f). name. mt, mod)
13761389end
13771390methods (@nospecialize (f), @nospecialize (t), mod:: Module ) = methods (f, t, (mod,))
13781391
@@ -1382,12 +1395,12 @@ function methods_including_ambiguous(@nospecialize(f), @nospecialize(t))
13821395 world == typemax (UInt) && error (" code reflection cannot be used from generated functions" )
13831396 min = RefValue {UInt} (typemin (UInt))
13841397 max = RefValue {UInt} (typemax (UInt))
1385- ms = _methods_by_ftype (tt, nothing , - 1 , world, true , min, max, Ptr {Int32} (C_NULL )):: Vector
1386- return MethodList (Method[(m :: Core.MethodMatch ) . method for m in ms] , typeof (f). name. mt)
1398+ ms = _methods_by_ftype (tt, nothing , - 1 , world, true , min, max, Ptr {Int32} (C_NULL )):: Vector{Any}
1399+ return matches_to_methods (ms , typeof (f). name. mt, nothing )
13871400end
13881401
13891402function methods (@nospecialize (f),
1390- mod:: Union{Module,AbstractArray{Module},Nothing} = nothing )
1403+ mod:: Union{Module,AbstractArray{Module},AbstractSet{Module}, Nothing} = nothing )
13911404 # return all matches
13921405 return methods (f, Tuple{Vararg{Any}}, mod)
13931406end
0 commit comments