From 3bb78151c5e3e17a267e0a0c726d011101e3c817 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 11 Aug 2016 16:32:08 -0500 Subject: [PATCH] Generalize getindex for AbstractUnitRanges It turns out we were returning a Vector{Int} for operations that should return a Range --- base/range.jl | 5 +++-- test/ranges.jl | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/base/range.jl b/base/range.jl index caca080802553..00b2b217c1f6a 100644 --- a/base/range.jl +++ b/base/range.jl @@ -499,10 +499,11 @@ end getindex(r::Range, ::Colon) = copy(r) -function getindex{T<:Integer}(r::UnitRange, s::AbstractUnitRange{T}) +function getindex{T<:Integer}(r::AbstractUnitRange, s::AbstractUnitRange{T}) @_inline_meta @boundscheck checkbounds(r, s) - st = oftype(r.start, r.start + first(s)-1) + f = first(r) + st = oftype(f, f + first(s)-1) range(st, length(s)) end diff --git a/test/ranges.jl b/test/ranges.jl index c92ebeb385bbf..b22db9b77a1b4 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -755,6 +755,7 @@ r = Base.OneTo(3) @test minimum(r) == 1 @test maximum(r) == 3 @test r[2] == 2 +@test r[2:3] === 2:3 @test_throws BoundsError r[4] @test_throws BoundsError r[0] @test r+1 === 2:4