From 873cc304df06301354ba6421dd219a885b62b835 Mon Sep 17 00:00:00 2001 From: jishnub Date: Sun, 24 Jan 2021 09:42:39 +0400 Subject: [PATCH] Fix for issue 194 --- Project.toml | 2 +- src/axes.jl | 10 ++++++++++ test/runtests.jl | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6b287db0..ce912476 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "OffsetArrays" uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" -version = "1.5.1" +version = "1.5.2" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/axes.jl b/src/axes.jl index d5e168e0..ff21f477 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -179,6 +179,16 @@ 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 91571549..cdce5773 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -711,6 +711,22 @@ end @test setindex!(A, 2, 4) === A @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 + end end @testset "Vector indexing" begin