From 268c2758a372f2ceec74bd6ada0c4666cf8a4c5c Mon Sep 17 00:00:00 2001 From: Jakob Peters Date: Mon, 16 Dec 2024 06:59:52 -0800 Subject: [PATCH] Visit callable objects --- src/visit.jl | 15 ++++++++++++++- test/runtests.jl | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/visit.jl b/src/visit.jl index 59ff1f3..ddb29f1 100644 --- a/src/visit.jl +++ b/src/visit.jl @@ -223,7 +223,20 @@ if isdefined(Core, :CodeInstance) end end -_visit(@nospecialize(operation), @nospecialize(x), visited::IdSet{Any}, print::Bool) = nothing +function _visit(@nospecialize(operation), @nospecialize(x), visited::IdSet{Any}, print::Bool) + x ∈ visited && return nothing + x === Vararg && return nothing + push!(visited, x) + print && (show(x); println()) + if operation(x) + ml = methods(x) + _visit(operation, ml.mt, visited, print) + for m in ml.ms + _visit(operation, m, visited, print) + end + end + return nothing +end """ visit_backedges(operation, obj) diff --git a/test/runtests.jl b/test/runtests.jl index 1a62c9d..dcc5eab 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -56,6 +56,11 @@ module Two One.f(x::Int) = x + 2 end +module CallableObjects + struct Foo end + (::Foo)() = 1 + Foo()() +end @testset "visit" begin # Do we pick up kwfuncs? @@ -121,6 +126,15 @@ end isa(m, Method) && @test Base.unwrap_unionall(m.sig).parameters[1].parameters[1] === IndexStyle return m === IndexStyle end + + # See issue #50 + foo = CallableObjects.Foo() + empty!(mis) + visit(foo) do x + isa(x, Core.MethodInstance) && push!(mis, x) + true + end + @test only(mis) == only(methods(foo)).specializations end @testset "visit_withmodule" begin