Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
013fe08
Parametrize signatures by method table
serenity4 Apr 18, 2025
4610b4b
Add tests
serenity4 Apr 18, 2025
f53718b
Update src/signatures.jl
serenity4 Apr 21, 2025
6ee24af
Update docstring
serenity4 Apr 21, 2025
1ecbfcf
Tighten the type of `signatures`
serenity4 Apr 21, 2025
6d3e785
Merge branch 'support-external-methodtables' of github.com:serenity4/…
serenity4 Apr 21, 2025
4d713f1
Merge branch 'master' of github.com:JuliaDebug/LoweredCodeUtils.jl in…
serenity4 Apr 21, 2025
80581f0
Use MethodInfoKey from CodeTracking
serenity4 Apr 21, 2025
1910570
Add CodeTracking as a direct dependency
serenity4 Apr 21, 2025
118978a
Remove `method_table` definition
serenity4 Apr 21, 2025
d8bd0bd
Don't accidentally override `Base.sin(::Float64)`
serenity4 Apr 21, 2025
73d89d5
Temporarily add CodeTracking PR for CI testing
serenity4 Apr 21, 2025
b8520fc
Fix patch for Documenter on CI
serenity4 Apr 21, 2025
8548679
Add missing handling for methods defined for external method tables
serenity4 Apr 24, 2025
36961bf
Merge branch 'master' of github.com:JuliaDebug/LoweredCodeUtils.jl in…
serenity4 Apr 24, 2025
0b58387
Adjust docs, add type annotations
serenity4 Apr 25, 2025
b985309
Update CI dependency patch
serenity4 Jun 10, 2025
e624e99
Test function definition, not method definition
serenity4 Jun 10, 2025
8d8784f
Update CI docs dependency patch
serenity4 Jun 10, 2025
f57ed81
Update src/codeedges.jl
serenity4 Jul 15, 2025
2cada0d
Merge branch 'master' of github.com:JuliaDebug/LoweredCodeUtils.jl in…
serenity4 Jul 24, 2025
154f937
Remove CI patches
serenity4 Jul 24, 2025
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