Skip to content

Commit

Permalink
Tighten validation of hvncat implementation (#43940)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioTurboNick committed Feb 16, 2022
1 parent c839221 commit 76dc379
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76dc379

Please sign in to comment.