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

Allow Iterators.flatten for empty tuple #29112

merged 3 commits into from
Sep 12, 2018

Conversation

KlausC
Copy link
Contributor

@KlausC KlausC commented Sep 10, 2018

What happened:

$ julia

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.1.0-DEV.222 (2018-09-09)
 _/ |\__'_|_|_|\__'_|  |  Commit 97864d39ea (0 days old master)
|__/                   |

julia> collect(Iterators.flatten(()))
ERROR: MethodError: Base.IteratorEltype(::Type{Union{}}) is ambiguous. Candidates:
  Base.IteratorEltype(::Type{#s57} where #s57<:Base.Iterators.Repeated) in Base.Iterators at iterators.jl:725
  Base.IteratorEltype(::Type{#s57} where #s57<:Base.Iterators.PartitionIterator{T}) where T in Base.Iterators at iterators.jl:939
  Base.IteratorEltype(::Type{#s560} where #s560<:Base.Broadcast.Broadcasted) in Base.Broadcast at broadcast.jl:250
  Base.IteratorEltype(::Type{#s57} where #s57<:(Base.Iterators.Rest{I,S} where S)) where I in Base.Iterators at iterators.jl:501
Possible fix, define
  Base.IteratorEltype(::Type{Union{}})
Stacktrace:
 [1] _flatteneltype(::Type, ::Base.HasEltype) at ./iterators.jl:877
 [2] Base.IteratorEltype(::Type{Base.Iterators.Flatten{Tuple{}}}) at ./iterators.jl:876
 [3] Base.IteratorEltype(::Base.Iterators.Flatten{Tuple{}}) at ./generator.jl:123
 [4] collect(::Base.Iterators.Flatten{Tuple{}}) at ./array.jl:557
 [5] top-level scope at none:0

I propose, it should be:

$ julia

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.1.0-DEV.223 (2018-09-10)
 _/ |\__'_|_|_|\__'_|  |  krc/flatten_empty_tuple/046be325bb (fork: 1 commits, 0 days)
|__/                   |

julia> collect(Iterators.flatten(()))
0-element Array{Union{},1}

@@ -873,13 +873,15 @@ julia> collect(Iterators.flatten((1:2, 8:9)))
flatten(itr) = Flatten(itr)

eltype(::Type{Flatten{I}}) where {I} = eltype(eltype(I))
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.

This can go in generator.jl along with the other standard methods for IteratorEltype; it's not specific to flatten.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok - I will move the line to generator.jl

@JeffBezanson
Copy link
Member

Thanks!

@JeffBezanson JeffBezanson merged commit b4c370d into JuliaLang:master Sep 12, 2018
@KlausC KlausC deleted the krc/flatten_empty_tuple branch September 15, 2018 18:56
KristofferC pushed a commit that referenced this pull request Sep 17, 2018
@KristofferC KristofferC mentioned this pull request Sep 17, 2018
@@ -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{}.

JeffBezanson pushed a commit that referenced this pull request Oct 8, 2018
KristofferC pushed a commit that referenced this pull request Oct 9, 2018
(cherry picked from commit 38612e0)
KristofferC pushed a commit that referenced this pull request Oct 10, 2018
(cherry picked from commit 38612e0)
KristofferC pushed a commit that referenced this pull request Feb 11, 2019
KristofferC pushed a commit that referenced this pull request Feb 11, 2019
(cherry picked from commit 38612e0)
KristofferC pushed a commit that referenced this pull request Feb 20, 2020
(cherry picked from commit 38612e0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants