-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Allow Iterators.flatten
for empty tuple
#29112
Conversation
base/iterators.jl
Outdated
@@ -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() |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
Thanks! |
(cherry picked from commit b4c370d)
@@ -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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Line 123 in 1968b23
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 1 ─ return $(QuoteNode(Base.HasEltype()))
This causes the regression noted in #29464.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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{}
.
(cherry picked from commit b4c370d)
What happened:
I propose, it should be: