-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Description
This issue elaborates on parts of #33326.
One of the most crucial choices in our type inference algorithm is what to do when a call site has multiple possible targets (i.e. the inferred argument types aren't specific enough to know exactly which method would be called). Currently, we simply infer up to 4 possible targets, and if more than 4 match we give up and say Any
. So for example you can get the following behavior:
julia> f(::Int) = 0;
julia> f(::String) = 0;
julia> f(::Dict) = 0;
julia> f(::Array) = 0;
julia> g() = f(Any[1][1]);
julia> @code_typed g()
...
) => Int64
julia> f(::Tuple) = 0;
julia> @code_typed g()
...
) => Any
This is clearly non-ideal, since the inferred type changes abruptly when a fifth unrelated method is added. To some extent this is unavoidable --- any inference of call sites with inexact argument types might change if a method is added or its signature changes. But, a new algorithm could still do better in three ways: (1) be less arbitrary and more intuitive, (2) make inference generally faster, (3) reduce the likelihood of inference "blowups" as in issues like #34098, #34681.
I have tried a couple alternate algorithms; data incoming!