Skip to content

Commit 89ae151

Browse files
authored
Relax is_self_call to allow empty names (#19)
Also make it standalone so it can be called directly by Revise.
1 parent 55d0ec5 commit 89ae151

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

Project.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ version = "0.3.7"
66
[deps]
77
JuliaInterpreter = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"
88

9-
[extras]
10-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
11-
129
[compat]
1310
JuliaInterpreter = "0.3, 0.4, 0.5, 0.6, 0.7"
1411

12+
[extras]
13+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
14+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
15+
1516
[targets]
16-
test = ["Test"]
17+
test = ["InteractiveUtils", "Test"]

src/LoweredCodeUtils.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -478,26 +478,28 @@ function _methoddefs!(@nospecialize(recurse), signatures, frame::Frame, pc; defi
478478
return pc
479479
end
480480

481+
function is_self_call(stmt, slotnames, argno=1)
482+
if isa(stmt, Expr)
483+
if stmt.head == :call
484+
a = stmt.args[argno]
485+
if isa(a, SlotNumber) || isa(a, Core.SlotNumber)
486+
sn = slotnames[a.id]
487+
if sn == Symbol("#self#") || sn == Symbol("") # allow empty to fix https://github.com/timholy/CodeTracking.jl/pull/48
488+
return true
489+
end
490+
end
491+
end
492+
end
493+
return false
494+
end
495+
481496
"""
482497
mbody = bodymethod(m::Method)
483498
484499
Return the "body method" for a method `m`. `mbody` contains the code of the function body
485500
when `m` was defined.
486501
"""
487502
function bodymethod(mkw::Method)
488-
function is_self_call(stmt, slotnames, argno=1)
489-
if isa(stmt, Expr)
490-
if stmt.head == :call
491-
a = stmt.args[argno]
492-
if isa(a, SlotNumber)
493-
if slotnames[a.id] == Symbol("#self#")
494-
return true
495-
end
496-
end
497-
end
498-
end
499-
return false
500-
end
501503
m = mkw
502504
local src
503505
while true
@@ -519,7 +521,7 @@ function bodymethod(mkw::Method)
519521
stmt = src.code[end-1]
520522
if isexpr(stmt, :call) && (f = stmt.args[1]; isa(f, QuoteNode))
521523
# Check that it has a #self# call
522-
hasself = any(i->is_self_call(stmt, src.slotnames, i), 1:length(stmt.args))
524+
hasself = any(i->is_self_call(stmt, src.slotnames, i), 2:length(stmt.args))
523525
hasself || return m
524526
f = f.value
525527
mths = methods(f)

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using InteractiveUtils
12
using LoweredCodeUtils, JuliaInterpreter
23
using JuliaInterpreter: finish_and_return!
34
using Core: CodeInfo
@@ -250,6 +251,10 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
250251
@test startswith(String(bodymethod(first(methods(bodymethtest3))).name), "#")
251252
@test bodymethod(first(methods(bodymethtest4))).nargs == 3 # one extra for #self#
252253
@test bodymethod(first(methods(bodymethtest5))).nargs == 3
254+
m = @which sum([1]; dims=1)
255+
# Issue in https://github.com/timholy/CodeTracking.jl/pull/48
256+
mbody = bodymethod(m)
257+
@test mbody != m && mbody.file != :none
253258

254259
ex = :(typedsig(x) = 1)
255260
frame = JuliaInterpreter.prepare_thunk(Lowering, ex)

0 commit comments

Comments
 (0)