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

Workaround for type inference problem in index_sizes #139

Merged
merged 3 commits into from
Apr 15, 2017
Merged
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
24 changes: 12 additions & 12 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ 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 tail(s::Size{()}) = 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
Expand Down
6 changes: 6 additions & 0 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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