# "A Tragedy of Julia’s Type System"

**URL:** https://discourse.julialang.org/t/a-tragedy-of-julia-s-type-system/124619
**Category:** Offtopic
**Created:** [January 10, 2025, 3:12am UTC](https://discourse.julialang.org/t/a-tragedy-of-julia-s-type-system/124619 "2025-01-10T03:12:02Z")
**Posts on this page:** 1
**Showing post:** 36

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [January 13, 2025, 5:10pm UTC](https://discourse.julialang.org/t/a-tragedy-of-julia-s-type-system/124619/36 "2025-01-13T17:10:11Z")

</div>

> [@foobar\_lv2](#):
>
> So, at least @Benny is of the opinion that it _has_ to be that way, i.e. that inference of non-SSA captures will always and unavoidable suck.
> 
> But additionally, it sucks extra hard, because the lowering is dumb. E.g.
> 
> ```julia
> function f()
> x = 0
> if false x = 1 end
> g()=x
> g()
> end
> 
> ```
> 
> gleefully infers as Any. This is because lowering is dumb and only sees the AST, and neither CFG nor const-prop nor escape analysis nor anything!
> 
> I tend to agree with @Benny, the problem is hard, and it will always suck. But it doesn’t need to suck _that_ badly.

For what it’s worth, there’s a currently open PR specifically trying to fix cases like this right now under active development: [perform inference using optimizer-derived type information by aviatesk · Pull Request #56687 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/56687)

The idea is that after things like type inference and dead code elimination, the compiler can go back to a very early point in the pipeline and use knowledge it learned later to say

> ah, `x`s type never changes and can’t be lexically modified anywhere in the program, so it’s okay to just let `g` return `0` instead of forcing it to go through a `Box`

I’m pretty optimistic this can work. Specifically check out the tests the PR adds showing it can correctly infer return types in very similar functions: [julia/Compiler/test/inference.jl at b775f2008d944c376f4f3f9f48ff2a096eb66a74 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/b775f2008d944c376f4f3f9f48ff2a096eb66a74/Compiler/test/inference.jl#L6166-L6192)

These tests are already passing.

---

_[View the full topic](https://discourse.julialang.org/t/a-tragedy-of-julia-s-type-system/124619)._
