Skip to content

Commit b643ff5

Browse files
authored
Merge pull request #139 from martinholters/mh/index_sizes
Workaround for type inference problem in `index_sizes`
2 parents 836f874 + 09a86d0 commit b643ff5

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/indexing.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ end
5252
## Indexing utilities ##
5353
#########################
5454

55-
@pure increment(::Type{Val{N}}) where {N} = Val{N+1}
56-
57-
@inline index_sizes(s::Size, inds...) = _index_sizes(s, Val{1}, (), inds...)
58-
@inline _index_sizes(s::Size, ::Type{Val{N}}, x::Tuple) where {N} = x
59-
@inline _index_sizes(s::Size, v::Type{Val{N}}, x::Tuple, ::Int, inds...) where {N} = _index_sizes(s, increment(v), (x..., Size()), inds...)
60-
@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...)
61-
@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...)
62-
63-
@inline index_sizes(inds...) = _index_sizes(Val{1}, (), inds...)
64-
@inline _index_sizes(::Type{Val{N}}, x::Tuple) where {N} = x
65-
@inline _index_sizes(v::Type{Val{N}}, x::Tuple, ::Int, inds...) where {N} = _index_sizes(increment(v), (x..., Size()), inds...)
66-
@inline _index_sizes(v::Type{Val{N}}, x::Tuple, a::StaticArray, inds...) where {N} = _index_sizes(increment(v), (x..., Size(a)), inds...)
55+
@pure tail(::Type{Size{S}}) where {S} = Size{Base.tail(S)}
56+
@inline tail(::S) where {S<:Size} = tail(S)()
57+
@inline tail(s::Size{()}) = s
58+
59+
@inline index_sizes(s::Size) = ()
60+
@inline index_sizes(s::Size, ::Int, inds...) = (Size(), index_sizes(tail(s), inds...)...)
61+
@inline index_sizes(s::Size, a::StaticArray, inds...) = (Size(a), index_sizes(tail(s), inds...)...)
62+
@inline index_sizes(s::Size, ::Colon, inds...) = (Size(s[1]), index_sizes(tail(s), inds...)...)
63+
64+
@inline index_sizes() = ()
65+
@inline index_sizes(::Int, inds...) = (Size(), index_sizes(inds...)...)
66+
@inline index_sizes(a::StaticArray, inds...) = (Size(a), index_sizes(inds...)...)
6767

6868
out_index_size(ind_sizes::Type{<:Size}...) = Size(_out_index_size((), ind_sizes...))
6969
@inline _out_index_size(t::Tuple) = t

test/indexing.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,10 @@
124124
a[SVector{0,Int}()] = 5.0
125125
@test b == a
126126
end
127+
128+
@testset "inferabilty of index_sizes helper" begin
129+
# see JuliaLang/julia#21244
130+
# it's not about inferring the correct type, but about inference throwing an error
131+
@test code_warntype(DevNull, StaticArrays.index_sizes, Tuple{Vararg{Any}}) == nothing
132+
end
127133
end

0 commit comments

Comments
 (0)