Skip to content

Commit a67e65a

Browse files
committed
Disable partial linear indexing for non-1 arrays, but allow partial indexing
1 parent 381fbec commit a67e65a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

base/abstractarray.jl

+15-2
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ function checkbounds_indices(::Type{Bool}, IA::Tuple, I::Tuple{Any})
365365
@_inline_meta
366366
checkbounds_linear_indices(Bool, IA, I[1])
367367
end
368-
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple, i)
368+
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple{Vararg{OneTo}}, i)
369369
@_inline_meta
370370
if checkindex(Bool, IA[1], i)
371371
return true
@@ -375,6 +375,10 @@ function checkbounds_linear_indices(::Type{Bool}, IA::Tuple, i)
375375
end
376376
return false
377377
end
378+
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}, i)
379+
@_inline_meta
380+
checkindex(Bool, IA[1], i)
381+
end
378382
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple, i::Union{Slice,Colon})
379383
partial_linear_indexing_warning_lookup(length(IA))
380384
true
@@ -856,11 +860,20 @@ function _to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Int...) # TODO: DE
856860
J, Jrem = IteratorsMD.split(I, Val{N})
857861
_to_subscript_indices(A, J, Jrem)
858862
end
859-
function _to_subscript_indices(A::AbstractArray, J::Tuple, Jrem::Tuple{})
863+
_to_subscript_indices(A::AbstractArray, J::Tuple, Jrem::Tuple{}) =
864+
__to_subscript_indices(A, indices(A), J, Jrem)
865+
# We allow partial linear indexing deprecation for OneTo arrays
866+
function __to_subscript_indices(A::AbstractArray, ::Tuple{Vararg{OneTo}}, J::Tuple, Jrem::Tuple{})
860867
@_inline_meta
861868
sz = _remaining_size(J, indices(A)) # compute trailing size (overlapping the final index)
862869
(front(J)..., _unsafe_ind2sub(sz, last(J))...) # (maybe) extend the last index
863870
end
871+
# After the partial linear indexing deprecation is removed, this next method can
872+
# become the new normal. For now, it's limited to non-OneTo arrays.
873+
function __to_subscript_indices(A::AbstractArray, ::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}, J::Tuple, Jrem::Tuple{})
874+
@_inline_meta
875+
(J..., map(first, tail(_remaining_size(J, indices(A))))...)
876+
end
864877
_to_subscript_indices(A, J::Tuple, Jrem::Tuple) = J # already bounds-checked, safe to drop
865878
_to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Vararg{Int,N}) = I
866879
_remaining_size(::Tuple{Any}, t::Tuple) = t

test/offsetarray.jl

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # LinearSlow
3030
@test_throws BoundsError S[1,1]
3131
@test_throws BoundsError A[0,3,2]
3232
@test_throws BoundsError S[0,3,2]
33+
# partial indexing
34+
S3 = OffsetArray(view(reshape(collect(1:4*3*2), 4, 3, 2), 1:3, 1:2, :), (-1,-2,1))
35+
@test S3[1,-1] == 2
36+
@test S3[1,0] == 6
37+
@test_throws BoundsError S3[1,1]
38+
@test_throws BoundsError S3[1,-2]
3339

3440
# Vector indexing
3541
@test A[:, 3] == S[:, 3] == OffsetArray([1,2], (A.offsets[1],))

0 commit comments

Comments
 (0)