diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 22bd61a706057..501af97715c74 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -137,18 +137,19 @@ reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = reshape( _any_colon(post...) && throw1(dims) post::Tuple{Vararg{Int}} len = length(A) - sz, remainder = if iszero(len) - (0, 0) + sz, is_exact = if iszero(len) + (0, true) else let pr = Core.checked_dims(pre..., post...) # safe product if iszero(pr) throw2(A, dims) end - divrem(len, pr) + (quo, rem) = divrem(len, pr) + (Int(quo), iszero(rem)) end - end::NTuple{2,Int} - remainder == 0 || throw2(A, dims) - (pre..., Int(sz), post...)::Tuple{Int,Vararg{Int}} + end::Tuple{Int,Bool} + is_exact || throw2(A, dims) + (pre..., sz, post...)::Tuple{Int,Vararg{Int}} end @inline _any_colon() = false @inline _any_colon(dim::Colon, tail...) = true diff --git a/test/abstractarray.jl b/test/abstractarray.jl index d1bc4735327a7..8eeda15295791 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1860,6 +1860,14 @@ end @test_throws ArgumentError reshape([7], (b, :, b)) @test reshape([], (b, :, b)) isa Array{<:Any, 3} end + for iterator ∈ (7:6, 7:7, 7:8) + for it ∈ (iterator, map(BigInt, iterator)) + @test reshape(it, (:, Int(length(it)))) isa AbstractMatrix + @test reshape(it, (Int(length(it)), :)) isa AbstractMatrix + @test reshape(it, (1, :)) isa AbstractMatrix + @test reshape(it, (:, 1)) isa AbstractMatrix + end + end end @testset "strides for ReshapedArray" begin