# Types truncated in v0.6.2 but not in v0.7?

**URL:** <https://discourse.julialang.org/t/types-truncated-in-v0-6-2-but-not-in-v0-7/9541>\
**Category:** Internals & Design\
**Created:** [March 6, 2018, 4:20pm UTC](https://discourse.julialang.org/t/types-truncated-in-v0-6-2-but-not-in-v0-7/9541 "2018-03-06T16:20:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jamii/32/1373_2.png) [@jamii](https://discourse.julialang.org/u/jamii)\
**Post date:** [March 6, 2018, 4:20pm UTC](https://discourse.julialang.org/t/types-truncated-in-v0-6-2-but-not-in-v0-7/9541/1 "2018-03-06T16:20:33Z")

</div>

I’m playing around with using type specialization to turn interpreters into compilers. I ran into this roadblock with the types being truncated when they get too deep, even if they are actual concrete types present in the input rather than inferred outputs.

```julia
struct Foo{T}
  t::T
end

function f(n)
  if n == 0
    0
  else
    Foo(f(n-1))
  end
end

function t(foo)
  foo.t
end

@code_warntype t(f(20))

```

```julia
Variables:
  #self# <optimized out>
  foo::Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Int64}}}}}}}}}}}}}}}}}}}}

Body:
  begin
      return (Core.getfield)(foo::Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Int64}}}}}}}}}}}}}}}}}}}}, :t)::Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{_}}}}}}}}} where _<:Foo
  end::Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{_}}}}}}}}} where _<:Foo

```

I can’t reproduce this in v0.7, but it’s sometimes fiddly to trigger in v0.6.2 too. So does anyone know if something fundamental changed or if I’ll still have problems trying to do this?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 6, 2018, 5:31pm UTC](https://discourse.julialang.org/t/types-truncated-in-v0-6-2-but-not-in-v0-7/9541/2 "2018-03-06T17:31:47Z")

</div>

You’re just seeing inference give up at some point. The correct type is returned, but Julia’s type-inference just can’t _prove_ what that type will be at compile-time.

```julia
julia> typeof(f(20))
Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Foo{Int64}}}}}}}}}}}}}}}}}}}}

```

The fact that this gets inferred at all is actually pretty impressive, since you’re doing something fundamentally type-unstable. The _type_ of the output of the function `f()` depends on the _value_ of `n`, which is the definition of type instability. Maybe if you explain what you’re trying to accomplish it will be easier to suggest a solution.

---

<div class="post-metadata">

**Author:** ![jamii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jamii/32/1373_2.png) [@jamii](https://discourse.julialang.org/u/jamii)\
**Post date:** [March 6, 2018, 5:57pm UTC](https://discourse.julialang.org/t/types-truncated-in-v0-6-2-but-not-in-v0-7/9541/3 "2018-03-06T17:57:15Z")

</div>

> The fact that this gets inferred at all is actually pretty impressive, since you’re doing something fundamentally type-unstable.

It’s not inferring the type of `f(20)`, it’s just running that and then calling `code_warntype(t, Foo{Foo{Foo{...}}})`. And `t` should absolutely be type-stable:

```julia
struct Foo{T}
  t::T
end

function t(foo)
  foo.t
end

```

But the return type of `getfield` gets truncated, by a heuristic that is presumably trying to curtail runaway type inference.

Luckily, the heuristic in v0.7 seems to be smarter, but it’s also possible that I just need some more complicated examples to trigger it. I’m asking if anyone **knows** what changed so I can figure out what I can safely do.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 6, 2018, 6:56pm UTC](https://discourse.julialang.org/t/types-truncated-in-v0-6-2-but-not-in-v0-7/9541/4 "2018-03-06T18:56:11Z")

</div>

Ah, I see. My mistake.
