`@inbounds` behaving strangely for varargs functions

I am puzzled by the behavior of @inbounds for functions with a variable number of arguments. My understanding is that calling the following function g should avoid a bounds check inside f.

@inline function f(x...)
    @boundscheck println("checking bounds")
    return 0
end

g(x...) = @inbounds f(x...)

However, this is what I get:

julia> g(1)     # OK
0
julia> g(1,1)   # why is this?
checking bounds
0
julia> g(1)     # and why does this suddenly change?
checking bounds
0

I’m using Julia 1.7.3.

When I examine g(1,1) with @descend, it actually removes the bounds check correctly.

When I run Debugger.@run g(1), it prints checking bounds even the first time round. Huh.

Maybe it’s because f(x...) is actually Core._apply_iterate(Base.iterate, Main.f, x), and that call doesn’t propagate inbounds?

If that’s true, then why is the bounds check skipped when calling g(1) the first time?

I’ve just tried again, and sometimes I don’t see a change in the behavior for g(1), but most times I do. g(1,1) always gives a bounds check.

@code_typed and @code_llvm also claim that there is no bounds check, neither for g(1) nor for g(1,1).

There is now a GitHub issue for this.