Skip to content

methoddef!: use mt => sig format when filling in signatures #125

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 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version = "3.1.0"
JuliaInterpreter = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"

[compat]
JuliaInterpreter = "0.9"
JuliaInterpreter = "0.9.45"
julia = "1.10"

[extras]
Expand Down
2 changes: 1 addition & 1 deletion src/LoweredCodeUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using JuliaInterpreter
using JuliaInterpreter: SSAValue, SlotNumber, Frame
using JuliaInterpreter: @lookup, moduleof, pc_expr, step_expr!, is_global_ref, is_quotenode_egal, whichtt,
next_until!, finish_and_return!, get_return, nstatements, codelocation, linetable,
is_return, lookup_return
is_return, lookup_return, extract_method_table

include("packagedef.jl")

Expand Down
27 changes: 15 additions & 12 deletions src/signatures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ function signature(@nospecialize(recurse), frame::Frame, @nospecialize(stmt), pc
stmt = pc_expr(frame, pc)
end
isa(stmt, Expr) || return nothing, pc
mt = extract_method_table(frame, stmt; eval = false)
sigsv = @lookup(frame, stmt.args[2])::SimpleVector
sigt = signature(sigsv)
return sigt, lastpc
return mt => sigt, lastpc
end
signature(@nospecialize(recurse), frame::Frame, pc) = signature(recurse, frame, pc_expr(frame, pc), pc)
signature(frame::Frame, pc) = signature(finish_and_return!, frame, pc)
Expand Down Expand Up @@ -439,9 +440,9 @@ function get_running_name(@nospecialize(recurse), frame, pc, name, parentname)
pctop -= 1
stmt = pc_expr(frame, pctop)
end # end fix
sigtparent, lastpcparent = signature(recurse, frame, pctop)
(mt, sigtparent), lastpcparent = signature(recurse, frame, pctop)
sigtparent === nothing && return name, pc, lastpcparent
methparent = whichtt(sigtparent)
methparent = whichtt(sigtparent, mt)
methparent === nothing && return name, pc, lastpcparent # caller isn't defined, no correction is needed
if isgen
cname = GlobalRef(moduleof(frame), nameof(methparent.generator.gen))
Expand Down Expand Up @@ -507,6 +508,8 @@ function skip_until!(predicate, @nospecialize(recurse), frame)
return pc
end

method_table(method::Method) = isdefined(method, :external_mt) ? method.external_mt::MethodTable : nothing

"""
ret = methoddef!(recurse, signatures, frame; define=true)
ret = methoddef!(signatures, frame; define=true)
Expand Down Expand Up @@ -536,22 +539,22 @@ function methoddef!(@nospecialize(recurse), signatures, frame::Frame, @nospecial
if ismethod3(stmt)
pc3 = pc
arg1 = stmt.args[1]
sigt, pc = signature(recurse, frame, stmt, pc)
meth = whichtt(sigt)
(mt, sigt), pc = signature(recurse, frame, stmt, pc)
meth = whichtt(sigt, mt)
if isa(meth, Method) && (meth.sig <: sigt && sigt <: meth.sig)
pc = define ? step_expr!(recurse, frame, stmt, true) : next_or_nothing!(recurse, frame)
elseif define
pc = step_expr!(recurse, frame, stmt, true)
meth = whichtt(sigt)
meth = whichtt(sigt, mt)
end
if isa(meth, Method) && (meth.sig <: sigt && sigt <: meth.sig)
push!(signatures, meth.sig)
push!(signatures, mt => meth.sig)
else
if arg1 === false || arg1 === nothing
# If it's anonymous and not defined, define it
pc = step_expr!(recurse, frame, stmt, true)
meth = whichtt(sigt)
isa(meth, Method) && push!(signatures, meth.sig)
meth = whichtt(sigt, mt)
isa(meth, Method) && push!(signatures, mt => meth.sig)
return pc, pc3
else
# guard against busted lookup, e.g., https://github.com/JuliaLang/julia/issues/31112
Expand Down Expand Up @@ -592,7 +595,7 @@ function methoddef!(@nospecialize(recurse), signatures, frame::Frame, @nospecial
end
found || return nothing
while true # methods containing inner methods may need multiple trips through this loop
sigt, pc = signature(recurse, frame, stmt, pc)
(mt, sigt), pc = signature(recurse, frame, stmt, pc)
stmt = pc_expr(frame, pc)
while !isexpr(stmt, :method, 3)
pc = next_or_nothing(recurse, frame, pc) # this should not check define, we've probably already done this once
Expand All @@ -607,8 +610,8 @@ function methoddef!(@nospecialize(recurse), signatures, frame::Frame, @nospecial
# signature of the active method. So let's get the active signature.
frame.pc = pc
pc = define ? step_expr!(recurse, frame, stmt, true) : next_or_nothing!(recurse, frame)
meth = whichtt(sigt)
isa(meth, Method) && push!(signatures, meth.sig) # inner methods are not visible
meth = whichtt(sigt, mt)
isa(meth, Method) && push!(signatures, mt => meth.sig) # inner methods are not visible
name === name3 && return pc, pc3 # if this was an inner method we should keep going
stmt = pc_expr(frame, pc) # there *should* be more statements in this frame
end
Expand Down
43 changes: 34 additions & 9 deletions test/signatures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bodymethtest5(x, y=Dict(1=>2)) = 5

# Manually add the signature for the Caller constructor, since that was defined
# outside of manual lowering
push!(signatures, Tuple{Type{Lowering.Caller}})
push!(signatures, nothing => Tuple{Type{Lowering.Caller}})

nms = names(Lowering; all=true)
modeval, modinclude = getfield(Lowering, :eval), getfield(Lowering, :include)
Expand All @@ -107,7 +107,7 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
isa(f, Base.Callable) || continue
(f === modeval || f === modinclude) && continue
for m in methods(f)
if m.sig ∉ signatures
if (nothing => m.sig) ∉ signatures
push!(failed, m.sig)
end
end
Expand Down Expand Up @@ -151,7 +151,8 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
rename_framemethods!(frame)
methoddefs!(signatures, frame; define=false)
@test length(signatures) == 1
@test LoweredCodeUtils.whichtt(signatures[1]) == first(methods(Lowering.fouter))
mt, sig = first(signatures)
@test LoweredCodeUtils.whichtt(sig, mt) == first(methods(Lowering.fouter))

# Check output of methoddef!
frame = Frame(Lowering, :(function nomethod end))
Expand All @@ -170,7 +171,8 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
signatures = Set{Any}()
methoddef!(signatures, frame; define=false)
@test length(signatures) == 1
@test first(signatures) == which(Base.max_values, Tuple{Type{Int16}}).sig
mt, sig = first(signatures)
@test sig == which(Base.max_values, Tuple{Type{Int16}}).sig

# define
ex = :(fdefine(x) = 1)
Expand Down Expand Up @@ -291,7 +293,8 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
JuliaInterpreter.next_until!(LoweredCodeUtils.ismethod3, frame, true)
empty!(signatures)
methoddefs!(signatures, frame; define=true)
@test first(signatures).parameters[end] == Int
mt, sig = first(signatures)
@test sig.parameters[end] == Int

# Multiple keyword arg methods per frame
# (Revise issue #363)
Expand All @@ -310,7 +313,7 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
@test kw2sig ∉ signatures
pc = methoddefs!(signatures, frame; define=false)
@test pc === nothing
@test kw2sig ∈ signatures
@test (nothing => kw2sig) ∈ signatures

# Module-scoping
ex = :(Base.@irrational π 3.14159265358979323846 pi)
Expand All @@ -336,7 +339,7 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
rename_framemethods!(frame)
empty!(signatures)
methoddefs!(signatures, frame; define=false)
@test Tuple{typeof(Lowering.CustomMS)} ∈ signatures
@test (nothing => Tuple{typeof(Lowering.CustomMS)}) ∈ signatures

# https://github.com/timholy/Revise.jl/issues/398
ex = quote
Expand Down Expand Up @@ -370,7 +373,7 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
frame = Frame(Lowering422, ex)
rename_framemethods!(frame)
pc = methoddefs!(signatures, frame; define=false)
@test typeof(Lowering422.fneg) ∈ Set(Base.unwrap_unionall(sig).parameters[1] for sig in signatures)
@test typeof(Lowering422.fneg) ∈ Set(Base.unwrap_unionall(sig).parameters[1] for (mt, sig) in signatures)

# Scoped names (https://github.com/timholy/Revise.jl/issues/568)
ex = :(f568() = -1)
Expand All @@ -385,7 +388,7 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
pc = JuliaInterpreter.step_expr!(finish_and_return!, frame, true)
end
pc = methoddef!(finish_and_return!, signatures, frame, pc; define=true)
@test Tuple{typeof(Lowering.f568)} ∈ signatures
@test (nothing => Tuple{typeof(Lowering.f568)}) ∈ signatures
@test Lowering.f568() == -2

# Undefined names
Expand Down Expand Up @@ -502,4 +505,26 @@ end

end

@testset "Support for external method tables" begin
ExternalMT = Module()
Core.eval(ExternalMT, :(Base.Experimental.@MethodTable method_table))
signatures = []

ex = :(Base.sin(::Float64) = "sin")
Core.eval(ExternalMT, ex)
frame = Frame(ExternalMT, ex)
pc = methoddefs!(signatures, frame; define = false)
@test length(signatures) == 1
(mt, sig) = pop!(signatures)
@test (mt, sig) === (nothing, Tuple{typeof(sin), Float64})

ex = :(Base.Experimental.@overlay method_table sin(::Float64) = "sin")
Core.eval(ExternalMT, ex)
frame = Frame(ExternalMT, ex)
pc = methoddefs!(signatures, frame; define = false)
@test length(signatures) == 1
(mt, sig) = pop!(signatures)
@test (mt, sig) === (ExternalMT.method_table, Tuple{typeof(sin), Float64})
end

end # module signatures
Loading