Skip to content

Commit 9c11dcb

Browse files
BioTurboNickKristofferC
authored andcommitted
Tighten validation of hvncat implementation (#43940)
(cherry picked from commit 76dc379)
1 parent a2a7623 commit 9c11dcb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

base/abstractarray.jl

+18
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,24 @@ function _typed_hvncat_dims(::Type{T}, dims::NTuple{N, Int}, row_first::Bool, as
23152315

23162316
outdims = zeros(Int, N)
23172317

2318+
# validate shapes for lowest level of concatenation
2319+
d = findfirst(>(1), dims)
2320+
if d !== nothing # all dims are 1
2321+
nblocks = length(as) ÷ dims[d]
2322+
for b 1:nblocks
2323+
offset = ((b - 1) * dims[d])
2324+
startelementi = offset + 1
2325+
for i offset .+ (2:dims[d])
2326+
for dd 1:N
2327+
dd == d && continue
2328+
if size(as[startelementi], dd) != size(as[i], dd)
2329+
throw(ArgumentError("incompatible shape in element $i"))
2330+
end
2331+
end
2332+
end
2333+
end
2334+
end
2335+
23182336
# discover number of rows or columns
23192337
for i 1:dims[d1]
23202338
outdims[d1] += cat_size(as[i], d1)

test/abstractarray.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,10 @@ using Base: typed_hvncat
15341534
@test Int[] == typed_hvncat(Int, 1) isa Array{Int, 1}
15351535
@test Array{Int, 2}(undef, 0, 0) == typed_hvncat(Int, 2) isa Array{Int, 2}
15361536
@test Array{Int, 3}(undef, 0, 0, 0) == typed_hvncat(Int, 3) isa Array{Int, 3}
1537+
1538+
# Issue 43933 - semicolon precedence mistake should produce an error
1539+
@test_throws ArgumentError [[1 1]; 2 ;; 3 ; [3 4]]
1540+
@test_throws ArgumentError [[1 ;;; 1]; 2 ;;; 3 ; [3 ;;; 4]]
15371541
end
15381542

15391543
@testset "keepat!" begin

0 commit comments

Comments
 (0)