From ae58ea5036746db9dcd5b52733abdfe349727d14 Mon Sep 17 00:00:00 2001 From: Raghvendra Gupta Date: Mon, 17 Dec 2018 20:20:13 +0530 Subject: [PATCH] Fix #30006, getindex accessing fields that might not exist (#30405) * Fix #30006, range getindex accessing fields that might not exist * Add tests for #30006 --- base/range.jl | 4 ++-- stdlib/SparseArrays/test/sparse.jl | 4 ++++ test/ranges.jl | 7 +++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/base/range.jl b/base/range.jl index af28228cd74ac9..517d0456db5e8e 100644 --- a/base/range.jl +++ b/base/range.jl @@ -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 diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index 79290cfc6433c9..76dd90bc74c480 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -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) diff --git a/test/ranges.jl b/test/ranges.jl index cfb41a477ebef6..dba2e8b24983cc 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -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