Skip to content

Commit 899ae12

Browse files
authored
Support direct_backedges(::MethodInstance) (#25)
This is a small convenience that avoids the risk of an `UndefRefError` from `mi.backedges`
1 parent d40304b commit 899ae12

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MethodAnalysis"
22
uuid = "85b6ec6f-f7df-4429-9514-a64bcd9ee824"
33
authors = ["Tim Holy <[email protected]>"]
4-
version = "0.4.3"
4+
version = "0.4.4"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/backedges.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252
5353
Collect all backedges for a function `f` as pairs `instance=>caller` or `sig=>caller` pairs.
5454
The latter occur for MethodTable backedges.
55-
If `skip` is `true`, any `caller` listed in a MethodTable backedge is omitted from the instance backedges.
55+
If `skip` is `true`, any `caller` listed in a MethodTable backedge is omitted from the instance backedges.
5656
"""
5757
function direct_backedges(f::Union{Method,Callable}; skip::Bool=true)
5858
bes = []
@@ -83,3 +83,17 @@ function direct_backedges(f::Union{Method,Callable}; skip::Bool=true)
8383
end
8484
return bes
8585
end
86+
87+
"""
88+
direct_backedges(mi::MethodInstance)
89+
90+
A vector of all direct backedges of `mi`. This is equivalent to `mi.backedges` except that it's "safe,"
91+
meaning it returns an empty list even when `mi.backedges` is not defined.
92+
"""
93+
function direct_backedges(mi::MethodInstance)
94+
out = MethodInstance[]
95+
if isdefined(mi, :backedges)
96+
append!(out, mi.backedges)
97+
end
98+
return out
99+
end

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ end
141141
pr = bes[2]
142142
@test pr.first == methodinstance(f, (Integer,))
143143
@test pr.second == methodinstance(applyf, (Vector{Any},))
144+
145+
nocallers(x) = x
146+
nocallers(3)
147+
mi = methodinstance(nocallers, (Int,))
148+
@test isempty(direct_backedges(mi))
149+
callnocallers(x) = nocallers(x)
150+
callnocallers(3)
151+
@test !isempty(direct_backedges(mi))
144152
end
145153

146154
@testset "call_type" begin

0 commit comments

Comments
 (0)