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

Empty reshapes legalization (issue #45589) #46011

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,14 @@ reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = reshape(
pre = _before_colon(dims...)
post = _after_colon(dims...)
_any_colon(post...) && throw1(dims)
sz, remainder = divrem(length(A), prod(pre)*prod(post))
remainder == 0 || throw2(A, dims)
sz, remainder = if _all_zero(size(A)...) && (_any_zero(pre...) || _any_zero(post...))
(0, 0)
elseif _any_zero(size(A)...) && (_any_zero(pre...) || _any_zero(post...))
divrem(prod(_remove_zeros(size(A)...)), prod(_remove_zeros(pre...))*prod(_remove_zeros(post...)))
else
divrem(length(A), prod(pre)*prod(post))
end
length(A) == 0 || remainder == 0 || throw2(A, dims)
(pre..., Int(sz), post...)
end
@inline _any_colon() = false
Expand All @@ -136,6 +142,16 @@ end
@inline _before_colon(dim::Colon, tail...) = ()
@inline _after_colon(dim::Any, tail...) = _after_colon(tail...)
@inline _after_colon(dim::Colon, tail...) = tail
@inline _any_zero() = false
@inline _any_zero(head::Any, tail...) = iszero(head) || _any_zero(tail...)
@inline _all_zero() = false
@inline _all_zero(head::Any, tail...) = iszero(head) && _any_zero(tail...)
@inline _remove_zeros(dim::Any, tail...) = if iszero(dim)
_remove_zeros(tail...)
else
(dim, _remove_zeros(tail...)...)
end
@inline _remove_zeros() = ()

reshape(parent::AbstractArray{T,N}, ndims::Val{N}) where {T,N} = parent
function reshape(parent::AbstractArray, ndims::Val{N}) where N
Expand Down
25 changes: 25 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1649,3 +1649,28 @@ end
@test (@inferred A[i,i,i]) === A[1]
@test (@inferred to_indices([], (1, CIdx(1, 1), 1, CIdx(1, 1), 1, CIdx(1, 1), 1))) == ntuple(Returns(1), 10)
end

@testset "reshape inference for empty arrays does not fail, and attempts to preserve size of non-empty dimensions (issue #45589)" begin
@test size(reshape(zeros(0,2), :)) == (0,)
@test size(reshape(zeros(0,2), 0,:)) == (0,2)
@test size(reshape(zeros(0,2), 1,:)) == (1,0)
@test size(reshape(zeros(0,2), 2,:)) == (2,0)
@test size(reshape(zeros(0,2), 3,:)) == (3,0)
@test size(reshape(zeros(0,0,0), 0,:,0)) == (0,0,0)
@test size(reshape(zeros(0,0,0), 0,:,1)) == (0,0,1)
@test size(reshape(zeros(0,0,0), 0,:,1111)) == (0,0,1111)
@test size(reshape(zeros(0,2,3), 0,:)) == (0,6)
@test size(reshape(zeros(0,2,3), 5,:)) == (5,0)
@test size(reshape(zeros(0,2,3), 6,:)) == (6,0)
@test size(reshape(zeros(0,2,3), 7,:)) == (7,0)
@test size(reshape(zeros(0,2,3), 0,:,2)) == (0,3,2)
@test size(reshape(zeros(0,2,3), 0,:,3)) == (0,2,3)
@test size(reshape(zeros(0,2,3), 0,:,5)) == (0,1,5)
@test size(reshape(zeros(0,2,3), 0,:,6)) == (0,1,6)
@test size(reshape(zeros(0,2,3), 0,:,7)) == (0,0,7)
@test size(reshape(zeros(0,2,3), 1,:,3)) == (1,0,3)
@test size(reshape(zeros(0,2,3), 1,:,5)) == (1,0,5)
@test size(reshape(zeros(0,2,3), 1,:,6)) == (1,0,6)
@test size(reshape(zeros(0,2,3), 1,:,7)) == (1,0,7)
@test size(reshape(zeros(0,2,3), 3,:,2)) == (3,0,2)
end