Repository navigation
fix(types): type self-referential tables as recursive types - #84
Merged
Merged
Conversation
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.
skhaz
approved these changes
Nov 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Defect
Type checking never finishes on a table that stores itself:
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 typedunknown. Preflow symbol inference has the same growth fortable.insert(t, t). It runs out of rounds there and widens the table tounknown.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 asholder.clockcannot makeholder.entriesself-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] = tgivesmu 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 toa = 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] = vaftert[1] = t) is a write into the table itself.types/typ:BindRecursiveSnapshotThroughbinds an identity through nested recursive types.JoinSnapshotUnionsjoins same-identity snapshots, reusingJoinRecursiveSnapshotsfor record bodies.compiler/check/flowbuild/assign: preflow inference folds a table mutator's self-embedding with the rulejoinInferredTypealready applies to assignments.compiler/check/synth/ops:MayHaveLengthchecks a recursive type's body, so#tis accepted on a recursive table. This also covers declared recursive aliases such astype 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] = nodesgivesmu X. X[], and chained readsnodes[1][1][1]type-checknodes[key] = nodeswith a string key givesmu X. {[string]: X}table.insert(nodes, nodes)givesmu X. X[]a[1] = b; b[1] = a: two identities, each an array of the othernumberis still an errorA 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, andMayHaveLengthon recursive types. On the base commit, the insert and mutual cases do not finish, and the constant-key and dynamic-key cases giveunknownand a non-recursive type.go test ./...,go vet, andgolangci-lint(errcheck, govet, staticcheck, unused, ineffassign) pass on the touched packages.