@@ -87,12 +87,20 @@ Base.@propagate_inbounds _unsafe_view_impl(IFwd::NTuple{M,Base.ViewIndex}, A::Un
8787 _unsafe_view_impl ((IFwd... , i), A, I... )
8888
8989@inline function _unsafe_view_impl (IFwd:: NTuple{M,Base.Slice} , A:: UnsafeArray{T,N} , i:: DenseIdx , I:: Integer... ) where {T,M,N}
90+ ax_A = axes (A)
9091 @assert IFwd == ntuple (i -> axes (A)[i], Val {M} ())
9192 I_all = (IFwd... , i, I... )
9293 @boundscheck checkbounds (A, I_all... )
9394 startidxs = map (first, (IFwd... , i, I... ))
95+ firstidxs = map (first, ax_A)
9496 sub_s = _sub_size (I_all... )
95- p = pointer (A, LinearIndices (size (A))[startidxs... ])
97+ A_isempty = length (A) == 0
98+ view_isempty = prod (sub_s) == 0
99+ # getindex on LinearIndices fails with startidxs if A is empty, so use pseudo-size in that case:
100+ sz = size (A)
101+ pseudo_sz = ntuple (_ -> 1 , Val (N))
102+ mod_size, mod_startidxs = ifelse (A_isempty && view_isempty, (pseudo_sz, firstidxs), (sz, startidxs))
103+ p = pointer (A, LinearIndices (mod_size)[mod_startidxs... ])
96104 UnsafeArray (p, sub_s)
97105end
98106
0 commit comments