diff --git a/base/subarray.jl b/base/subarray.jl index d1207ec094037..7b705be82a31f 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -380,7 +380,7 @@ compute_offset1(parent::AbstractVector, stride1::Integer, I::Tuple{AbstractRange # indexing uses the indices along the given dimension. # If the result is one-dimensional and it's a range, then linear # indexing might be offset if the index itself is offset -# Otherwise linear indexing always starts with 1. +# Otherwise linear indexing always matches the parent. compute_offset1(parent, stride1::Integer, I::Tuple) = (@_inline_meta; compute_offset1(parent, stride1, find_extended_dims(1, I...), find_extended_inds(I...), I)) compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{Slice}, I::Tuple) = @@ -388,11 +388,11 @@ compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{Slice}, compute_offset1(parent, stride1::Integer, dims, inds::Tuple{AbstractRange}, I::Tuple) = (@_inline_meta; compute_linindex(parent, I) - stride1*first(axes1(inds[1]))) # potentially index-offsetting case compute_offset1(parent, stride1::Integer, dims, inds, I::Tuple) = - (@_inline_meta; compute_linindex(parent, I) - stride1) # linear indexing starts with 1 + (@_inline_meta; compute_linindex(parent, I) - stride1) function compute_linindex(parent, I::NTuple{N,Any}) where N @_inline_meta IP = fill_to_length(axes(parent), OneTo(1), Val(N)) - compute_linindex(1, 1, IP, I) + compute_linindex(first(LinearIndices(parent)), 1, IP, I) end function compute_linindex(f, s, IP::Tuple, I::Tuple{ScalarIndex, Vararg{Any}}) @_inline_meta diff --git a/test/offsetarray.jl b/test/offsetarray.jl index bb0b4b13b8da4..75a4133824056 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -110,6 +110,15 @@ let a1 = [11,12,13], a2 = [1 2; 3 4] @test_throws BoundsError i2[1:2:5] end +# issue #37274 +let a = 1:3 + oa = OffsetArray(a, 0:2) + b = @view oa[0] + @test b[] == b[1] == b[1,1] == 1 + @test_throws BoundsError b[0] + @test_throws BoundsError b[2] +end + # logical indexing @test A[A .> 2] == [3,4] @test_throws BoundsError h[trues(2)]