Skip to content

Commit d485b8c

Browse files
committed
Support generated functions with kwargs
Fixes timholy/Revise.jl#290
1 parent e5b770d commit d485b8c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/LoweredCodeUtils.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,30 @@ function methoddef!(@nospecialize(recurse), signatures, frame::Frame, @nospecial
386386
if isa(name, Bool)
387387
error("not valid for anonymous methods")
388388
end
389+
if !define && String(name)[1] == '#'
390+
# We will have to correct the name.
391+
# We can only correct one at a time, so work backwards from a non-gensymmed name
392+
# (https://github.com/timholy/Revise.jl/issues/290)
393+
pc0 = pc
394+
idx1 = findall(ismethod1, frame.framecode.src.code)
395+
idx1 = idx1[idx1 .>= pc]
396+
i = 1
397+
while i < length(idx1) && startswith(String(pc_expr(frame, idx1[i]).args[1]), '#')
398+
i += 1
399+
end
400+
while i > 2
401+
i -= 1
402+
frame.pc = idx1[i]
403+
methoddef!(recurse, [], frame, frame.pc; define=define)
404+
end
405+
frame.pc = pc0
406+
end
389407
parentname = get_parentname(name) # e.g., name = #foo#7 and parentname = foo
390408
nextstmt = pc_expr(frame, pc+1)
391409
if ismethod1(nextstmt)
392410
name = nextstmt.args[1]
393411
end
394-
if name != parentname
412+
if name != parentname && !define
395413
name, endpc = correct_name!(recurse, frame, pc, name, parentname)
396414
end
397415
while true # methods containing inner methods may need multiple trips through this loop

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
5656
end
5757
end
5858
end,
59+
:(@generated genkw(; b=2) = nothing), # https://github.com/timholy/Revise.jl/issues/290
5960
# Generated constructors
6061
quote
6162
function Gen{T}(x) where T
@@ -203,6 +204,12 @@ bodymethtest5(x, y=Dict(1=>2)) = 5
203204
methoddefs!(signatures, frame; define=true)
204205
@test length(signatures) == 5
205206
@test Lowering.another_kwdef(0) == 333
207+
ex = :(@generated genkw2(; b=2) = nothing) # https://github.com/timholy/Revise.jl/issues/290
208+
frame = JuliaInterpreter.prepare_thunk(Lowering, ex)
209+
empty!(signatures)
210+
methoddefs!(signatures, frame; define=true)
211+
@test length(signatures) == 4
212+
@test Lowering.genkw2() === nothing
206213

207214
# Test for correct exit (example from base/namedtuples.jl)
208215
ex = quote

0 commit comments

Comments
 (0)