Sisu
E0140

Instantiations that never end

Error

Built from the repository at commit a8614dc.

A generic function, class or enum cannot use itself with a type argument that grows, directly or through other declarations: f([x]) inside fn f<T>(x: T) calls f with [T] for T, and a field of type Nest<[T]> inside Nest<T> does the same for a type.

Why

Each set of type arguments a program uses gets its own compiled copy of the declaration. A copy for i64 that uses one for [i64], which uses one for [[i64]], would need copies without end, so the compiler reports the cycle instead. A use with the same arguments, such as f(x) inside f, needs no new copy, and a growing use that never comes back to its declaration, such as g([x]) in f for a g that does not use f, needs one.

Broken

fn nest<T>(x: T, n: i64) {
    if n > 0 {
        nest([x], n - 1)
    }
}

fn main() {
    nest(1, 3)
}

What sisu says

error[E0140]: `nest` instantiates itself without end
 --> E0140.sisu:3:9
  |
3 |         nest([x], n - 1)
  |         ^^^^^^^^^^^^^^^^ `nest<[T]>` here

sisu: 1 error
sisu: for more on a code, run `sisu explain <code>`: E0140

Fixed

fn nest<T>(x: T, n: i64) {
    if n > 0 {
        nest(x, n - 1)
    }
}

fn main() {
    nest(1, 3)
}

Related

  • E0027: a class or enum no value can be built of, which a growing field may also make.