Skip to content

Commit 40d3bdd

Browse files
committed
Cover all implementations of getindex(::AbstractRange,...)
1 parent ac1cc2c commit 40d3bdd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

base/subarray.jl

+9-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function view(A::AbstractArray, I::Vararg{Any,N}) where {N}
164164
unsafe_view(_maybe_reshape_parent(A, index_ndims(J...)), J...)
165165
end
166166

167-
# Ranges tend to be compact & immutable
167+
# Ranges implement getindex to return recomputed ranges; use that for views, too (when possible)
168168
function view(r1::OneTo, r2::OneTo)
169169
@_propagate_inbounds_meta
170170
getindex(r1, r2)
@@ -181,6 +181,14 @@ function view(r1::StepRange, r2::AbstractRange{<:Integer})
181181
@_propagate_inbounds_meta
182182
getindex(r1, r2)
183183
end
184+
function view(r1::StepRangeLen, r2::OrdinalRange{<:Integer})
185+
@_propagate_inbounds_meta
186+
getindex(r1, r2)
187+
end
188+
function view(r1::LinRange, r2::OrdinalRange{<:Integer})
189+
@_propagate_inbounds_meta
190+
getindex(r1, r2)
191+
end
184192

185193
function unsafe_view(A::AbstractArray, I::Vararg{ViewIndex,N}) where {N}
186194
@_inline_meta

test/ranges.jl

+9
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,15 @@ end
13681368
@test view(1:10, 1:5) === 1:5
13691369
@test view(1:10, 1:2:5) === 1:2:5
13701370
@test view(1:2:9, 1:5) === 1:2:9
1371+
1372+
# Ensure we don't hit a fallback `view` if there's a better `getindex` implementation
1373+
vmt = collect(methods(view, Tuple{AbstractRange, AbstractRange}))
1374+
for m in methods(getindex, Tuple{AbstractRange, AbstractRange})
1375+
tt = Base.tuple_type_tail(m.sig)
1376+
tt == Tuple{AbstractArray,Vararg{Any,N}} where N && continue
1377+
vm = findfirst(sig->tt <: Base.tuple_type_tail(sig.sig), vmt)
1378+
@test vmt[vm].sig != Tuple{typeof(view),AbstractArray,Vararg{Any,N}} where N
1379+
end
13711380
end
13721381

13731382
@testset "Issue #26608" begin

0 commit comments

Comments
 (0)