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

some generally-applicable changes from #16622 #17214

Merged
merged 6 commits into from
Jul 1, 2016
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
24 changes: 17 additions & 7 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,28 @@ promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type

## copying iterators to containers

# make a collection similar to `c` and appropriate for collecting `itr`
_similar_for(c::AbstractArray, T, itr, ::SizeUnknown) = similar(c, T, 0)
_similar_for(c::AbstractArray, T, itr, ::HasLength) = similar(c, T, Int(length(itr)::Integer))
_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, convert(Dims,size(itr)))
_similar_for(c, T, itr, isz) = similar(c, T)

"""
collect(element_type, collection)

Return an array of type `Array{element_type,1}` of all items in a collection.
"""
collect{T}(::Type{T}, itr) = collect(Generator(T, itr))
collect{T}(::Type{T}, itr) = _collect(T, itr, iteratorsize(itr))

_collect{T}(::Type{T}, itr, isz::HasLength) = copy!(Array{T,1}(Int(length(itr)::Integer)), itr)
_collect{T}(::Type{T}, itr, isz::HasShape) = copy!(Array{T}(convert(Dims,size(itr))), itr)
function _collect{T}(::Type{T}, itr, isz::SizeUnknown)
a = Array{T,1}(0)
for x in itr
push!(a,x)
end
return a
end

# make a collection similar to `c` and appropriate for collecting `itr`
_similar_for(c::AbstractArray, T, itr, ::SizeUnknown) = similar(c, T, 0)
_similar_for(c::AbstractArray, T, itr, ::HasLength) = similar(c, T, Int(length(itr)::Integer))
_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, convert(Dims,size(itr)))
_similar_for(c, T, itr, isz) = similar(c, T)

"""
collect(collection)
Expand Down
2 changes: 1 addition & 1 deletion base/generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ immutable Generator{I,F}
iter::I
end

Generator(f, c1, c...) = Generator(a->f(a...), zip(c1, c...))
Generator(f, I1, I2, Is...) = Generator(a->f(a...), zip(I1, I2, Is...))

Generator{T,I}(::Type{T}, iter::I) = Generator{I,Type{T}}(T, iter)

Expand Down
7 changes: 6 additions & 1 deletion base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,12 @@ function tmerge(typea::ANY, typeb::ANY)
if isa(typea, DataType) && isa(typeb, DataType) && length(typea.parameters) == length(typeb.parameters) && !isvatuple(typea) && !isvatuple(typeb)
return typejoin(typea, typeb)
end
return Tuple
if isa(typea, Union) || isa(typeb, Union) || (isa(typea,DataType) && length(typea.parameters)>3) ||
Copy link
Member

Choose a reason for hiding this comment

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

did something change such that the fix for #6704 isn't useful anymore? Tuples must widen really fast or inference can get stuck trying to infer some really bad cases. (IIUC, you are finding that the lack of a recursive version of this test causes #14743 (comment))

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure. We need to do something here, so that it's possible to infer the result of (tup::Union{Tuple{Bool}, Tuple{Bool,Int}})[1]. Maybe allow a max of only 2 tuple types in a union?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok I found a good more conservative version of this that doesn't affect performance on #6704.

(isa(typeb,DataType) && length(typeb.parameters)>3)
# widen tuples faster (see #6704), but not too much, to make sure we can infer
# e.g. (t::Union{Tuple{Bool},Tuple{Bool,Int}})[1]
return Tuple
end
end
u = Union{typea, typeb}
if length(u.types) > MAX_TYPEUNION_LEN || type_too_complex(u, 0)
Expand Down
8 changes: 5 additions & 3 deletions base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function advance_filter(pred, itr, st)
end
s=t
end
v, (true,)
v, (true, v, s)
end

done(f::Filter, s) = s[1]
Expand Down Expand Up @@ -479,7 +479,9 @@ flatten(itr) = Flatten(itr)

eltype{I}(::Type{Flatten{I}}) = eltype(eltype(I))
iteratorsize{I}(::Type{Flatten{I}}) = SizeUnknown()
iteratoreltype{I}(::Type{Flatten{I}}) = iteratoreltype(eltype(I))
iteratoreltype{I}(::Type{Flatten{I}}) = _flatteneltype(I, iteratoreltype(I))
_flatteneltype(I, ::HasEltype) = iteratoreltype(eltype(I))
_flatteneltype(I, et) = EltypeUnknown()

function start(f::Flatten)
local inner, s2
Expand All @@ -496,7 +498,7 @@ function start(f::Flatten)
return s, inner, s2
end

function next(f::Flatten, state)
@inline function next(f::Flatten, state)
s, inner, s2 = state
val, s2 = next(inner, s2)
while done(inner, s2) && !done(f.it, s)
Expand Down
8 changes: 5 additions & 3 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2380,10 +2380,12 @@ static jl_value_t *inst_tuple_w_(jl_value_t *t, jl_value_t **env, size_t n,
int i;
for(i=0; i < ntp; i++) {
jl_value_t *elt = jl_svecref(tp, i);
iparams[i] = (jl_value_t*)inst_type_w_(elt, env, n, stack, 0);
jl_value_t *pi = (jl_value_t*)inst_type_w_(elt, env, n, stack, 0);
if (jl_is_typevar(pi) && !((jl_tvar_t*)pi)->bound)
pi = ((jl_tvar_t*)pi)->ub;
iparams[i] = pi;
if (ip_heap)
jl_gc_wb(ip_heap, iparams[i]);
jl_value_t *pi = iparams[i];
jl_gc_wb(ip_heap, pi);
check_tuple_parameter(pi, i, ntp);
if (cacheable && !jl_is_leaf_type(pi)) {
cacheable = 0;
Expand Down
4 changes: 4 additions & 0 deletions test/functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ end
# typed `collect`
@test collect(Float64, Filter(isodd, [1,2,3,4]))[1] === 1.0

@test isa(collect(Any, [1,2]), Vector{Any})

# enumerate (issue #6284)
let b = IOBuffer("1\n2\n3\n"), a = []
for (i,x) in enumerate(eachline(b))
Expand Down Expand Up @@ -374,6 +376,8 @@ import Base.flatten
@test eltype(flatten(UnitRange{Int8}[1:2, 3:4])) == Int8
@test_throws ArgumentError collect(flatten(Any[]))

@test Base.iteratoreltype(Base.Flatten((i for i=1:2) for j=1:1)) == Base.EltypeUnknown()

# foreach
let
a = []
Expand Down