diff --git a/base/array.jl b/base/array.jl index b1e67a91617f1..6e5b9b148a043 100644 --- a/base/array.jl +++ b/base/array.jl @@ -838,9 +838,9 @@ function getindex(A::Array, I::AbstractUnitRange{<:Integer}) @_inline_meta @boundscheck checkbounds(A, I) lI = length(I) - X = similar(A, lI) + X = similar(A, axes(I)) if lI > 0 - unsafe_copyto!(X, 1, A, first(I), lI) + copyto!(X, firstindex(X), A, first(I), lI) end return X end diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 50fe2fcef4c1f..96a44c71e5483 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -776,3 +776,13 @@ end strY = String(take!(io)) @test strX == strY end + +@testset "vector indexing (issue #39896)" begin + a = collect(1:10) + r = Base.IdentityUnitRange(2:3) + b = a[r] + @test axes(b) == axes(r) + for i in r + @test b[i] == a[r[i]] + end +end