Skip to content

Commit

Permalink
Fix #30006, getindex accessing fields that might not exist (#30405)
Browse files Browse the repository at this point in the history
* Fix #30006, range getindex accessing fields that might not exist
* Add tests for #30006

(cherry picked from commit 64133f6)
  • Loading branch information
raghav9-97 authored and KristofferC committed Dec 31, 2018
1 parent aede024 commit e4a7db2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ function getindex(v::AbstractRange{T}, i::Integer) where T
@_inline_meta
ret = convert(T, first(v) + (i - 1)*step_hp(v))
ok = ifelse(step(v) > zero(step(v)),
(ret <= v.stop) & (ret >= v.start),
(ret <= v.start) & (ret >= v.stop))
(ret <= last(v)) & (ret >= first(v)),
(ret <= first(v)) & (ret >= last(v)))
@boundscheck ((i > 0) & ok) || throw_boundserror(v, i)
ret
end
Expand Down
4 changes: 4 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ do33 = fill(1.,3)
end
end

@testset "Issue #30006" begin
SparseMatrixCSC{Float64,Int32}(spzeros(3,3))[:, 1] == [1, 2, 3]
end

@testset "concatenation tests" begin
sp33 = sparse(1.0I, 3, 3)

Expand Down
7 changes: 7 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,13 @@ end
@test @inferred(z4 .+ z4) === z4
end

@testset "Issue #30006" begin
@test Base.Slice(Base.OneTo(5))[Int32(1)] == Int32(1)
@test Base.Slice(Base.OneTo(3))[Int8(2)] == Int8(2)
@test Base.Slice(1:10)[Int32(2)] == Int32(2)
@test Base.Slice(1:10)[Int8(2)] == Int8(2)
end

@testset "allocation of TwicePrecision call" begin
0:286.493442:360
0:286:360
Expand Down

0 comments on commit e4a7db2

Please sign in to comment.