Strange behavior with list comprehensions using Iterators.Stateful

I’m having trouble to understand the following behavior of Iterators.Stateful. This is as expected

julia> for a in Iterators.Stateful([1,2,3])
             print(a)
         end
123

But this seems strange:

julia> [a for a in Iterators.Stateful([1,2,3])]
2-element Array{Int64,1}:
 1
 2
julia> [a for a in Iterators.Stateful([1])]
ERROR: BoundsError: attempt to access 0-element Array{Int64,1} at index [1]

Is this a bug, or is there something I don’t understand here?

Looking at the code

https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L630-L643

The iterate call at

https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L636

makes length(itr) go from 1 to 0.

Then in _array_for(typeof(v1), itr.iter, isz):

https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L627

a length 0 vector is allocated which is then tried to get assigned into:

https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L657-L661

I’m guessing the axis / length should be computed before the call to iterate.

That quite looks like a bug, is there issue for it on github?

Don’t think so.