# Stack overflow in type inference... julia 1.9.0-DEV

**URL:** <https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694>\
**Category:** General Usage\
**Tags:** bug, stackoverflow\
**Created:** [May 8, 2022, 6:03am UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694 "2022-05-08T06:03:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjiw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jjiw/32/29944_2.png) [@jjiw](https://discourse.julialang.org/u/jjiw)\
**Post date:** [May 8, 2022, 6:03am UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694/1 "2022-05-08T06:03:18Z")

</div>

In following I show some code for the problem program, but don’t have a small demo for the problem, and at this point don’t have permission to post the whole 800-line program. I hope that someone can say what causes the problem and how to avoid the errors.

Program `toverflow3.jl` runs to completion in Julia 1.7.0-rc2, 1.7.2, and 1.9.0-DEV with ~17 sec. compilation, ~ 70 milliseconds computation. Slightly-different `toverflow4.jl` running in 1.7.2 produced 136000 error-message lines (repeating exactly the same error message set about 10000 times, plus ~80 scattered variants) in about 15 minutes before it reached an 8MiB limit I’d set via `script`. In 1.9.0-DEV, `toverflow4.jl` ran to ok completion in 27 seconds while producing some 5 dozen error-message lines. The two lines shown below repeated 30 times each:

```julia
    Internal error: stack overflow in type inference of (::findRP.var"#findMore#15"{findRP.OptValues, Array{Array{Float64, N} where N, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
    This might be caused by recursion over very long tuples or argument lists.

```

It’s true that `findMore` is recursive, but it doesn’t recurse more than 3 levels deep, and it has only one argument, `level`. In outline, it is like:

```julia
function findMore(level)::Nothing
    for v[level] in cots[v[level-1]]
    ...
        if level ≥ tVerts
                  writeRP(vops, points, v[1:tVerts])
        else
                  findMore(level+1)
        end
    end
    return nothing
 end

```

where findmore, v, cots, and tVerts all are defined in the function `findPoly3(vops, points, sds, maxVerts, cots)` and non-recursive writeRP is defined elsewhere in the module, via

```julia
   function writeRP(vops, points, v)::Nothing
       ...
      <Compute f1, f2, then output with them - see below>
      ...
    return nothing
end

```

Note, I added `::Nothing` and `return nothing` phrases for both findMore and writeRP with the notion that that might aid Julia in type inference. It made no difference; results were the same with or without those phrases.

The difference between `toverflow3.jl` and `toverflow4.jl` is shown next, along with `wc` results for both; Julia --version output; `using` line. The 10 lines marked with ! are what differ between the two files.

```julia
> diff -C3 toverflow3.jl toverflow4.jl 
*** toverflow3.jl	2022-05-07 21:11:28.705168072 -0600
--- toverflow4.jl	2022-05-07 21:11:30.325142445 -0600
***************
 ***221,231****
      println(fos("raxi",raxi), fos("zaxi",zaxi), fos("atra",atra)); flush(stdout)
      pz = [vj-1 for vj in v] # Zero-base the point indices in following
      f1 = vops.q ?
! # j n r s cx cy cz nx ny nz
! "{:4d} {:2d} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f}" :
! # j n r s cx cy cz nx ny nz
! " RPFace({:4d}, {:2d}, {:8.5f}, {:8.5f}, [{:8.5f}, {:8.5f}, {:8.5f}], [{:8.5f}, {:8.5f}, {:8.5f}], "
! printfmt(DRP.Pfi, f1, DRP.RPnumber, n, r, s, c[1],c[2],c[3], nnnv[1],nnnv[2],nnnv[3])
      f2 = vops.q ?
           string([" {:3d}" for i in pz]..., '\n') :
           string('[', [" {:3d}," for i in pz]..., "]);\n")
--- 221,231 ----
      println(fos("raxi",raxi), fos("zaxi",zaxi), fos("atra",atra)); flush(stdout)
      pz = [vj-1 for vj in v] # Zero-base the point indices in following
      f1 = vops.q ?
! # j n r s raxi zaxi cx cy cz nx ny nz
! "{:4d} {:2d} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f} {:9.5f}" :
! # j n r s raxi zaxi cx cy cz nx ny nz
! " RPFace({:4d}, {:2d}, {:8.5f}, {:8.5f}, {:8.5f}, {:8.5f}, [{:8.5f}, {:8.5f}, {:8.5f}], [{:8.5f}, {:8.5f}, {:8.5f}], "
! printfmt(DRP.Pfi, f1, DRP.RPnumber, n, r, s, raxi, zaxi, c[1],c[2],c[3], nnnv[1],nnnv[2],nnnv[3])
      f2 = vops.q ?
           string([" {:3d}" for i in pz]..., '\n') :
           string('[', [" {:3d}," for i in pz]..., "]);\n")
> wc toverflow3.jl toverflow4.jl 
  780 3349 29658 toverflow3.jl
  780 3359 29738 toverflow4.jl
 1560 6708 59396 total

> julia-1.7.0 --version
julia version 1.7.0-rc2
> julia-latest --version
julia version 1.9.0-DEV
> julia --version
julia version 1.7.2
> grep using toverflow3.jl 
using Formatting, LinearAlgebra, Rotations, JSON3

```

In above, `fos` prints a value as a formatted string. Also, the last lines of output from toverflow3 and toverflow4 are

```julia
   RPFace( 28, 3, 1.41421, 2.44949, [0.00000, 0.00000, 0.00000], [-3.00000, 3.00000, -3.00000], [3, 8, 5,]);
   RPFace( 28, 3, 1.41421, 2.44949, -0.36137, 2.96468, [0.00000, 0.00000, 0.00000], [-3.00000, 3.00000, -3.00000], [3, 8, 5,]);

```

respectively.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 8, 2022, 6:10am UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694/2 "2022-05-08T06:10:52Z")

