Skip to content

Commit 4d0a469

Browse files
authored
SubArray: avoid invalid elimination of singleton indices (JuliaLang#53228)
close JuliaLang#53209
1 parent 2bd4cf8 commit 4d0a469

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

base/subarray.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ _maybe_reshape_parent(A::AbstractArray, ::NTuple{1, Bool}) = reshape(A, Val(1))
154154
_maybe_reshape_parent(A::AbstractArray{<:Any,1}, ::NTuple{1, Bool}) = reshape(A, Val(1))
155155
_maybe_reshape_parent(A::AbstractArray{<:Any,N}, ::NTuple{N, Bool}) where {N} = A
156156
_maybe_reshape_parent(A::AbstractArray, ::NTuple{N, Bool}) where {N} = reshape(A, Val(N))
157+
# The trailing singleton indices could be eliminated after bounds checking.
158+
rm_singleton_indices(ndims::Tuple, J1, Js...) = (J1, rm_singleton_indices(IteratorsMD._splitrest(ndims, index_ndims(J1)), Js...)...)
159+
rm_singleton_indices(::Tuple{}, ::ScalarIndex, Js...) = rm_singleton_indices((), Js...)
160+
rm_singleton_indices(::Tuple) = ()
161+
157162
"""
158163
view(A, inds...)
159164
@@ -200,15 +205,12 @@ julia> view(2:5, 2:3) # returns a range as type is immutable
200205
3:4
201206
```
202207
"""
203-
function view(A::AbstractArray{<:Any,N}, I::Vararg{Any,M}) where {N,M}
208+
function view(A::AbstractArray, I::Vararg{Any,M}) where {M}
204209
@inline
205210
J = map(i->unalias(A,i), to_indices(A, I))
206211
@boundscheck checkbounds(A, J...)
207-
if length(J) > ndims(A) && J[N+1:end] isa Tuple{Vararg{Int}}
208-
# view([1,2,3], :, 1) does not need to reshape
209-
return unsafe_view(A, J[1:N]...)
210-
end
211-
unsafe_view(_maybe_reshape_parent(A, index_ndims(J...)), J...)
212+
J′ = rm_singleton_indices(ntuple(Returns(true), Val(ndims(A))), J...)
213+
unsafe_view(_maybe_reshape_parent(A, index_ndims(J′...)), J′...)
212214
end
213215

214216
# Ranges implement getindex to return recomputed ranges; use that for views, too (when possible)

test/subarray.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,9 +923,9 @@ end
923923

924924
@testset "issue #41221: view(::Vector, :, 1)" begin
925925
v = randn(3)
926-
@test view(v,:,1) == v
927-
@test parent(view(v,:,1)) === v
928-
@test parent(view(v,2:3,1,1)) === v
926+
@test @inferred(view(v,:,1)) == v
927+
@test parent(@inferred(view(v,:,1))) === v
928+
@test parent(@inferred(view(v,2:3,1,1))) === v
929929
@test_throws BoundsError view(v,:,2)
930930
@test_throws BoundsError view(v,:,1,2)
931931

@@ -934,6 +934,13 @@ end
934934
@test parent(view(m, 1:2, 3, 1, 1)) === m
935935
end
936936

937+
@testset "issue #53209: avoid invalid elimination of singleton indices" begin
938+
A = randn(4,5)
939+
@test A[CartesianIndices(()), :, 3] == @inferred(view(A, CartesianIndices(()), :, 3))
940+
@test parent(@inferred(view(A, :, 3, 1, CartesianIndices(()), 1))) === A
941+
@test_throws BoundsError view(A, :, 3, 2, CartesianIndices(()), 1)
942+
end
943+
937944
@testset "replace_in_print_matrix" begin
938945
struct MyIdentity <: AbstractMatrix{Bool}
939946
n :: Int

0 commit comments

Comments
 (0)