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

WIP: Preserve concrete Union types in collect() with generators #23940

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 14 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,14 @@ end
_array_for(::Type{T}, itr, ::HasLength) where {T} = Array{T,1}(Int(length(itr)::Integer))
_array_for(::Type{T}, itr, ::HasShape) where {T} = similar(Array{T}, indices(itr))

function _isconcreteunion(::Type{T}) where T
if T isa Union
_isconcreteunion(T.a) && _isconcreteunion(T.b)
else
isconcrete(T)
end
end

Copy link
Member

Choose a reason for hiding this comment

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

We already have Base.isbitsunion for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

This one is slightly different, it also includes things like Union{String, Void}.

function collect(itr::Generator)
isz = iteratorsize(itr.iter)
et = _default_eltype(typeof(itr))
Expand All @@ -675,7 +683,9 @@ function collect(itr::Generator)
return _array_for(et, itr.iter, isz)
end
v1, st = next(itr, st)
collect_to_with_first!(_array_for(typeof(v1), itr.iter, isz), v1, itr, st)
S = _default_eltype(typeof(itr))
T = _isconcreteunion(S) ? S : typeof(v1)
collect_to_with_first!(_array_for(T, itr.iter, isz), v1, itr, st)
end
end

Expand All @@ -688,7 +698,9 @@ function _collect(c, itr, ::EltypeUnknown, isz::Union{HasLength,HasShape})
return _similar_for(c, _default_eltype(typeof(itr)), itr, isz)
end
v1, st = next(itr, st)
collect_to_with_first!(_similar_for(c, typeof(v1), itr, isz), v1, itr, st)
S = _default_eltype(typeof(itr))
T = _isconcreteunion(S) ? S : typeof(v1)
collect_to_with_first!(_similar_for(c, T, itr, isz), v1, itr, st)
end

function collect_to_with_first!(dest::AbstractArray, v1, itr, st)
Expand Down
4 changes: 4 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,10 @@ end
@test isequal([1,2,3], [a for (a,b) in enumerate(2:4)])
@test isequal([2,3,4], [b for (a,b) in enumerate(2:4)])

@test [s for s in Union{String, Void}["a", nothing]] isa Vector{Union{String, Void}}
@test [s for s in Union{String, Void}["a"]] isa Vector{Union{String, Void}}
@test [s for s in Vector{Union{String, Void}}()] isa Vector{Union{String, Void}}

@testset "comprehension in let-bound function" begin
let x⊙y = sum([x[i]*y[i] for i=1:length(x)])
@test [1,2] ⊙ [3,4] == 11
Expand Down
42 changes: 42 additions & 0 deletions test/functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,45 @@ end
for n = 0:5:100-q-d
for p = 100-q-d-n
if p < n < d < q] == [(50,30,15,5), (50,30,20,0), (50,40,10,0), (75,20,5,0)]

@testset "return type of map() and collect() on generators" begin
x = ["a", "b"]
res = @inferred collect(s for s in x)
@test res isa Vector{String}
res = @inferred map(identity, x)
@test res isa Vector{String}
res = @inferred collect(s === nothing for s in x)
@test res isa Vector{Bool}
res = @inferred map(s -> s === nothing, x)
@test res isa Vector{Bool}

y = Union{String, Void}["a", nothing]
f(::Void) = nothing
f(s::String) = s == "a"
res = @inferred collect(s for s in y)
@test res isa Vector{Union{String, Void}}
res = @inferred map(identity, y)
@test res isa Vector{Union{String, Void}}
res = @inferred collect(s === nothing for s in y)
@test res isa Vector{Bool}
res = @inferred map(s -> s === nothing, y)
@test res isa Vector{Bool}
res = @inferred collect(f(s) for s in y)
@test res isa Vector{Union{Bool, Void}}
res = @inferred map(f, y)
@test res isa Vector{Union{Bool, Void}}

y[2] = "c"
res = @inferred collect(s for s in y)
@test res isa Vector{Union{String, Void}}
res = @inferred map(identity, y)
@test res isa Vector{Union{String, Void}}
res = @inferred collect(s === nothing for s in y)
@test res isa Vector{Bool}
res = @inferred map(s -> s === nothing, y)
@test res isa Vector{Bool}
res = @inferred collect(f(s) for s in y)
@test res isa Vector{Union{Bool, Void}}
res = @inferred map(f, y)
@test res isa Vector{Union{Bool, Void}}
end