# When does julia give up on type inference?

**URL:** <https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 1, 2019, 2:00pm UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077 "2019-08-01T14:00:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [August 1, 2019, 2:00pm UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077/1 "2019-08-01T14:00:25Z")

</div>

`f` is a function that has quite a few methods and I use it somewhere deep in the call stack. I have two very similar methods, `g`, in one of them the return type of `f` is always inferred correctly for all `x`, and in the other one the return type of `f` is `Any`. Something like:

```julia
function g(x, y, z)
  fx = f(x) # inferred as ::Any
  h(fx, y, z)
end

function g(x, y)
  fx = f(x) # inferred correctly
  h(fx, y)
end

```

What are the criteria for `julia` to give up on type inference?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 1, 2019, 3:20pm UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077/2 "2019-08-01T15:20:59Z")

</div>

There’s no spec (it doesn’t affect program behavior), it’s implementation-defined:

[https://github.com/JuliaLang/julia/blob/master/base/compiler/typeinfer.jl](https://github.com/JuliaLang/julia/blob/master/base/compiler/typeinfer.jl)

If you want details on why this particular case isn’t getting type inferred, you’ll need to provide more context.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 1, 2019, 3:24pm UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077/3 "2019-08-01T15:24:36Z")

</div>

Cthulhu.jl can be helpful in helping you debug these sorts of problems — you can `@descend` into the function and keep asking Julia for its code\_warntype until you spot the issue.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [August 1, 2019, 3:36pm UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077/4 "2019-08-01T15:36:29Z")

</div>

> [@StefanKarpinski](#):
>
> (it doesn’t affect program behavior)

I probably wouldn’t even have notice this, because it is not in a performance sensitive part of the code but the issue was that there were only `h(fx::SomeAbstractType, ...)` methods which caused the compiler to assume that `h` would never be called and the program crashed with “Reached the unreachable” errors.

This could easily be fixed by adding `f(x)::SomeAbstractType` but finding the issue caused me a lot of headache.

> [@mbauman](#):
>
> Cthulhu.jl can be helpful in helping you debug these sorts of problems — you can `@descend` into the function and keep asking Julia for its code\_warntype until you spot the issue.

I finally found the issue with `Cthulhu.jl`, it saved me from madness. 🐙

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 1, 2019, 3:42pm UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077/5 "2019-08-01T15:42:25Z")

</div>

> [@gdkrmr](#):
>
> the program crashed with “Reached the unreachable” errors

Reached the unreachable errors are _always_ Julia bugs — not yours! If you can, please take the time to file a bug report so we can continue shaking these things out.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [August 1, 2019, 8:47pm UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077/6 "2019-08-01T20:47:56Z")

</div>

I wasn’t able to make a reproducible example, hence this topic.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [August 1, 2019, 9:38pm UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077/7 "2019-08-01T21:38:10Z")

</div>

This example has the same basic structure but is no failing:

```julia
abstract type TheAbstract end
struct ConcreteA <: TheAbstract end
struct ConcreteB <: TheAbstract end
struct ConcreteC <: TheAbstract end

abstract type TheOtherAbstract end
struct OtherA <: TheOtherAbstract end
struct OtherB <: TheOtherAbstract end
struct OtherC <: TheOtherAbstract end

f(x::ConcreteA) = OtherA()
f(x::ConcreteB) = OtherB()
f(x::ConcreteC) = OtherC()

run_type(::OtherA) = "run_otherA"
run_type(::OtherB) = "run_otherB"
run_type(::OtherC) = "run_otherC"

h(x::TheOtherAbstract, y::Float64, z::Float64) = println("h(x, y, z), ", typeof(x), " ", run_type(x))
h(x::TheOtherAbstract, y::Float64) = println("h(x, y), ", typeof(x), " ", run_type(x))

function g(x::TheAbstract, y, z)
    fx = f(x)
    h(fx, y , z)
end

function g(x::TheAbstract, y)
    fx = f(x)::Any # simulate failing type inference
    h(fx, y) # reached the unreachable error
end

g(ConcreteA(), 1.0, 1.0)
g(ConcreteA(), 1.0)

```

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [August 2, 2019, 9:54am UTC](https://discourse.julialang.org/t/when-does-julia-give-up-on-type-inference/27077/8 "2019-08-02T09:54:29Z")

</div>

I broke out all the necessary code into a repo and opened a github issue:

[https://github.com/JuliaLang/julia/issues/32766](https://github.com/JuliaLang/julia/issues/32766)

It seems fixed on Julia 1.3-alpha and master, but occurs on 1.0-1.2.
