Failure in type inference when indexing Tuple with UnitRange (?)

I believe this may be a bug, but thought I’d ask here first before filing an issue:

julia> foo{N,T}(bar::NTuple{N,T})=sum(bar[1:N]);@code_warntype foo((1,))
Variables:
#self#::#foo
bar::Tuple{Int64}

Body:
begin
return (Main.sum)((Expr(:invoke, LambdaInfo for getindex(::Tuple{Int64}, ::UnitRange{Int64}), :(Main.getindex), :(bar), :((Expr(:new, UnitRange{Int64}, 1, :((Base.select_value)((Base.sle_int)(1,(Expr(:static_parameter, 1)))::Bool,(Expr(:static_parameter, 1)),(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64)))))))::Any
end::Any

Annotating the return type doesn’t help:

julia> workspace();foo{N,T}(bar::NTuple{N,T})::Int=sum(bar[1:N]);@code_warntype foo((1,))
Variables:
#self#::#foo
bar::Tuple{Int64}

Body:
begin
return (Base.convert)(Main.Int,(Main.sum)((Expr(:invoke, LambdaInfo for getindex(::Tuple{Int64}, ::UnitRange{Int64}), :(Main.getindex), :(bar), :((Expr(:new, UnitRange{Int64}, 1, :((Base.select_value)((Base.sle_int)(1,(Expr(:static_parameter, 1)))::Bool,(Expr(:static_parameter, 1)),(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64)))))))::Any)::Any
end::Any

Version info:

julia> versioninfo()
Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
Platform Info:
System: NT (x86_64-w64-mingw32)
CPU: Intel(R) Processor 5Y10 CPU @ 0.80GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.7.1 (ORCJIT, broadwell)

Any suggestions for a workaround? Splatting the tuple into an array first within the function resolves the type inference issue, but generates horrible looking code.

Thanks!
Simon

If you really need tuple, you generally want to do most computation in type domain. 1:N puts it into a value so putting it back to a type with bar[1:N] won’t be inferrable. (Writing a loop should be inferrable though)

Return type annotation doesn’t work is fixed on master.

Thanks yuyichao! A loop works fine indeed. Should have thought of that first, must be my Matlab upbringing getting the better of me :blush: