JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.6k stars 5.48k forks source link

suboptimal abstract return type inference involving `getindex(::Tuple)` #55155

Open nsajko opened 3 months ago

nsajko commented 3 months ago
julia> f(t::Tuple, n::Bool) = t[10 + n]
f (generic function with 1 method)

julia> Core.Compiler.return_type(f, Tuple{Tuple{Bool,String,Vararg{Int}},Bool})
Union{Bool, Int64, String}

julia> versioninfo()
Julia Version 1.12.0-DEV.unknown
Commit 0fdd655 (2024-07-17 15:39 UTC)

Ideally, the inferred return type would be Int. Proof: the index 10 + n is always either 10 or 11, thus the index is always greater than two.

Context, hypothesized solution approach using refinement types: https://discourse.julialang.org/t/would-it-make-sense-for-julia-to-adopt-refinement-types/113586/10

JeffBezanson commented 2 months ago

Ok good point, but this is pretty greedy :smile: