Skip to content
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

Fix generator-invocation legality check for varargs generators #47739

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1165,13 +1165,25 @@ function may_invoke_generator(method::Method, @nospecialize(atype), sparams::Sim
end
end
end
for i = 1:length(at.parameters)
non_va_args = method.isva ? method.nargs - 1 : method.nargs
for i = 1:non_va_args
if !isdispatchelem(at.parameters[i])
if (ast_slotflag(code, 1 + i + nsparams) & SLOT_USED) != 0
return false
end
end
end
if method.isva
# If the va argument is used, we need to ensure that all arguments that
# contribute to the va tuple are dispatchelemes
if (ast_slotflag(code, 1 + method.nargs + nsparams) & SLOT_USED) != 0
for i = (non_va_args+1):length(at.parameters)
if !isdispatchelem(at.parameters[i])
return false
end
end
end
end
return true
end

Expand Down
15 changes: 15 additions & 0 deletions test/staged.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,18 @@ end
end
@test f33243() === 2
@test x33243 === 2

# https://github.com/JuliaDebug/CassetteOverlay.jl/issues/12
# generated function with varargs and unfortunately placed unused slot
@generated function f_vararg_generated(args...)
:($args)
end
g_vararg_generated() = f_vararg_generated((;), (;), Base.inferencebarrier((;)))
let tup = g_vararg_generated()
@test !any(==(Any), tup)
# This is just to make sure that the test is actually testing what we want -
# the test only works if there's an unused that matches the position of the
# inferencebarrier argument above (N.B. the generator function itself
# shifts everything over by 1)
@test code_lowered(first(methods(f_vararg_generated)).generator.gen)[1].slotflags[5] == UInt8(0x00)
end