From dc3cbef16747bb5dcbf960e8992dd6629d0cf952 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Wed, 16 Feb 2022 06:41:54 -0500 Subject: [PATCH] Tighten validation of `hvncat` implementation (#43940) --- base/abstractarray.jl | 18 ++++++++++++++++++ test/abstractarray.jl | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 8bba670b964731..0f900aeef5f45a 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -2364,6 +2364,24 @@ function _typed_hvncat_dims(::Type{T}, dims::NTuple{N, Int}, row_first::Bool, as outdims = zeros(Int, N) + # validate shapes for lowest level of concatenation + d = findfirst(>(1), dims) + if d !== nothing # all dims are 1 + nblocks = length(as) ÷ dims[d] + for b ∈ 1:nblocks + offset = ((b - 1) * dims[d]) + startelementi = offset + 1 + for i ∈ offset .+ (2:dims[d]) + for dd ∈ 1:N + dd == d && continue + if size(as[startelementi], dd) != size(as[i], dd) + throw(ArgumentError("incompatible shape in element $i")) + end + end + end + end + end + # discover number of rows or columns for i ∈ 1:dims[d1] outdims[d1] += cat_size(as[i], d1) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index a33cf53698d1cd..84d69200368e8e 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1534,6 +1534,10 @@ using Base: typed_hvncat @test Int[] == typed_hvncat(Int, 1) isa Array{Int, 1} @test Array{Int, 2}(undef, 0, 0) == typed_hvncat(Int, 2) isa Array{Int, 2} @test Array{Int, 3}(undef, 0, 0, 0) == typed_hvncat(Int, 3) isa Array{Int, 3} + + # Issue 43933 - semicolon precedence mistake should produce an error + @test_throws ArgumentError [[1 1]; 2 ;; 3 ; [3 4]] + @test_throws ArgumentError [[1 ;;; 1]; 2 ;;; 3 ; [3 ;;; 4]] end @testset "keepat!" begin