Skip to content

Commit

Permalink
Add full module context to types (fixes #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jan 3, 2020
1 parent 255f316 commit d2d23fb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/parcel_snoopi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ function add_if_evals!(pclist, mod::Module, fstr, params, tt; prefix = "")
return pclist
end

function reprcontext(mod::Module, @nospecialize(T::Type))
# First try without the full module context
rplain = repr(T)
try
ex = Meta.parse(rplain)
Core.eval(mod, ex)
return rplain
catch
# Add full module context
rdec = repr(T; context=:module=>nothing)
ex = Meta.parse(rdec)
Core.eval(mod, ex)
return rdec
end
end

function handle_kwbody(topmod::Module, m::Method, paramrepr, tt, fstr="fbody")
nameparent = Symbol(match(r"^#([^#]*)#", String(m.name)).captures[1])
if !isdefined(m.module, nameparent) # TODO: replace debugging with error-handling
Expand Down Expand Up @@ -213,7 +229,7 @@ function parcel(tinf::AbstractVector{Tuple{Float64,Core.MethodInstance}}; subst=
# Create the string representation of the signature
# Use special care with keyword functions, anonymous functions
p = tt.parameters[1] # the portion of the signature related to the function itself
paramrepr = map(repr, Iterators.drop(tt.parameters, 1)) # all the rest of the args
paramrepr = map(T->reprcontext(topmod, T), Iterators.drop(tt.parameters, 1)) # all the rest of the args
if any(str->occursin('#', str), paramrepr)
@debug "Skipping $tt due to argument types having anonymous bindings"
continue
Expand Down Expand Up @@ -279,7 +295,7 @@ function parcel(tinf::AbstractVector{Tuple{Float64,Core.MethodInstance}}; subst=
fstr = "getfield($mmod, Symbol(\"$mname\"))" # this is universal, var is Julia 1.3+
add_if_evals!(pc[topmodname], topmod, fstr, paramrepr, tt; prefix=prefix)
else
add_if_evals!(pc[topmodname], topmod, repr(p), paramrepr, tt)
add_if_evals!(pc[topmodname], topmod, reprcontext(topmod, p), paramrepr, tt)
end
end
return pc
Expand Down
11 changes: 11 additions & 0 deletions test/snoopi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ push!(LOAD_PATH, joinpath(@__DIR__, "testmodules"))
using A
using E
using FuncKinds
using Reachable.ModuleA
using Reachable.ModuleB
pop!(LOAD_PATH)

@testset "topmodule" begin
Expand Down Expand Up @@ -174,6 +176,15 @@ end

end

@testset "Reachable" begin
tinf = @snoopi begin
include("snoopreachable.jl")
end
pc = SnoopCompile.parcel(tinf)
pcd = pc[:Reachable]
@test length(pcd) == 2
end

@testset "@snoopi docs" begin
# docstring is present (weird Docs bug)
dct = Docs.meta(SnoopCompile)
Expand Down
2 changes: 2 additions & 0 deletions test/snoopreachable.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = RchA()
rchb(a)
14 changes: 14 additions & 0 deletions test/testmodules/Reachable.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Reachable

module ModuleA
export RchA
struct RchA end
end # ModuleA

module ModuleB
using Reachable.ModuleA
export rchb
rchb(::RchA) = "hello"
end # ModuleB

end # Reachable

0 comments on commit d2d23fb

Please sign in to comment.