From 8b7c13310ee40302fe4a479cf6c338c53e84b2bd Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Thu, 28 Jan 2021 14:51:35 -0500 Subject: [PATCH] =?UTF-8?q?fix=20#39379,=20use=20first=E2=88=98LinearIndic?= =?UTF-8?q?es=20instead=20of=201=20(#39393)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 9de107a63a56f9ca922497991a7eb9dcd027dc06) --- base/abstractarray.jl | 2 +- test/offsetarray.jl | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 383814ff38ddc..a880b4eecfd3f 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1193,7 +1193,7 @@ function _getindex(::IndexLinear, A::AbstractArray, I::Vararg{Int,M}) where M end _to_linear_index(A::AbstractArray, i::Integer) = i _to_linear_index(A::AbstractVector, i::Integer, I::Integer...) = i -_to_linear_index(A::AbstractArray) = 1 +_to_linear_index(A::AbstractArray) = first(LinearIndices(A)) _to_linear_index(A::AbstractArray, I::Integer...) = (@_inline_meta; _sub2ind(A, I...)) ## IndexCartesian Scalar indexing: Canonical method is full dimensionality of Ints diff --git a/test/offsetarray.jl b/test/offsetarray.jl index bb0b4b13b8da4..07f2e12999f90 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -715,6 +715,15 @@ end @test axes(S) == (OffsetArrays.IdOffsetRange(0:1), Base.OneTo(2), OffsetArrays.IdOffsetRange(2:5)) end +@testset "Zero-index indexing" 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 + @testset "IdentityUnitRange indexing" begin a = OffsetVector(3:4, 2:3) ax = IdentityUnitRange(2:3)