Skip to content

Commit fe42838

Browse files
authored
follow up #66, allow method definition in other module (#67)
I found JuliaInterpreter can define a method even if its module isn't same as `moduleof(frame::Frame)`.
1 parent 6fa01bb commit fe42838

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/signatures.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ function identify_framemethod_calls(frame)
154154
elseif ismethod1(stmt)
155155
key = stmt.args[1]
156156
key = normalize_defsig(key, frame)
157-
key === missing && continue
158157
key = key::Symbol
159158
mi = get(methodinfos, key, nothing)
160159
if mi === nothing
@@ -165,7 +164,6 @@ function identify_framemethod_calls(frame)
165164
elseif ismethod3(stmt)
166165
key = stmt.args[1]
167166
key = normalize_defsig(key, frame)
168-
key === missing && continue
169167
if key isa Symbol
170168
mi = methodinfos[key]
171169
mi.stop = i
@@ -216,10 +214,8 @@ end
216214
# try to normalize `def` to `Symbol` representation
217215
function normalize_defsig(@nospecialize(def), frame::Frame)
218216
if def isa QuoteNode
219-
parentmodule(def.value) === moduleof(frame) || return false
220217
def = nameof(def.value)
221218
elseif def isa GlobalRef
222-
def.mod === moduleof(frame) || return false
223219
def = def.name
224220
end
225221
return def

test/signatures.jl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ using LoweredCodeUtils, JuliaInterpreter, Test
443443
macro deffoogr()
444444
gr = GlobalRef(__module__, :foogr) # will be lowered to `GlobalRef`
445445
quote
446-
$gr(args...) = length(args) + 2
446+
$gr(args...) = length(args)
447447
end
448448
end
449449
let
@@ -469,4 +469,35 @@ let
469469
@test haskey(methranges, :fooqn)
470470
end
471471

472+
# define methods in other module
473+
module sandboxgr end
474+
macro deffoogr_sandbox()
475+
gr = GlobalRef(sandboxgr, :foogr_sandbox) # will be lowered to `GlobalRef`
476+
quote
477+
$gr(args...) = length(args)
478+
end
479+
end
480+
let
481+
ex = quote
482+
@deffoogr_sandbox
483+
@show sandboxgr.foogr_sandbox(1,2,3)
484+
end
485+
methranges = rename_framemethods!(Frame(@__MODULE__, ex))
486+
@test haskey(methranges, :foogr_sandbox)
487+
end
488+
489+
module sandboxqn; function fooqn_sandbox end; end
490+
macro deffooqn_sandbox()
491+
sig = :($(GlobalRef(sandboxqn, :fooqn_sandbox))(args...)) # will be lowered to `QuoteNode`
492+
return Expr(:function, sig, Expr(:block, __source__, :(length(args))))
493+
end
494+
let
495+
ex = quote
496+
@deffooqn_sandbox
497+
@show sandboxqn.fooqn_sandbox(1,2,3)
498+
end
499+
methranges = rename_framemethods!(Frame(@__MODULE__, ex))
500+
@test haskey(methranges, :fooqn_sandbox)
501+
end
502+
472503
end

0 commit comments

Comments
 (0)