Skip to content

Commit de9832c

Browse files
BioTurboNickKristofferC
authored andcommitted
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 (cherry picked from commit 296acf2)
1 parent 684587d commit de9832c

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
@@ -2866,3 +2866,9 @@ end
28662866
@test only(Base.return_types(f, (Int,))) === Union{Array{Int,0}, Array{Nothing,0}}
28672867
@test only(Base.return_types(f, (UnitRange{Int},))) <: Vector
28682868
end
2869+
2870+
@testset "0-dimensional shape checking #39608" begin
2871+
@test [fill(1); [2; 2]] == [1; 2; 2]
2872+
@test [fill(1); fill(2, (2,1,1))] == reshape([1; 2; 2], (3, 1, 1))
2873+
@test_throws DimensionMismatch [fill(1); rand(2, 2, 2)]
2874+
end

0 commit comments

Comments
 (0)