</div>

[type inference stackoverflow · Issue #43050 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/43050) maybe?

---

<div class="post-metadata">

**Author:** ![jjiw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jjiw/32/29944_2.png) [@jjiw](https://discourse.julialang.org/u/jjiw)\
**Post date:** [May 8, 2022, 6:48am UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694/3 "2022-05-08T06:48:10Z")

</div>

I’ve looked at #43050 and perhaps a dozen other issue numbers re stack overflow and type inference. Several look much like the problem above but have been closed. I haven’t figured out why printing out two more results causes a change from no error messages to (in 1.7.2) indefinitely many, or (in 1.9.0) 30 of them, and don’t know how to interpret the `Internal error: stack overflow in type inference of (::findRP.var"#findMore#15"{findRP.OptValues, Array{Array{Float64, N} where N, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64)` message. I might end up moving `findMore` out of `findPoly3`, or might add types to more parameters … ie change things a bit randomly…

---

<div class="post-metadata">

**Author:** ![jjiw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jjiw/32/29944_2.png) [@jjiw](https://discourse.julialang.org/u/jjiw)\
**Post date:** [May 9, 2022, 12:43pm UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694/4 "2022-05-09T12:43:04Z")

</div>

I removed uses of the `OptValues` struct from `findPoly3`, `writeRP`, and `findMore` and removed conditional assignments to `f1` and `f2`, and in the process found that splitting the `printfmt` into two parts disappears the problem. Here is some output from running v4 (with split `printfmt`) vs v6 (not split), with ~26 repeated error line pairs elided (the first `internal error` message differs from the other ~28); also below: `diff`s that show outputs were the same (`stdout`s in `x` and `z`, file outputs in `x00` and `z00`), and a `diff -C1` on the two versions of code.

```julia
> Running toverflow6
Internal error: stack overflow in type inference of doR(Array{Array{Float64, N} where N, 1}, findRP.VarValues, findRP.OptValues, Char).
This might be caused by recursion over very long tuples or argument lists.
Internal error: stack overflow in type inference of (::findRP.var"#findMore#13"{Array{Array{Float64, N} where N, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
This might be caused by recursion over very long tuples or argument lists.
…
Internal error: stack overflow in type inference of (::findRP.var"#findMore#13"{Array{Array{Float64, N} where N, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
This might be caused by recursion over very long tuples or argument lists.

real	0m26.805s
user	0m25.467s
sys	0m1.480s
  64 630 4650 z
  53 668 4985 z00
 117 1298 9635 total
toverflow6 done

> Running toverflow4

real	0m8.271s
user	0m8.194s
sys	0m0.301s
  64 630 4650 x
  53 668 4985 x00
 117 1298 9635 total
toverflow4 done

> ls -lG x* z*
-rw-rw-r-- 1 j-waldby 4650 May 9 06:17 x
-rw-rw-r-- 1 j-waldby 4985 May 9 06:17 x00
-rw-rw-r-- 1 j-waldby 4650 May 9 06:16 z
-rw-rw-r-- 1 j-waldby 4985 May 9 06:16 z00

> diff x z|wc
      0 0 0
> diff x00 z00|wc
      0 0 0
> diff -C1 toverflow4.jl toverflow6.jl
*** toverflow4.jl	2022-05-09 06:08:50.047648585 -0600
--- toverflow6.jl	2022-05-09 06:09:11.034922222 -0600
***************
 ***222,225****
      pz = [vj-1 for vj in v] # Zero-base the point indices in following
! printfmt(DRP.Pfi, " RPFace({:4d}, {:2d}, {:8.5f}, {:8.5f}, {:8.5f}, {:8.5f}, ", DRP.RPnumber, n, r, s, raxi, zaxi)
! printfmt(DRP.Pfi, "[{:8.5f}, {:8.5f}, {:8.5f}], [{:8.5f}, {:8.5f}, {:8.5f}], ", c[1],c[2],c[3], nnnv[1],nnnv[2],nnnv[3])
      printfmt(DRP.Pfi, "[$(join(["{:3d}" for i in pz], ", "))]);\n", pz...)
--- 222,224 ----
      pz = [vj-1 for vj in v] # Zero-base the point indices in following
! printfmt(DRP.Pfi, " RPFace({:4d}, {:2d}, {:8.5f}, {:8.5f}, {:8.5f}, {:8.5f}, [{:8.5f}, {:8.5f}, {:8.5f}], [{:8.5f}, {:8.5f}, {:8.5f}], ", DRP.RPnumber, n, r, s, raxi, zaxi, c[1],c[2],c[3], nnnv[1],nnnv[2],nnnv[3])
      printfmt(DRP.Pfi, "[$(join(["{:3d}" for i in pz], ", "))]);\n", pz...)

```

I’ll replace several functions with stubs and see if I can reproduce the problem in sharable code.

---

<div class="post-metadata">

**Author:** ![jjiw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jjiw/32/29944_2.png) [@jjiw](https://discourse.julialang.org/u/jjiw)\
**Post date:** [May 11, 2022, 7:06am UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694/5 "2022-05-11T07:06:16Z")

</div>

Below is code that exhibits the problem, and if a character is changed, does not exhibit the problem. I removed 600+ lines of code, leaving about 163.

As noted above, splitting a certain `printfmt` call into two parts disappears the problem for this code. If the code below (which exhibits “Internal error: stack overflow in type inference…” problem) is in file `toverflow5.jl`, the `sed` command `sed -e 's/if 1<2/if 1>2/' toverflow5.jl > toverflow3.jl` will produce `toverflow3.jl` that runs without overflows.

Here is output from `diff toverflow3.jl overflow5.jl`:

```julia
< if 1>2 # sed changes < to > for other version
---
> if 1<2 # sed changes < to > for other version

```

Here is output from running the two programs. Script `Running` uses a command like `julia-latest toverflow3.jl` to run a program and a command like `cat outP00` to print out file `outP00`. [Like splitting the print, writing to standard output instead of `outP00` makes the problem go away.]

```julia
Running toverflow5
Internal error: stack overflow in type inference of (::toverflow.var"#findMore#7"{Array{Array{Float64, 1}, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
This might be caused by recursion over very long tuples or argument lists.
Internal error: stack overflow in type inference of (::toverflow.var"#findMore#7"{Array{Array{Float64, 1}, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
This might be caused by recursion over very long tuples or argument lists.
Internal error: stack overflow in type inference of (::toverflow.var"#findMore#7"{Array{Array{Float64, 1}, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
This might be caused by recursion over very long tuples or argument lists.
Internal error: stack overflow in type inference of (::toverflow.var"#findMore#7"{Array{Array{Float64, 1}, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
This might be caused by recursion over very long tuples or argument lists.
Internal error: stack overflow in type inference of (::toverflow.var"#findMore#7"{Array{Array{Float64, 1}, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
This might be caused by recursion over very long tuples or argument lists.
Internal error: stack overflow in type inference of (::toverflow.var"#findMore#7"{Array{Array{Float64, 1}, 1}, Array{Tuple, 1}, Array{Array{Float64, 1}, 1}, Array{Int32, 1}, Array{Int32, 1}, Array{Float64, 1}, Int64, Float64})(Int64).
This might be caused by recursion over very long tuples or argument lists.
RPtotals = Int32[0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0]
 contents:
( 0, 4, 1.41, 2.00, -0.31, 13.00, [13.00, 13.00, 13.00], [0.00, 0.00, -4.00], [1, 0, 4, 5]);
( 1, 4, 1.41, 2.00, 0.00, 13.00, [13.00, 13.00, 13.00], [0.00, -4.00, 0.00], [6, 2, 3, 7]);
( 2, 4, 1.41, 2.00, 0.00, 13.00, [13.00, 13.00, 13.00], [-4.00, 0.00, 0.00], [9, 8, 10, 11]);
toverflow5 done in ~ 9 seconds

Running toverflow3
RPtotals = Int32[0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0]
 contents:
( 0, 4, 1.41, 2.00, -0.31, 13.00, [13.00, 13.00, 13.00], [0.00, 0.00, -4.00], [1, 0, 4, 5]);
( 1, 4, 1.41, 2.00, 0.00, 13.00, [13.00, 13.00, 13.00], [0.00, -4.00, 0.00], [6, 2, 3, 7]);
( 2, 4, 1.41, 2.00, 0.00, 13.00, [13.00, 13.00, 13.00], [-4.00, 0.00, 0.00], [9, 8, 10, 11]);
toverflow3 done in ~ 2 seconds

```

Here is output from `julia-latest <<< 'versioninfo()'`:

```julia
Julia Version 1.9.0-DEV.485
Commit 6e06132243 (2022-05-07 08:12 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: 4 × Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 1 on 4 virtual cores

```

Here is the `toverflow5.jl` program as cut down: [Note, see my later reply for slightly smaller version]

```julia
# -*- mode: julia; -*- 
# jiw - 10 May 2022 - exhibit (or not) errors like "Internal error:
# stack overflow in type inference of (::findRP.var"#findMore#11"
# {Array{Array{Float64, 1}, 1}, ... Int64, Float64})(Int64)."

module toverflow
using Formatting, LinearAlgebra

const eps=1e-6
mutable struct DataRP
    counts :: Array{Int} 
    csize :: Int 
    sLo :: Int
    sHi :: Int
    RPnumber::Int
    fnumber:: Int
    Pfi; Cfi;
end
const DRP=DataRP(zeros(Int,200),200,0,0,0,0,0,0)
#------------------------------------------------------------
struct SDitem
    sd ::Float64
    j ::Int32
    k ::Int32
end
function Base.isless(a::SDitem, b::SDitem)
    a.sd < b.sd-eps || (abs(a.sd-b.sd)<eps && (a.j<b.j ||(a.j==b.j && a.k<b.k)))
end
#------------------------------------------------------------
struct CoItem
    co :: Int32
    ln :: Int32
end
function Base.zero(::Type{CoItem})
    CoItem(0,0)
end
#------------------------------------------------------------
function ccc3(A,B,C)
    v = (B-A)×(C-B)
    if v[3] > eps
        return true
    elseif v[3] < -eps
        return false
    elseif v[2] > eps
        return true
    elseif v[2] < -eps
        return false
    else
        return v[1] > 0
    end
end
#------------------------------------------------------------
function cosAngle3(A,B,C)
    p, q = A-B, C-B
    return min(1.0, max(-1.0, (p·q)/sqrt((p·p)*(q·q))))
end
#------------------------------------------------------------
function writeRP(points, v)::Nothing
    # writeRP is called by recursive findMore (and, in real code, by findPoly3)
    n = length(v)
    pp1 = 1; pp2 = max(1+pp1, (n+4)÷3); pp3 = max(1+pp2, (2n+3)÷3)
    p1, p2, p3 = points[v[pp1]], points[v[pp2]], points[v[pp3]]
    s0, s1, s2 = p1-points[v[2]], p1-p2, p3-p2
    s = sqrt(s0⋅s0)
    r = s/(2*sin(π/n))
    c = [13.,13.,13.]
    nnnv = s1×s2
    atra= [nnnv[2], -nnnv[1], 0.13]
    raxi = π/2 - acos(nnnv[3]/13)
    zaxi = 13.
    pz = [vj-1 for vj in v]

    # One long arg list vs two short ones in next few lines happens to
    # trigger "Internal error: stack overflow in type inference..."
    # message. Besides splitting the prints, dropping `DRP.Pfi` from
    # the prints sidesteps the problem.

    if 1<2 # sed changes < to > for other version
        printfmt(DRP.Pfi, "({:2d}, {:2d}, {:5.2f}, {:5.2f}, {:5.2f}, {:5.2f}, [{:5.2f}, {:5.2f}, {:5.2f}], [{:5.2f}, {:5.2f}, {:5.2f}], ", DRP.RPnumber, n, r, s, raxi, zaxi, c[1],c[2],c[3], nnnv[1],nnnv[2],nnnv[3])
    else
        printfmt(DRP.Pfi, "({:2d}, {:2d}, {:5.2f}, {:5.2f}, {:5.2f}, {:5.2f}, ", DRP.RPnumber, n, r, s, raxi, zaxi)
        printfmt(DRP.Pfi, "[{:5.2f}, {:5.2f}, {:5.2f}], [{:5.2f}, {:5.2f}, {:5.2f}], ", c[1],c[2],c[3], nnnv[1],nnnv[2],nnnv[3])
    end
    printfmt(DRP.Pfi, "[$(join(["{:2d}" for i in pz], ", "))]);\n", pz...)
    DRP.RPnumber += 1
    return nothing
end
#------------------------------------------------------------
function findPoly3(points, maxVerts, cots)
    αlo, αhi = π/2-eps, π/2+eps
    RPcounts = zeros(Int32, maxVerts)
    v = zeros(Int32, maxVerts)
    p = [[0.,0.,0.] for j in 1:maxVerts]
    cSiz = length(cots)
    for v[1] in 1:cSiz
        p[1] = points[v[1]]
        for v[2] in cots[v[1]]
            # Changing > to == will double the number of outputs and number
            # of "Internal error: stack overflow in type inference" messages
            if v[2] > v[1]
                continue
            end
            p[2] = points[v[2]]
            for v[3] in cots[v[2]]
                p[3] = points[v[3]]
                if ccc3(p[1],p[2],p[3])
                    γ = cosAngle3(p[1],p[2],p[3])
                    α = acos(γ)
                    if αlo > α || α > αhi
                        continue
                    end
                    tVerts = 4
                    munnv = (p[1]-p[2])×(p[2]-p[3])
                    # Recursive function
                    function findMore(level)::Nothing
                        for v[level] in cots[v[level-1]]
                            p[level] = points[v[level]]
                            if v[level] < v[1] || !ccc3(p[level-2],p[level-1],p[level])
                                continue
                            end
                            γt = cosAngle3(p[level-2],p[level-1],p[level])
                            if abs(γ-γt) > eps
                                continue
                            end
                            unnv = (p[level-2]-p[level-1])×(p[level-1]-p[level])
                            if norm(unnv-munnv) > eps
                                continue
                            end
                            if level ≥ tVerts
                                RPcounts[tVerts] += 1
                                writeRP(points, v[1:tVerts])
                            else
                                findMore(level+1)
                            end
                        end
                        return nothing
                    end
                    findMore(4) # Start recursion
                end
            end
        end
    end
    return RPcounts
end
#------------------------------------------------------------
function toverR()
    ppoints = [
        [-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [-1.0, 0.0, -1.0], [-1.0, 0.0, 1.0],
        [1.0, -1.0, 0.0], [1.0, 1.0, 0.0], [1.0, 0.0,-1.0], [1.0, 0.0, 1.0],
        [0.0, -1.0, -1.0], [0.0, -1.0, 1.0], [0.0, 1.0, -1.0], [0.0, 1.0, 1.0]];
    sds = [SDitem(4.0,1,2),SDitem(2.0,1,3),SDitem(2.0,1,4),SDitem(4.0,1,5),SDitem(8.0,1,6),SDitem(6.0,1,7),SDitem(6.0,1,8),SDitem(2.0,1,9),SDitem(2.0,1,10),SDitem(6.0,1,11),SDitem(6.0,1,12),SDitem(2.0,2,3),SDitem(2.0,2,4),SDitem(8.0,2,5),SDitem(4.0,2,6),SDitem(6.0,2,7),SDitem(6.0,2,8),SDitem(6.0,2,9),SDitem(6.0,2,10),SDitem(2.0,2,11),SDitem(2.0,2,12),SDitem(4.0,3,4),SDitem(6.0,3,5),SDitem(6.0,3,6),SDitem(4.0,3,7),SDitem(8.0,3,8),SDitem(2.0,3,9),SDitem(6.0,3,10),SDitem(2.0,3,11),SDitem(6.0,3,12),SDitem(6.0,4,5),SDitem(6.0,4,6),SDitem(8.0,4,7),SDitem(4.0,4,8),SDitem(6.0,4,9),SDitem(2.0,4,10),SDitem(6.0,4,11),SDitem(2.0,4,12),SDitem(4.0,5,6),SDitem(2.0,5,7),SDitem(2.0,5,8),SDitem(2.0,5,9),SDitem(2.0,5,10),SDitem(6.0,5,11),SDitem(6.0,5,12),SDitem(2.0,6,7),SDitem(2.0,6,8),SDitem(6.0,6,9),SDitem(6.0,6,10),SDitem(2.0,6,11),SDitem(2.0,6,12),SDitem(4.0,7,8),SDitem(2.0,7,9),SDitem(6.0,7,10),SDitem(2.0,7,11),SDitem(6.0,7,12),SDitem(6.0,8,9),SDitem(2.0,8,10),SDitem(6.0,8,11),SDitem(2.0,8,12),SDitem(4.0,9,10),SDitem(4.0,9,11),SDitem(8.0,9,12),SDitem(8.0,10,11),SDitem(4.0,10,12),SDitem(4.0,11,12)]
    DRP.Pfi = open("outP00", "w")
    DRP.RPnumber = 0
    cots = Tuple[(2, 5), (1, 6), (4, 7), (3, 8), (1, 6), (2, 5), (3, 8), (4, 7), (10, 11), (9, 12), (9, 12), (10, 11)]
    RPtotals = findPoly3(ppoints, 12, cots)
    close(DRP.Pfi)
    println("RPtotals = $RPtotals")
end
toverR()
end # module

```

---

<div class="post-metadata">

**Author:** ![jjiw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jjiw/32/29944_2.png) [@jjiw](https://discourse.julialang.org/u/jjiw)\
**Post date:** [May 11, 2022, 9:21pm UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694/6 "2022-05-11T21:21:10Z")

</div>

Here is `toverflow5.jl` reduced ~~a thousand~~ 1300 characters more, and with the `cots` variable set to produce only 4 error messages instead of 6.

```julia
# -*- mode: julia; -*- 

# jiw - 10 May 2022 - exhibit (or not) errors like "Internal error:
# stack overflow in type inference of (::findRP.var"#findMore#11"
# {Array{Array{Float64, 1}, 1}, ... Int64, Float64})(Int64)."

module toverflow
using Formatting, LinearAlgebra

const eps=1e-6
mutable struct DataRP
    RPnumber::Int
    Pfi
end
const DRP=DataRP(0,0)
#------------------------------------------------------------
function ccc3(A,B,C)
    v = (B-A)×(C-B)
    if v[3] > eps
        return true
    elseif v[3] < -eps
        return false
    elseif v[2] > eps
        return true
    elseif v[2] < -eps
        return false
    else
        return v[1] > 0
    end
end
#------------------------------------------------------------
function cosAngle3(A,B,C)
    p, q = A-B, C-B
    return min(1.0, max(-1.0, (p·q)/sqrt((p·p)*(q·q))))
end
#------------------------------------------------------------
function writeRP(points, v)::Nothing
    # writeRP is called by recursive findMore (and, in real code, by findPoly3)
    n = length(v)
    pp1 = 1; pp2 = max(1+pp1, (n+4)÷3); pp3 = max(1+pp2, (2n+3)÷3)
    p1, p2, p3 = points[v[pp1]], points[v[pp2]], points[v[pp3]]
    s0, s1, s2 = p1-points[v[2]], p1-p2, p3-p2
    s = sqrt(s0⋅s0)
    r = s/(2*sin(π/n))
    c = [13.,13.,13.]
    nnnv = s1×s2
    atra= [nnnv[2], -nnnv[1], 0.13]
    raxi = π/2 - acos(nnnv[3]/13)
    zaxi = 13.
    pz = [vj-1 for vj in v]

    # One long arg list vs two short ones in next few lines happens to
    # trigger "Internal error: stack overflow in type inference..."
    # message. Besides splitting the prints, dropping `DRP.Pfi` from
    # the prints sidesteps the problem.

    if 1<2 # sed changes < to > for other version
        printfmt(DRP.Pfi, "({:2d}, {:2d}, {:5.2f}, {:5.2f}, {:5.2f}, {:5.2f}, [{:5.2f}, {:5.2f}, {:5.2f}], [{:5.2f}, {:5.2f}, {:5.2f}], ", DRP.RPnumber, n, r, s, raxi, zaxi, c[1],c[2],c[3], nnnv[1],nnnv[2],nnnv[3])
    else
        printfmt(DRP.Pfi, "({:2d}, {:2d}, {:5.2f}, {:5.2f}, {:5.2f}, {:5.2f}, ", DRP.RPnumber, n, r, s, raxi, zaxi)
        printfmt(DRP.Pfi, "[{:5.2f}, {:5.2f}, {:5.2f}], [{:5.2f}, {:5.2f}, {:5.2f}], ", c[1],c[2],c[3], nnnv[1],nnnv[2],nnnv[3])
    end
    printfmt(DRP.Pfi, "[$(join(["{:2d}" for i in pz], ", "))]);\n", pz...)
    DRP.RPnumber += 1
    return nothing
end
#------------------------------------------------------------
function findPoly3(points, maxVerts, cots)
    αlo, αhi = π/2-eps, π/2+eps
    RPcounts = zeros(Int32, maxVerts)
    v = zeros(Int32, maxVerts)
    p = [[0.,0.,0.] for j in 1:maxVerts]
    cSiz = length(cots)
    for v[1] in 1:cSiz
        p[1] = points[v[1]]
        for v[2] in cots[v[1]]
            # Changing > to == will double the number of outputs and number
            # of "Internal error: stack overflow in type inference" messages
            if v[2] > v[1]
                continue
            end
            p[2] = points[v[2]]
            for v[3] in cots[v[2]]
                p[3] = points[v[3]]
                if ccc3(p[1],p[2],p[3])
                    γ = cosAngle3(p[1],p[2],p[3])
                    α = acos(γ)
                    if αlo > α || α > αhi
                        continue
                    end
                    tVerts = 4
                    munnv = (p[1]-p[2])×(p[2]-p[3])
                    # Recursive function
                    function findMore(level)::Nothing
                        for v[level] in cots[v[level-1]]
                            p[level] = points[v[level]]
                            if v[level] < v[1] || !ccc3(p[level-2],p[level-1],p[level])
                                continue
                            end
                            γt = cosAngle3(p[level-2],p[level-1],p[level])
                            if abs(γ-γt) > eps
                                continue
                            end
                            unnv = (p[level-2]-p[level-1])×(p[level-1]-p[level])
                            if norm(unnv-munnv) > eps
                                continue
                            end
                            if level ≥ tVerts
                                RPcounts[tVerts] += 1
                                writeRP(points, v[1:tVerts])
                            else
                                findMore(level+1)
                            end
                        end
                        return nothing
                    end
                    findMore(4) # Start recursion
                end
            end
        end
    end
    return RPcounts
end
#------------------------------------------------------------
function toverR()
    ppoints = [
        [-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [-1.0, 0.0, -1.0], [-1.0, 0.0, 1.0],
        [1.0, -1.0, 0.0], [1.0, 1.0, 0.0], [1.0, 0.0,-1.0], [1.0, 0.0, 1.0],
        [0.0, -1.0, -1.0], [0.0, -1.0, 1.0], [0.0, 1.0, -1.0], [0.0, 1.0, 1.0]];
# Reduce 6 errors & 3 output lines to 4 errors & 2 output lines via ]#
    cots = Tuple[(2, 5), (1, 6), (4, 7), (3, 8), (1, 6), (2, 5), (3, 8), (4, 7)]#, (10, 11), (9, 12), (9, 12), (10, 11)]
    DRP.Pfi = open("outP00", "w")
    DRP.RPnumber = 0
    RPtotals = findPoly3(ppoints, 12, cots)
    close(DRP.Pfi)
    println("RPtotals = $RPtotals")
end
toverR()
end # module

```

---

<div class="post-metadata">

**Author:** ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)\
**Post date:** [May 11, 2022, 10:03pm UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694/7 "2022-05-11T22:03:38Z")

</div>

It might be best to open a new issue at JuliaLang/julia. Reference both [issue #43050](https://github.com/JuliaLang/julia/issues/43050), where you commented, and [issue #44852](https://github.com/JuliaLang/julia/issues/44852), which seems to be a related type inference error.

In the new issue, post both `versioninfo(verbose=true)` and the minimum working example (toverflow5.jl + call to get error).

---

<div class="post-metadata">

**Author:** ![jjiw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jjiw/32/29944_2.png) [@jjiw](https://discourse.julialang.org/u/jjiw)\
**Post date:** [May 14, 2022, 2:00am UTC](https://discourse.julialang.org/t/stack-overflow-in-type-inference-julia-1-9-0-dev/80694/8 "2022-05-14T02:00:31Z")

</div>

I opened an issue at JuliaLang/julia:  
[Stack overflow in type inference… julia 1.9.0-DEV #45286](https://github.com/JuliaLang/julia/issues/45286)

The working example there (in second instead of first post) is manageably small, about 28 lines.
