diff --git a/src/indexing.jl b/src/indexing.jl index 4cbb45eb..95d85506 100644 --- a/src/indexing.jl +++ b/src/indexing.jl @@ -369,4 +369,13 @@ Base.checkindex(B::Type{Bool}, inds::AbstractUnitRange, i::StaticIndexing{T}) wh # unsafe_view -Base.unsafe_view(A::AbstractArray, i::StaticIndexing{T}) where T = Base.unsafe_view(A, unwrap(i)) +# unsafe_view need only deal with vargs of `StaticIndexing`, as wrapped by to_indices. +# i1 is explicitly specified to avoid ambiguities with Base +Base.unsafe_view(A::AbstractArray, i1::StaticIndexing, indices::StaticIndexing...) = Base.unsafe_view(A, unwrap(i1), map(unwrap, indices)...) + +# Views of views need a new method for Base.SubArray because storing indices +# wrapped in StaticIndexing in field indices of SubArray causes all sorts of problems. +# Additionally, in some cases the SubArray constructor may be called directly +# instead of unsafe_view so we need this method too (Base._maybe_reindex +# is a good example) +Base.SubArray(A::AbstractArray, indices::NTuple{<:Any,StaticIndexing}) = Base.SubArray(A, map(unwrap, indices)) diff --git a/test/indexing.jl b/test/indexing.jl index 39d82806..481324cb 100644 --- a/test/indexing.jl +++ b/test/indexing.jl @@ -209,5 +209,18 @@ using StaticArrays, Test a = collect(11:20) @test view(a, SVector(1,2,3)) == [11,12,13] @test_throws BoundsError view(a, SVector(1,11,3)) + B = rand(Int,3,4,5,6) + Bv = view(B, 1, (@SVector [2, 1]), [2, 3], (@SVector [4])) + @test Bv == B[1, [2,1], 2:3, [4]] + @test axes(Bv, 1) === SOneTo(2) + @test axes(Bv, 3) === SOneTo(1) + Bvv = view(Bv, (@SVector [1, 2]), 2, 1) + @test axes(Bvv) === (SOneTo(2),) + @test Bvv[1] == B[1, 2, 3, 4] + Bvv[1] = 100 + @test Bvv[1] == 100 + @test B[1,2,3,4] == 100 + @test eltype(Bvv) == Int + @test Bvv[:] == [B[1,2,3,4], B[1,1,3,4]] end end