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

Support reshaping custom 0-dimensional arrays #26870

Merged
merged 2 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,7 @@ else
# fixpoint.
function fixpoint_iter_type(itrT::Type, valT::Type, stateT::Type)
nextvalstate = Base._return_type(next, Tuple{itrT, stateT})
nextvalstate === Union{} && return Any
Copy link
Member Author

Choose a reason for hiding this comment

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

Cc @Keno — I coupled this change here. The failure mode is join((), ","), which attempts to find the value type of next((), start(())) — that's an error and will always be Union{}. I'm assuming that a case like this was intended to be caught by the next line, but of course the bottom type is a subtype of everything so it flies on by and throws an error later. Would it be better to return Any or Union{} in this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

My stab in the dark here didn't work. I've just marked things as @test_broken and opened #26871.

nextvalstate <: Tuple{Any, Any} || return Any
nextvalstate = Tuple{
typejoin(valT, fieldtype(nextvalstate, 1)),
Expand Down
5 changes: 5 additions & 0 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ function __reshape(p::Tuple{AbstractArray,IndexCartesian}, dims::Dims)
ReshapedArray(parent, dims, reverse(mi))
end

function __reshape(p::Tuple{AbstractArray{<:Any,0},IndexCartesian}, dims::Dims)
parent = p[1]
ReshapedArray(parent, dims, ())
end

function __reshape(p::Tuple{AbstractArray,IndexLinear}, dims::Dims)
parent = p[1]
ReshapedArray(parent, dims, ())
Expand Down
16 changes: 16 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ end
end
end

struct Z26163 <: AbstractArray{Int,0}; end
Base.size(::Z26163) = ()
Base.getindex(::Z26163) = 0
struct V26163 <: AbstractArray{Int,1}; end
Base.size(::V26163) = (1,)
Base.getindex(::V26163, ::Int) = 0
@testset "reshape of custom zero- and one-dimensional arrays" begin
z = Z26163()
v = V26163()
@test z == reshape(v, ()) == fill(0, ())
@test reshape(z, 1) == v == [0]
@test reshape(z, 1, 1) == reshape(v, 1, 1) == fill(0, 1, 1)
@test occursin("1-element reshape", summary(reshape(z, 1)))
@test occursin("0-dimensional reshape", summary(reshape(v, ())))
end

@test reshape(1:5, (5,)) === 1:5
@test reshape(1:5, 5) === 1:5

Expand Down
3 changes: 2 additions & 1 deletion test/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
end
end
@testset "join()" begin
@test join([]) == ""
@test join([]) == join(()) == ""
@test join([],",") == join((),",") == ""
@test join(["a"],"?") == "a"
@test join("HELLO",'-') == "H-E-L-L-O"
@test join(1:5, ", ", " and ") == "1, 2, 3, 4 and 5"
Expand Down