From 81c0a22e5ef74bf777cdc4b6305f7c85453d4eb8 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Fri, 7 Apr 2017 15:39:08 +0200 Subject: [PATCH 1/3] Workaround for type inference problem in `index_sizes` Wokraround for JuliaLang/julia#21244. --- src/indexing.jl | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/indexing.jl b/src/indexing.jl index 2b35270b..f7c0a643 100644 --- a/src/indexing.jl +++ b/src/indexing.jl @@ -52,18 +52,17 @@ end ## Indexing utilities ## ######################### -@pure increment(::Type{Val{N}}) where {N} = Val{N+1} - -@inline index_sizes(s::Size, inds...) = _index_sizes(s, Val{1}, (), inds...) -@inline _index_sizes(s::Size, ::Type{Val{N}}, x::Tuple) where {N} = x -@inline _index_sizes(s::Size, v::Type{Val{N}}, x::Tuple, ::Int, inds...) where {N} = _index_sizes(s, increment(v), (x..., Size()), inds...) -@inline _index_sizes(s::Size, v::Type{Val{N}}, x::Tuple, a::StaticArray, inds...) where {N} = _index_sizes(s, increment(v), (x..., Size(a)), inds...) -@inline _index_sizes(s::Size, v::Type{Val{N}}, x::Tuple, a::Colon, inds...) where {N} = _index_sizes(s, increment(v), (x..., Size(s[N])), inds...) - -@inline index_sizes(inds...) = _index_sizes(Val{1}, (), inds...) -@inline _index_sizes(::Type{Val{N}}, x::Tuple) where {N} = x -@inline _index_sizes(v::Type{Val{N}}, x::Tuple, ::Int, inds...) where {N} = _index_sizes(increment(v), (x..., Size()), inds...) -@inline _index_sizes(v::Type{Val{N}}, x::Tuple, a::StaticArray, inds...) where {N} = _index_sizes(increment(v), (x..., Size(a)), inds...) +@pure tail(::Type{Size{S}}) where {S} = Size{Base.tail(S)} +@inline tail(::S) where {S<:Size} = tail(S)() + +@inline index_sizes(s::Size) = () +@inline index_sizes(s::Size, ::Int, inds...) = (Size(), index_sizes(tail(s), inds...)...) +@inline index_sizes(s::Size, a::StaticArray, inds...) = (Size(a), index_sizes(tail(s), inds...)...) +@inline index_sizes(s::Size, ::Colon, inds...) = (Size(s[1]), index_sizes(tail(s), inds...)...) + +@inline index_sizes() = () +@inline index_sizes(::Int, inds...) = (Size(), index_sizes(inds...)...) +@inline index_sizes(a::StaticArray, inds...) = (Size(a), index_sizes(inds...)...) out_index_size(ind_sizes::Type{<:Size}...) = Size(_out_index_size((), ind_sizes...)) @inline _out_index_size(t::Tuple) = t From bfe0273ec384cbb9db353dbcf432b912afeaf0ac Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Fri, 7 Apr 2017 16:06:35 +0200 Subject: [PATCH 2/3] Add test for inferability of `index_sizes` --- test/indexing.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/indexing.jl b/test/indexing.jl index 74a85003..5e0e5f92 100644 --- a/test/indexing.jl +++ b/test/indexing.jl @@ -124,4 +124,10 @@ a[SVector{0,Int}()] = 5.0 @test b == a end + + @testset "inferabilty of index_sizes helper" begin + # see JuliaLang/julia#21244 + # it's not about inferring the correct type, but about inference throwing an error + @test code_warntype(DevNull, StaticArrays.index_sizes, Tuple{Vararg{Any}}) == nothing + end end From 09a86d02a91362c8b3d7b3e5bd3243ff16f749f4 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Mon, 10 Apr 2017 09:27:51 +0200 Subject: [PATCH 3/3] Fix `index_sizes(s::Size, inds...)` for `s` shorter than `inds` --- src/indexing.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/indexing.jl b/src/indexing.jl index f7c0a643..6d967c30 100644 --- a/src/indexing.jl +++ b/src/indexing.jl @@ -54,6 +54,7 @@ end @pure tail(::Type{Size{S}}) where {S} = Size{Base.tail(S)} @inline tail(::S) where {S<:Size} = tail(S)() +@inline tail(s::Size{()}) = s @inline index_sizes(s::Size) = () @inline index_sizes(s::Size, ::Int, inds...) = (Size(), index_sizes(tail(s), inds...)...)