From d5392acb5717635ee02f73523a4c20904d6a541f Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Mon, 9 Jul 2018 04:19:55 -0500 Subject: [PATCH] Fix offset LinearIndices range indexing (fixes #27986) --- base/indices.jl | 2 +- test/offsetarray.jl | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/base/indices.jl b/base/indices.jl index b5cb2b782fbe1..8712c5bc7db20 100644 --- a/base/indices.jl +++ b/base/indices.jl @@ -371,7 +371,7 @@ end function getindex(iter::LinearIndices, i::AbstractRange{<:Integer}) @_inline_meta @boundscheck checkbounds(iter, i) - @inbounds (first(iter):last(iter))[i] + @inbounds isa(iter, LinearIndices{1}) ? iter.indices[1][i] : (first(iter):last(iter))[i] end # More efficient iteration — predominantly for non-vector LinearIndices # but one-dimensional LinearIndices must be special-cased to support OffsetArrays diff --git a/test/offsetarray.jl b/test/offsetarray.jl index fc88ec7eb49b1..ce90aaaba9279 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -83,6 +83,29 @@ for i = 1:9 @test A_3_3[i] == i end @test eachindex(A) == 1:4 @test eachindex(S) == CartesianIndices(axes(S)) == CartesianIndices(map(Base.Slice, (0:1,3:4))) +# LinearIndices +# issue 27986 +let a1 = [11,12,13], a2 = [1 2; 3 4] + b1 = OffsetArray(a1, (-3,)) + i1 = LinearIndices(b1) + @test i1[-2] == -2 + @test_throws BoundsError i1[-3] + @test_throws BoundsError i1[1] + @test i1[-2:end] === -2:0 + @test @inferred(i1[-2:0]) === -2:0 + @test_throws BoundsError i1[-3:end] + @test_throws BoundsError i1[-2:1] + b2 = OffsetArray(a2, (-3,5)) + i2 = LinearIndices(b2) + @test i2[3] == 3 + @test_throws BoundsError i2[0] + @test_throws BoundsError i2[5] + @test @inferred(i2[2:3]) === 2:3 + @test @inferred(i2[1:2:4]) === 1:2:3 + @test_throws BoundsError i2[1:5] + @test_throws BoundsError i2[1:2:5] +end + # logical indexing @test A[A .> 2] == [3,4] @test_throws BoundsError h[trues(2)]