Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Iterators.flatten for empty tuple #29112

Merged
merged 3 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ IteratorEltype(::Type) = HasEltype() # HasEltype is the default
IteratorEltype(::Type{Generator{I,T}}) where {I,T} = EltypeUnknown()

IteratorEltype(::Type{Any}) = EltypeUnknown()
IteratorEltype(::Type{Union{}}) = EltypeUnknown()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this perhaps have known element type Union{}? I could imagine this pessimizing something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eltype(Union{}}) throws an error, so I think this is fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unfortunate in combination with

IteratorEltype(x) = IteratorEltype(typeof(x))

If x cannot be inferred exactly, we get typeof(x)::Type{<:Something}, which includes Type{Union{}}, although x will of course never be ::Union{}. E.g.

julia> code_warntype(Base.IteratorEltype, Tuple{Array{Int}})
Body::Union{EltypeUnknown, HasEltype}
123 1%1 = (Base.typeof)(x)::Type{#s55} where #s55<:Array{Int64,N} where N                                    │%2 = (isa)(%1, Type{Union{}})::Bool                                                                     │
    └──      goto #3 if not %2                                                                                  │
    2 ─      goto #4                                                                                            │
    3%5 = (Base.IteratorEltype)(%1)::Union{EltypeUnknown, HasEltype}                                         │
    └──      goto #4                                                                                            │
    4%7 = φ (#2 => $(QuoteNode(Base.EltypeUnknown())), #3 => %5)::Union{EltypeUnknown, HasEltype}            │
    └──      return %7   

Compare with this method removed:

julia> code_warntype(Base.IteratorEltype, Tuple{Array{Int}})
Body::Base.HasEltype
123 1return $(QuoteNode(Base.HasEltype()))  

This causes the regression noted in #29464.

Copy link
Contributor

@JeffreySarnoff JeffreySarnoff Oct 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw

julia> code_warntype(Base.IteratorEltype, Tuple{Array{Int}})
Body::Base.HasEltype
123 1 ─     return $(QuoteNode(Base.HasEltype()))

julia> versioninfo()
Julia Version 1.0.0
Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)
OS: Linux (x86_64-pc-linux-gnu)
LLVM: libLLVM-6.0.0 (ORCJIT, skylake)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the point here is that there is a regression in 1.0.1 vs 1.0.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be changed to throw an error instead, like eltype.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that won't quite work. Instead we can add a special case for flatten when eltype(I) is Union{}.

1 change: 1 addition & 0 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ _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()
Expand Down
1 change: 1 addition & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ end
@test length(flatten(zip(1:3, 4:6))) == 6
@test length(flatten(1:6)) == 6
@test collect(flatten(Any[])) == Any[]
@test collect(flatten(())) == Union{}[]
@test_throws ArgumentError length(flatten(NTuple[(1,), ()])) # #16680
@test_throws ArgumentError length(flatten([[1], [1]]))

Expand Down