Skip to content

Commit

Permalink
Fix generator-invocation legality check for varargs generators (#47739)
Browse files Browse the repository at this point in the history
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
  • Loading branch information
Keno authored and KristofferC committed Dec 21, 2022
1 parent 19c7e2b commit d2eb584
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1084,13 +1084,25 @@ function may_invoke_generator(method::Method, @nospecialize(atypes), sparams::Si
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 @@ -304,3 +304,18 @@ end
:(global x33243 = 2)
end
@test_throws ErrorException f33243()

# 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

0 comments on commit d2eb584

Please sign in to comment.