Skip to content

Commit 3c15710

Browse files
nsajkoKristofferC
authored andcommitted
fix zero-dimensional reverse! (#58086)
Also changed the check to compare against the tuple argument, instead of against the method static parameter for the tuple length. Should be better when the length isn't known at compile time. Fixes #58085 (cherry picked from commit b265dba)
1 parent 2d89891 commit 3c15710

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

base/arraymath.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ _reverse!(A::AbstractArray{<:Any,N}, ::Colon) where {N} = _reverse!(A, ntuple(id
7272
_reverse!(A, dim::Integer) = _reverse!(A, (Int(dim),))
7373
_reverse!(A, dims::NTuple{M,Integer}) where {M} = _reverse!(A, Int.(dims))
7474
function _reverse!(A::AbstractArray{<:Any,N}, dims::NTuple{M,Int}) where {N,M}
75+
dims === () && return A # nothing to reverse
7576
dimrev = ntuple(k -> k in dims, Val{N}()) # boolean tuple indicating reversed dims
7677

7778
if N < M || M != sum(dimrev)
7879
throw(ArgumentError("invalid dimensions $dims in reverse!"))
7980
end
80-
M == 0 && return A # nothing to reverse
8181

8282
# swapping loop only needs to traverse ≈half of the array
8383
halfsz = ntuple(k -> k == dims[1] ? size(A,k) ÷ 2 : size(A,k), Val{N}())

test/arrayops.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,12 @@ end
17141714
end
17151715
end
17161716

1717+
@testset "reverse zero dims" begin
1718+
a = fill(3)
1719+
@test a == reverse(a)
1720+
@test a === reverse!(a)
1721+
end
1722+
17171723
@testset "isdiag, istril, istriu" begin
17181724
# Scalar
17191725
@test isdiag(3)

0 commit comments

Comments
 (0)