Error: "all-underscore identifier used as rvalue"

I am trying to understand why the following error is thrown:

julia> function foo1(x, _, y; z)
         @show x, y, z
ERROR: syntax: all-underscore identifier used as rvalue around REPL[2]:1
 [1] top-level scope
   @ REPL[2]:1

julia> function foo1(x, _, y)
         @show x, y
foo1 (generic function with 1 method)

Note that the _ argument is not used in foo1.
Also note that the error disappears when there are no keyword arguments.
What is going on here?

A function with keyword arguments creates a hidden helper function that places all the keyword arguments into a positional function call. Something vaguely like

function foo1(x, _, y; z)
  z_positional = z
  _foo1_internal(x, _, y, z_positional) # <-- the problem arises here

function _foo1_internal(x, _, y, z)
  @show x,y,z

So the issue is that it isn’t being very smart with the helper function and is trying to forward the _ argument. This means it shows up as a value on the right hand side of a statement (“rvalue”) and you get the confusing error that you saw.

There’s an open issue for this #32727 but it hasn’t been addressed yet.