Skip to content

fix(types): type self-referential tables as recursive types - #84

Merged
wolfy-j merged 2 commits into
mainfrom
fix/typecheck-self-referential-table-v1.7
Nov 14, 2026
Merged

wolfy-j merged 2 commits into
mainfrom
fix/typecheck-self-referential-table-v1.7

Conversation

@wolfy-j

@wolfy-j wolfy-j commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Defect

Type checking never finishes on a table that stores itself:

local nodes = {}
nodes[1] = nodes            -- also table.insert(nodes, nodes), or a[1] = b; b[1] = a

A write into a table reads the written value at the write point. When that value is the table itself, directly or through other tables, the flow solver reads the table's type from the previous iteration and puts it inside the next one: T_(n+1) = widen(T_n, T_n). The chain never reaches a fixpoint. Each step also joins the earlier snapshots, so the type doubles in size every iteration ({...}[] | {...}[][] | {...}[] | {...}[][][][] | ...), and the solver stalls on the growing type long before its iteration cap. On some lines the self read at the write point resolves to the table's still-pending post-write version. The write is then dropped and the table is typed unknown. Preflow symbol inference has the same growth for table.insert(t, t). It runs out of rounds there and widens the table to unknown.

Fix

A table that stores itself gets a recursive type, mu X. widen(T, X), with a recursion identity per table.

  • types/flow: each table that a write's value can hold has one recursion identity per table path and allocation site (the root assignments of the variable that reach the write). A variable rebound to a new table therefore gets a new identity. A hold graph over the solve's indexer and mutator writes decides whether a value can hold the table. It retains exact container and read paths: a sibling scalar such as holder.clock cannot make holder.entries self-referential.
  • types/flow: a value read at an indexer or mutator write that reads the written table itself reads the table's recursion variable. t[k] = t gives mu X. X[] in one step.
  • types/flow: every written type of such a table is bound to its identity, including references held inside other tables' recursive types. Snapshots of one identity in a union join into one snapshot. Binding is by identity only: a table that only looks like the self-referential one stays a separate table. Mutual references converge to a = mu A. B[], b = mu B. A[].
  • types/flow: widening a recursive table widens its body under the same identity. This covers indexer writes, including nil writes, and array and map-of-array mutators. Before, the write was dropped. A write through a self reference (t[1][k] = v after t[1] = t) is a write into the table itself.
  • types/typ: BindRecursiveSnapshotThrough binds an identity through nested recursive types. JoinSnapshotUnions joins same-identity snapshots, reusing JoinRecursiveSnapshots for record bodies.
  • compiler/check/flowbuild/assign: preflow inference folds a table mutator's self-embedding with the rule joinInferredType already applies to assignments.
  • compiler/check/synth/ops: MayHaveLength checks a recursive type's body, so #t is accepted on a recursive table. This also covers declared recursive aliases such as type L = {L}.

Tests

compiler/check/tests/regression/self_referential_table_test.go. Each case runs under a deadline and must not produce a non-convergence warning:

  • nodes[1] = nodes gives mu X. X[], and chained reads nodes[1][1][1] type-check
  • nodes[key] = nodes with a string key gives mu X. {[string]: X}
  • table.insert(nodes, nodes) gives mu X. X[]
  • mutual references a[1] = b; b[1] = a: two identities, each an array of the other
  • a table holding a function that returns the table terminates, and the table is an array of functions
  • the self-referential table built inside a function and returned in a record
  • assigning the self-referential table to number is still an error
  • a structurally equal nested table is not merged into the self-referential table
  • a rebound variable's new table keeps a separate identity
  • a nil write makes entries of the recursive table optional
  • a write through a self reference reaches the table

A nested-map regression covers reading, evicting and replacing cache entries through an alias to a field. Unit tests distinguish sibling paths, parent retention, different bindings, indirect references and unrelated cycles. The nested-map case passes on v1.7.1 and on this change.

Also new: unit tests for BindRecursiveSnapshotThrough, JoinSnapshotUnions, and MayHaveLength on recursive types. On the base commit, the insert and mutual cases do not finish, and the constant-key and dynamic-key cases give unknown and a non-recursive type.

go test ./..., go vet, and golangci-lint (errcheck, govet, staticcheck, unused, ineffassign) pass on the touched packages.

Backport of #83 to the v1.7 release line.

A write whose value holds the written table, directly (t[k] = t,
table.insert(t, t)) or through other tables (a[1] = b; b[1] = a), read
the table's previous type into its next one, so the flow solver and
preflow inference widened the type without bound and never finished.
Such tables now get one recursion identity per table path and
allocation site, the written value reads the table as its recursion
variable, and written types bind to that identity.
@wolfy-j
wolfy-j requested a review from skhaz November 7, 2026 20:03
@wolfy-j wolfy-j changed the title fix(types): type self-referential tables as recursive types (backport #83) fix(types): type self-referential tables as recursive types Nov 10, 2026
@wolfy-j
wolfy-j changed the base branch from release/debug-coverage-v1.7.0 to main November 10, 2026 02:03
@wolfy-j
wolfy-j merged commit f8366a9 into main Nov 14, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants