Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete usage of inference in SubArray code #12409

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 20 additions & 11 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ end
push!(sizeexprs, dimsizeexpr(j, Jindex, length(Jp), :V, :J))
end
push!(indexexprs, :(reindex(V.indexes[$IVindex], J[$Jindex])))
push!(Itypes, rangetype(iv, j))
push!(Itypes, indextype(iv, j))
else
# We have a linear index that spans more than one
# dimension of the parent
N += 1
push!(sizeexprs, dimsizeexpr(j, Jindex, length(Jp), :V, :J))
push!(indexexprs, :(merge_indexes(V, V.indexes[$IVindex:end], size(V.parent)[$IVindex:end], J[$Jindex], $Jindex)))
push!(indexexprs, :(merge_indexes(V, V.indexes[$IVindex:end], size(V.parent)[$IVindex:end], J[$Jindex], $Jindex)::Array{Int,1}))
push!(Itypes, Array{Int, 1})
Iindex_lin = length(Itypes)
Jindex_lin = Jindex
Expand Down Expand Up @@ -215,7 +215,7 @@ end
quote
Inew = $Inew
$exfirst
SubArray{$T,$N,$PV,$It,$LD}(V.parent, Inew, $dims, f, $strideexpr)
SubArray{$T,$N,$PV,typeof(Inew),$LD}(V.parent, Inew, $dims, f, $strideexpr)
end
end

Expand Down Expand Up @@ -258,11 +258,11 @@ end
elseif Jindex < length(Jp) || Jindex == NV || IVindex == length(IVp)
# simple indexing
push!(indexexprs, :(reindex(V.indexes[$IVindex], J[$Jindex])))
push!(Itypes, rangetype(iv, j))
push!(Itypes, indextype(iv, j))
push!(ItypesLD, Itypes[end])
else
# We have a linear index that spans more than one dimension of the parent
push!(indexexprs, :(merge_indexes(V, V.indexes[$IVindex:end], size(V.parent)[$IVindex:end], J[$Jindex], $Jindex)))
push!(indexexprs, :(merge_indexes(V, V.indexes[$IVindex:end], size(V.parent)[$IVindex:end], J[$Jindex], $Jindex)::Array{Int,1}))
push!(Itypes, Array{Int, 1})
push!(ItypesLD, Itypes[end])
break
Expand Down Expand Up @@ -302,15 +302,24 @@ end
$preex
Inew = $Inew
$exfirst
SubArray{$T,$N,$PV,$It,$LD}(V.parent, Inew, $dims, f, $strideexpr)
SubArray{$T,$N,$PV,typeof(Inew),$LD}(V.parent, Inew, $dims, f, $strideexpr)
end
end

function rangetype(T1, T2)
rt = return_types(getindex, Tuple{T1, T2})
length(rt) == 1 || error("Can't infer return type")
rt[1]
end
# These pretty much only have to get the container-type right,
# for use in nextLD. The actual index type is set by typeof(I).
indextype{A<:AbstractArray,I<:Integer}(::Type{A}, ::Type{I}) = eltype(A)
indextype{A<:UnitRange,I<:UnitRange}(::Type{A}, ::Type{I}) = A
indextype{A<:Range,I<:UnitRange}(::Type{A}, ::Type{I}) = A
indextype{A<:UnitRange,I<:Range}(::Type{A}, ::Type{I}) = I
indextype{A<:Range,I<:Range}(::Type{A}, ::Type{I}) = A
indextype{A<:AbstractArray,I<:Range}(::Type{A}, ::Type{I}) = A
indextype{A<:Range,I<:AbstractArray}(::Type{A}, ::Type{I}) = Array{Int,1}
indextype{A<:AbstractArray,I<:AbstractArray}(::Type{A}, ::Type{I}) = Array{Int,1}
indextype(::Type{Colon}, ::Type{Colon}) = Colon
indextype{I<:Integer}(::Type{Colon}, ::Type{I}) = Int
indextype{I<:AbstractArray}(::Type{Colon}, ::Type{I}) = I
indextype{A<:AbstractArray}(::Type{A}, ::Type{Colon}) = A

reindex(a, b) = a[b]
reindex(a::UnitRange, b::UnitRange{Int}) = range(oftype(first(a), first(a)+first(b)-1), length(b))
Expand Down
25 changes: 19 additions & 6 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ end
function test_linear(A, B)
length(A) == length(B) || error("length mismatch")
isgood = true
for (iA, iB) in zip(1:length(A), 1:length(B))
if A[iA] != B[iB]
for i = 1:length(A)
if A[i] != B[i]
isgood = false
break
end
Expand Down Expand Up @@ -214,7 +214,14 @@ function runtests(A::Array, I...)
ldc = Base.subarray_linearindexing_dim(typeof(A), typeof(I))
ld == ldc || err_li(I, ld, ldc)
# sub
S = sub(A, I...)
local S
try
S = @inferred(sub(A, I...))
catch err
@show typeof(A)
@show I
rethrow(err)
end
getLD(S) == ldc || err_li(S, ldc)
if Base.iscontiguous(S)
@test S.stride1 == 1
Expand All @@ -223,7 +230,13 @@ function runtests(A::Array, I...)
test_cartesian(S, C)
test_mixed(S, C)
# slice
S = slice(A, I...)
try
S = @inferred(slice(A, I...))
catch err
@show typeof(A)
@show I
rethrow(err)
end
getLD(S) == ldc || err_li(S, ldc)
test_linear(S, C)
test_cartesian(S, C)
Expand Down Expand Up @@ -258,7 +271,7 @@ function runtests(A::SubArray, I...)
# sub
local S
try
S = sub(A, I...)
S = @inferred(sub(A, I...))
catch err
@show typeof(A)
@show A.indexes
Expand All @@ -272,7 +285,7 @@ function runtests(A::SubArray, I...)
test_mixed(S, C)
# slice
try
S = slice(A, I...)
S = @inferred(slice(A, I...))
catch err
@show typeof(A)
@show A.indexes
Expand Down