Skip to content

Commit 5008548

Browse files
authored
Merge pull request #14 from JuliaDebug/teh/genkw
Support generated functions with kwargs
2 parents e5b770d + 783a130 commit 5008548

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/LoweredCodeUtils.jl

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,34 @@ function methoddef!(@nospecialize(recurse), signatures, frame::Frame, @nospecial
387387
error("not valid for anonymous methods")
388388
end
389389
parentname = get_parentname(name) # e.g., name = #foo#7 and parentname = foo
390-
nextstmt = pc_expr(frame, pc+1)
391-
if ismethod1(nextstmt)
392-
name = nextstmt.args[1]
390+
pcinc = 1
391+
nextstmt = pc_expr(frame, pc+pcinc)
392+
while ismethod1(nextstmt) || isexpr(nextstmt, :global)
393+
if ismethod1(nextstmt)
394+
name = nextstmt.args[1]
395+
end
396+
pcinc += 1
397+
nextstmt = pc_expr(frame, pc+pcinc)
398+
end
399+
if !define && String(name)[1] == '#'
400+
# We will have to correct the name.
401+
# We can only correct one at a time, so work backwards from a non-gensymmed name
402+
# (https://github.com/timholy/Revise.jl/issues/290)
403+
pc0 = pc
404+
idx1 = findall(ismethod1, frame.framecode.src.code)
405+
idx1 = idx1[idx1 .>= pc]
406+
i = length(idx1)
407+
while i > 1 && !startswith(String(pc_expr(frame, idx1[i]).args[1]), '#')
408+
i -= 1
409+
end
410+
while i > 1
411+
frame.pc = idx1[i]
412+
methoddef!(recurse, [], frame, frame.pc; define=define)
413+
i -= 1
414+
end
415+
frame.pc = pc0
393416
end
394-
if name != parentname
417+
if name != parentname && !define
395418
name, endpc = correct_name!(recurse, frame, pc, name, parentname)
396419
end
397420
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)