Skip to content

Commit

Permalink
flatten empty tuple - fix #29112 (#29548)
Browse files Browse the repository at this point in the history
(cherry picked from commit 38612e0)
  • Loading branch information
KlausC authored and KristofferC committed Feb 20, 2020
1 parent 4608f87 commit 776a9ba
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
1 change: 0 additions & 1 deletion base/generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,3 @@ IteratorEltype(::Type) = HasEltype() # HasEltype is the default
IteratorEltype(::Type{Generator{I,T}}) where {I,T} = EltypeUnknown()

IteratorEltype(::Type{Any}) = EltypeUnknown()
IteratorEltype(::Type{Union{}}) = EltypeUnknown()
5 changes: 4 additions & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -873,18 +873,20 @@ julia> collect(Iterators.flatten((1:2, 8:9)))
flatten(itr) = Flatten(itr)

eltype(::Type{Flatten{I}}) where {I} = eltype(eltype(I))
eltype(::Type{Flatten{Tuple{}}}) = eltype(Tuple{})
IteratorEltype(::Type{Flatten{I}}) where {I} = _flatteneltype(I, IteratorEltype(I))
IteratorEltype(::Type{Flatten{Tuple{}}}) = IteratorEltype(Tuple{})
_flatteneltype(I, ::HasEltype) = IteratorEltype(eltype(I))
_flatteneltype(I, et) = EltypeUnknown()

flatten_iteratorsize(::Union{HasShape, HasLength}, ::Type{<:NTuple{N,Any}}) where {N} = HasLength()
flatten_iteratorsize(::Union{HasShape, HasLength}, ::Type{<:Tuple}) = SizeUnknown()
flatten_iteratorsize(::Union{HasShape, HasLength}, ::Type{<:Number}) = HasLength()
flatten_iteratorsize(::Union{HasShape, HasLength}, ::Type{Union{}}) = SizeUnknown()
flatten_iteratorsize(a, b) = SizeUnknown()

_flatten_iteratorsize(sz, ::EltypeUnknown, I) = SizeUnknown()
_flatten_iteratorsize(sz, ::HasEltype, I) = flatten_iteratorsize(sz, eltype(I))
_flatten_iteratorsize(sz, ::HasEltype, ::Type{Tuple{}}) = HasLength()

IteratorSize(::Type{Flatten{I}}) where {I} = _flatten_iteratorsize(IteratorSize(I), IteratorEltype(I), I)

Expand All @@ -895,6 +897,7 @@ flatten_length(f, ::Type{<:Number}) = length(f.it)
flatten_length(f, T) = throw(ArgumentError(
"Iterates of the argument to Flatten are not known to have constant length"))
length(f::Flatten{I}) where {I} = flatten_length(f, eltype(I))
length(f::Flatten{Tuple{}}) = 0

@propagate_inbounds function iterate(f::Flatten, state=())
if state !== ()
Expand Down
2 changes: 2 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ end
@test_throws ArgumentError length(flatten([[1], [1]]))

@test Base.IteratorEltype(Base.Flatten((i for i=1:2) for j=1:1)) == Base.EltypeUnknown()
# see #29112, #29464, #29548
@test Base.return_types(Base.IteratorEltype, Tuple{Array}) == [Base.HasEltype]

# partition(c, n)
let v = collect(partition([1,2,3,4,5], 1))
Expand Down

0 comments on commit 776a9ba

Please sign in to comment.