Skip to content

Commit 76dc379

Browse files
authored
Tighten validation of hvncat implementation (#43940)
1 parent c839221 commit 76dc379

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

base/abstractarray.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,24 @@ function _typed_hvncat_dims(::Type{T}, dims::NTuple{N, Int}, row_first::Bool, as
23642364

23652365
outdims = zeros(Int, N)
23662366

2367+
# validate shapes for lowest level of concatenation
2368+
d = findfirst(>(1), dims)
2369+
if d !== nothing # all dims are 1
2370+
nblocks = length(as) ÷ dims[d]
2371+
for b 1:nblocks
2372+
offset = ((b - 1) * dims[d])
2373+
startelementi = offset + 1
2374+
for i offset .+ (2:dims[d])
2375+
for dd 1:N
2376+
dd == d && continue
2377+
if size(as[startelementi], dd) != size(as[i], dd)
2378+
throw(ArgumentError("incompatible shape in element $i"))
2379+
end
2380+
end
2381+
end
2382+
end
2383+
end
2384+
23672385
# discover number of rows or columns
23682386
for i 1:dims[d1]
23692387
outdims[d1] += cat_size(as[i], d1)

test/abstractarray.jl

Lines changed: 4 additions & 0 deletions
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)