# A parametric-type related crash on Julia 1.7+

**URL:** <https://discourse.julialang.org/t/a-parametric-type-related-crash-on-julia-1-7/77844>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [March 14, 2022, 3:43am UTC](https://discourse.julialang.org/t/a-parametric-type-related-crash-on-julia-1-7/77844 "2022-03-14T03:43:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [March 14, 2022, 3:43am UTC](https://discourse.julialang.org/t/a-parametric-type-related-crash-on-julia-1-7/77844/1 "2022-03-14T03:43:11Z")

</div>

I discovered a very strange crash scenario while developing my package. It seems to only happen on Julia 1.7+. For example, 1.6 runs perfectly well. It took me some time to create this “MWE” from my code base and I hope someone can look into the Julia source code to see what’s going on. Thanks!

```julia
struct T1{T}
    m::T
end

struct T2{L}
    tuple::NTuple{3, Int}

    function T2{L}(t::NTuple{3, Int}) where {L}
        new{sum(t)}(t)
    end
end

struct T3{L, N}
    a::Tuple{T2{L}}
    param::NTuple{N, T1}
    function T3(a::Tuple{T2{L}}, pars::NTuple{N, T1}) where {L, N}
        new{L, N}(a, pars)
    end
end

function f(ts)
    pars = []
    for t in ts
        append!(pars, t.param)
    end
    pars
end

t1_1 = ((T1{Float64}(1.0), T1{Int}(2)))

t1_2 = ((T1{Float64}(1.2), T1{Int}(1)))

t2_1 = (T2{1}((1,0,0)),)

t2_2 = (T2{2}((2,0,0)),)

[T3(t2_1, t1_1), T3(t2_2, t1_2)] |> f

```

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [March 14, 2022, 7:41am UTC](https://discourse.julialang.org/t/a-parametric-type-related-crash-on-julia-1-7/77844/2 "2022-03-14T07:41:16Z")

</div>

After some debugging, I find out that this is because Julia tries to become smarter in Julia 1.7 and unfortunately miscalculates field offset.

The core problem is `t.param`, here `t` is inferred as an abstract type `T3{L, 2} where L`. But since `L` is only used for T2 and it doesn’t actually impact the size of T2 (I think you do this deliberately), the size of field `a` is always 24 (3 Int) and stored inline in T3, so a simple pointer arithmetic can be used to load the value of `param`.

In Julia 1.6 there’s no such optimization and Julia simply emits a dynamic call to `jl_f_getfield` and everything is fine. In Julia 1.7 the above optimization is applied and the field offset is wrongly calculated. It should be 24, but calculated as 8. It’s actually something like `fieldoffset(T3{L,2},2)`, where `L` is a type variable. So it will instead load the second element of the field `a`. If you change `t2_1` to `t2_1 = (T2{1}((1,100,0)),) `, then you can get a seg fault caused by dereferencing a 100(0x64) pointer. I think the logic is incorrect somewhere, but since I am not familiar with Julia’s type system so I simply left it here for further exploration.

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [March 14, 2022, 8:26am UTC](https://discourse.julialang.org/t/a-parametric-type-related-crash-on-julia-1-7/77844/3 "2022-03-14T08:26:52Z")

</div>

@frankwswang Have you opened an issue on github? This is definitely a bug needed to be fixed.

---

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [March 14, 2022, 4:49pm UTC](https://discourse.julialang.org/t/a-parametric-type-related-crash-on-julia-1-7/77844/4 "2022-03-14T16:49:37Z")

</div>

No. I will file one now. Thanks for the explanation!
