Skip to content

Commit 296acf2

Browse files
authored
Fix for infinite loop when passing 0d array to setindex of n-dim arrays (#39608)
* Fix for infinite loop with 0d array * Consistency * More consistency * Test
1 parent 2687bbb commit 296acf2

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

base/indices.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ setindex_shape_check(X::AbstractArray) =
239239
setindex_shape_check(X::AbstractArray, i::Integer) =
240240
(length(X)==i || throw_setindex_mismatch(X, (i,)))
241241

242+
setindex_shape_check(X::AbstractArray{<:Any, 0}, i::Integer...) =
243+
(length(X) == prod(i) || throw_setindex_mismatch(X, i))
244+
242245
setindex_shape_check(X::AbstractArray{<:Any,1}, i::Integer) =
243246
(length(X)==i || throw_setindex_mismatch(X, (i,)))
244247

test/arrayops.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,3 +2898,9 @@ end
28982898
@test as isa TSlow{Int,3}
28992899
@test size(as) == (3, 5, 1)
29002900
end
2901+
2902+
@testset "0-dimensional shape checking #39608" begin
2903+
@test [fill(1); [2; 2]] == [1; 2; 2]
2904+
@test [fill(1); fill(2, (2,1,1))] == reshape([1; 2; 2], (3, 1, 1))
2905+
@test_throws DimensionMismatch [fill(1); rand(2, 2, 2)]
2906+
end

0 commit comments

Comments
 (0)