Skip to content

TP0230

Error: recursive type alias

  • type alias '<A>' refers to itself
  • type alias '<A>' refers to itself through '<B>' ('<B>' and '<C>' for longer cycles), with help use a class for a recursive type (M18)

From the golden case tests/cases/m18/tp0230_recursive.tpn:

tp0230_recursive.tpn
# Recursive aliases (SPEC §3.12): each cycle is reported once, at its first alias.
type Tree = list[Tree]
type A = list[B]
type B = A?
type UsesCycle = list[A]
type X = dict[str, Y]
type Y = tuple[Z, int]
type Z = X
# A class refers to itself without expanding.
@dataclass
class Node:
children: list[Node]
let t: Tree = []
let u: UsesCycle = []
print(Node([]))

The compiler reports:

tp0230_recursive.tpn:2:6: error TP0230: type alias 'Tree' refers to itself
help: use a class for a recursive type
tp0230_recursive.tpn:3:6: error TP0230: type alias 'A' refers to itself through 'B'
help: use a class for a recursive type
tp0230_recursive.tpn:6:6: error TP0230: type alias 'X' refers to itself through 'Y' and 'Z'
help: use a class for a recursive type

The specification lists every code.