Lyricdev
GitHub

LYR-SEM0056

a value type that contains itself

error · spec 03-types

A struct or enum holds itself by value — directly or through other structs, enum payloads, optionals or tuples — and would have infinite size.

Explanation

A struct field or an enum payload is stored inside the value, so a value type that contains itself has no finite layout. The message names the cycle.

A reference breaks the chain: put the recursive part behind a class, such as Box<T> of std.core, or in an array.

Wrong

enum Tree {
    Leaf(int),
    Node(Tree, Tree),
}

fn main(): void {
    let _ = Tree.Leaf(1);
}
enum Tree {
    Leaf(int),
    Node(Box<Tree>, Box<Tree>),
}

fn main(): void {
    let _ = Tree.Leaf(1);
}