Skip to content

Commit dd78704

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 7182837 commit dd78704

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
@@ -1685,6 +1685,12 @@ end
16851685
end
16861686
end
16871687

1688+
@testset "reverse zero dims" begin
1689+
a = fill(3)
1690+
@test a == reverse(a)
1691+
@test a === reverse!(a)
1692+
end
1693+
16881694
@testset "isdiag, istril, istriu" begin
16891695
# Scalar
16901696
@test isdiag(3)

0 commit comments

Comments
 (0)