diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index 582661dd..c1ccb30d 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -507,6 +507,13 @@ if VERSION < v"1.1.0-DEV.783" Base.copyfirst!(dest::OffsetArray, src::OffsetArray) = (maximum!(parent(dest), parent(src)); return dest) end +# TODO: modify this when https://github.com/JuliaLang/julia/pull/39393 get merged +if VERSION <= v"1.7.0-DEV.375" + # issue 194 + # index for zero-argument getindex should be first linear index instead of 1 + Base._to_linear_index(A::OffsetArray) = first(LinearIndices(A)) +end + ## # Adapt allows for automatic conversion of CPU OffsetArrays to GPU OffsetArrays ## diff --git a/src/axes.jl b/src/axes.jl index ff21f477..d5e168e0 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -179,16 +179,6 @@ Base.show(io::IO, r::IdOffsetRange) = print(io, "OffsetArrays.IdOffsetRange(",fi # Optimizations @inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset) -# issue 194 -# The indexing operation A[] gets mapped to A[1]. -# The bounds-checking for this needs to be handled separately for AbstractVectors -# See https://github.com/JuliaLang/julia/issues/39379 for this issue reported in Base -# Once a PR fixing it is merged, we may limit our fix to earlier Julia versions -@inline function Base.checkbounds_indices(::Type{Bool}, IA::Tuple{IdOffsetRange}, ::Tuple{}) - x = IA[1] - length(x) == 1 && first(x) == one(eltype(x)) -end - if VERSION < v"1.5.2" # issue 100, 133: IdOffsetRange as another index-preserving case shouldn't comtribute offsets # fixed by https://github.com/JuliaLang/julia/pull/37204 diff --git a/test/runtests.jl b/test/runtests.jl index cdce5773..12d99bc4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -712,20 +712,13 @@ end @test A[4] == 2 end - @testset "issue 194" begin - A = OffsetArray([0], 1); - @test Base.checkbounds_indices(Bool, axes(A), ()) == false - @test_throws BoundsError A[] - A = OffsetArray([6], 1:1) - @test A[] == 6 - - A = OffsetArray(reshape(1:4, 2, 2), 2, 2); - @test Base.checkbounds_indices(Bool, axes(A), ()) == false - @test_throws BoundsError A[] - A = OffsetArray(reshape([6], 1, 1), 1:1, 1:1); - @test A[] == 6 - A = OffsetArray(A, 1:1, 2:2); - @test A[] == 6 + @testset "Zero-index indexing (#194)" begin + @test OffsetArray([6], 2:2)[] == 6 + @test OffsetArray(fill(6, 1, 1), 2:2, 3:3)[] == 6 + @test OffsetArray(fill(6))[] == 6 + @test_throws BoundsError OffsetArray([6,7], 2:3)[] + @test_throws BoundsError OffsetArray([6 7], 2:2, 2:3)[] + @test_throws BoundsError OffsetArray([], 2:1)[] end end