Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1377,8 +1377,16 @@ function vcat(rs::AbstractRange{T}...) where T
return a
end

Array{T,1}(r::AbstractRange{T}) where {T} = vcat(r)
collect(r::AbstractRange) = vcat(r)
function Array{T,1}(r::AbstractRange{T}) where {T}
a = Vector{T}(undef, length(r))
i = 1
for x in r
@inbounds a[i] = x
i += 1
end
return a
end
collect(r::AbstractRange) = Array(r)

_reverse(r::OrdinalRange, ::Colon) = (:)(last(r), negate(step(r)), first(r))
function _reverse(r::StepRangeLen, ::Colon)
Expand Down
20 changes: 20 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,7 @@ Base.div(x::Displacement, y::Displacement) = Displacement(div(x.val, y.val))
# required for collect (summing lengths); alternatively, should length return Int by default?
Base.promote_rule(::Type{Displacement}, ::Type{Int}) = Int
Base.convert(::Type{Int}, x::Displacement) = x.val
Base.Int(x::Displacement) = x.val

# Unsigned complement, for testing checked_length
struct UPosition <: Unsigned
Expand Down Expand Up @@ -2498,3 +2499,22 @@ end
# a case that using mul_with_overflow & add_with_overflow might get wrong:
@test (-10:2:typemax(Int))[typemax(Int)÷2+2] == typemax(Int)-9
end

@testset "collect with specialized vcat" begin
struct OneToThree <: AbstractUnitRange{Int} end
Base.size(r::OneToThree) = (3,)
Base.first(r::OneToThree) = 1
Base.length(r::OneToThree) = 3
Base.last(r::OneToThree) = 3
function Base.getindex(r::OneToThree, i::Int)
checkbounds(r, i)
i
end
Base.vcat(r::OneToThree) = r
r = OneToThree()
a = Array(r)
@test a isa Vector{Int}
@test a == r
@test collect(r) isa Vector{Int}
@test collect(r) == r
end