Implementing Tarjan's strongly connected components algorithm in a language without exceptions or undefined behavior
There will always be situations where we can see there is a complex invariant but the compiler cannot derive it.  Type systems are sound but not complete: not all invariants can be expressed in the type system.  So this isn't something specific to Tarjan's algorithm; it is a general fact of life when working in a type system -- sometimes the compiler will insert type checks that the programmer can tell won't be necessary, and similarly, as a programmer you might need to insert type checks that you can tell won't be necessary.