Skip to content

Visit callable objects #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/visit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down