From a4402010c214d9000eacab91fdfc669926560922 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Sat, 7 Dec 2024 15:32:11 -0500 Subject: [PATCH] fix 56771 --- base/abstractarray.jl | 2 -- test/abstractarray.jl | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 6102a0a8a00fa..1954b0944147a 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -2960,8 +2960,6 @@ _iterator_axes(x, ::IteratorSize) = axes(x) # For some dims values, stack(A; dims) == stack(vec(A)), and the : path will be faster _typed_stack(dims::Integer, ::Type{T}, ::Type{S}, A) where {T,S} = _typed_stack(dims, T, S, IteratorSize(S), A) -_typed_stack(dims::Integer, ::Type{T}, ::Type{S}, ::HasLength, A) where {T,S} = - _typed_stack(dims, T, S, HasShape{1}(), A) function _typed_stack(dims::Integer, ::Type{T}, ::Type{S}, ::HasShape{N}, A) where {T,S,N} if dims == N+1 _typed_stack(:, T, S, A, (_vec_axis(A),)) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 4af4099eced45..c1f7f36c60726 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1854,6 +1854,33 @@ end end end +@testset "issue 56771, stack(; dims) on containers with HasLength eltype & HasShape elements" begin + for T in (Matrix, Array, Any) + xs = T[rand(2,3) for _ in 1:4] + @test size(stack(xs; dims=1)) == (4,2,3) + @test size(stack(xs; dims=2)) == (2,4,3) # this was the problem case, for T=Array + @test size(stack(xs; dims=3)) == (2,3,4) + @test size(stack(identity, xs; dims=2)) == (2,4,3) + @test size(stack(x for x in xs if true; dims=2)) == (2,4,3) + + xmat = T[rand(2,3) for _ in 1:4, _ in 1:5] + @test size(stack(xmat; dims=1)) == (20,2,3) + @test size(stack(xmat; dims=2)) == (2,20,3) + @test size(stack(xmat; dims=3)) == (2,3,20) + end + + it = Iterators.product(1:2, 3:5) + @test size(it) == (2,3) + @test Base.IteratorSize(typeof(it)) == Base.HasShape{2}() + @test Base.IteratorSize(Iterators.ProductIterator) == Base.HasLength() + for T in (typeof(it), Iterators.ProductIterator, Any) + ys = T[it for _ in 1:4] + @test size(stack(ys; dims=2)) == (2,4,3) + @test size(stack(identity, ys; dims=2)) == (2,4,3) + @test size(stack(y for y in ys if true; dims=2)) == (2,4,3) + end +end + @testset "keepat!" begin a = [1:6;] @test a === keepat!(a, 1:5)