Skip to content

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

Closed
wolfy-j wants to merge 35 commits into
mainfrom
fix/typecheck-self-referential-table
Closed

wolfy-j wants to merge 35 commits into
mainfrom
fix/typecheck-self-referential-table

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.
  • 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

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.

xepozz and others added 30 commits August 3, 2026 22:02
Adds line and count debug hooks, the primitive test-coverage tooling
(LuaCov-style, LCOV output) is built on. debug.sethook/gethook were already
declared in the type stub (compiler/stdlib/debug.go) but had no runtime
implementation, so any coverage instrumentation was impossible.

- hook.go: sethook/gethook + the callHook/fireHook engine. All state lives on
  LState (hook, hookMask, hookCount, hookCounter, hookLastLine, inHook) — the
  hot callFrame struct is left untouched, so there is no per-frame size cost on
  the call path.
- vm.go: one guarded call at the dispatch-loop head; when no hook is installed
  it is a single uint8 compare. Measured (benchstat, n=10, paired vs main):
  every VM benchmark within noise, geomean -0.10% — no measurable overhead.
- Line events fire when the current source line changes (tracked on LState).
  This is a coverage-oriented simplification of PUC's per-frame oldpc tracking:
  every executed line is reported at least once; it does not re-fire a line on
  a same-frame loop back-edge. Happy to switch to full per-frame semantics if
  preferred.
- Reentrancy is blocked via LState.inHook so a hook cannot trigger itself; the
  register top is saved/restored around the call.
- Call/return ('c'/'r') masks are rejected explicitly rather than silently
  ignored — line/count only for now.

hook_test.go covers coverage collection, count firing, gethook round-trip,
nested calls, reentrancy, sethook(nil) clearing, and c/r rejection.
Full suite (2199 tests) green, go vet clean. No generated files touched.
Coverage is a consumer of the debug hook (this branch is based on the
sethook/gethook branch), not a separate instrumentation path:

- WIPPY_COVERAGE arms every LState's line hook via covArm (hookMask |=
  HookMaskLine) in newLState — no debug.sethook call needed, no separate
  dispatch-loop guard;
- the hook's line-event path in callHook records each executing source line
  (numerator), reusing the hook's hookLastLine dedup;
- CompileWithOptions registers every prototype's coverable lines (walking nested
  FunctionPrototypes' DbgSourcePositions) = exact denominator incl. never-run
  functions;
- WriteCoverageLCOV / CoverageSummary emit a standard LCOV tracefile.

No-op when WIPPY_COVERAGE is unset (covArm and covRegisterProto both bail).
Full suite green, go vet clean; smoke confirms covArm arms the hook and hits are
recorded with no explicit sethook.
* fix(flow): preserve optional root structure when composing field facts

* fix(types): preserve declared record domains during inference widening

* fix(flow): share declared slot projection across field facts and effects

* fix(check): preserve declared alias bounds in captured tables

* fix(flow): retain recursive identity when projecting table facts

* fix(check): narrow asserted member reads from the declared type

* fix(narrow): filter union members inside optional aliases

* fix(types): compare record presence separately from value domains

* fix(check): finalize contextual synthesis at the narrowing boundary

* fix(flow): retain predicate evidence without a projected member

* test(check): pin gate assertion and strict-any classifications

* fix(types): use element evidence when widening open records to maps

* fix(check): preserve capture environments through callback synthesis

* fix(check): seed parameter expectation inference with caller evidence

* fix(subtype): compare record presence by semantic nil admission

* fix(subtype): widen complete empty literals to collection slots

* fix(flow): keep pending scalar phi evidence out of predicate fallback

* fix(returns): retain literal cases across callable estimates

* fix(flow): invalidate asserted member predicates through writes

* fix(check): contextually type logical alternatives without widening aliases

* fix(types): unify record map conversion around key evidence

* fix(flow): distinguish pending values from missing member projections

* fix(check): type captured parameters at their flow capture point

* test(types): cover semantic nil admission across record field forms

* fix(check): compare captured alias bounds by semantic identity

* style(check): use a tagged switch for logical guards

* fix(check): retain declared map domains through field refinements

* fix(check): preserve Boolean literal values in contextual fields

* fix(check): retain completeness in all table literal synthesis

* fix(check): carry contextual empty collection initializers into flow

* fix(stdlib): describe table.create as a complete empty table

* fix(check): reuse index key domains for annotated map writes

* fix(subtype): preserve intersections when lifting into optional domains

* Revert "fix(check): reuse index key domains for annotated map writes"

This reverts commit 1999e5b.

* Revert "fix(stdlib): describe table.create as a complete empty table"

This reverts commit 9c91e1c.

* Revert "fix(check): retain declared map domains through field refinements"

This reverts commit 7d84675.

* Revert "fix(check): retain completeness in all table literal synthesis"

This reverts commit 4ca22be.

* fix(types): preserve interim v1.6.2 record map semantics

* Revert "fix(subtype): preserve intersections when lifting into optional domains"

This reverts commit 383a4c4.

* fix(check): preserve shared logical table alternatives before joining

* fix(check): match literal discriminants through optional union context

* fix(check): limit empty initializer flow context to array domains

* fix(types): preserve optional targets during mutable field widening

* fix(check): keep supplied captures authoritative over collected writes

* fix(types): pin legacy optional alias normalization for map conversion

* fix(check): preserve interim map views in fresh record fields

* fix(check): retain literal fields across alternative union contexts

* test(check): strengthen independent contextual typing evidence

* fix(check): preserve collection evidence over vacant return estimates

* fix(check): contextualize fresh instance slots from declared method writes

* fix(check): fold recursive data estimates through existing record widening

* fix(check): validate returns only at reachable flow points

* fix(subtype): retain present alias identity in record slots

* test(check): prove independent logical record return context fix

* fix(flow): expire ascribed child write evidence across alias calls

* Revert "fix(flow): expire ascribed child write evidence across alias calls"

This reverts commit 3b7e47b.

* fix(flow): narrow assertions only with current predicates

* Revert "fix(check): contextualize fresh instance slots from declared method writes"

This reverts commit 12b27f5.

* Revert "fix(check): validate returns only at reachable flow points"

This reverts commit 1cec453.

* Revert "fix(check): retain literal fields across alternative union contexts"

This reverts commit 63bd117.

* Revert "test(check): prove independent logical record return context fix"

This reverts commit 0fe3c06.

* Revert "fix(check): preserve interim map views in fresh record fields"

This reverts commit 1dfb225.

* Revert "fix(types): pin legacy optional alias normalization for map conversion"

This reverts commit 1e641e9.

* Revert "fix(check): keep supplied captures authoritative over collected writes"

This reverts commit 2f1f521.

* Revert "fix(types): preserve optional targets during mutable field widening"

This reverts commit cc46268.

* Revert "fix(check): match literal discriminants through optional union context"

This reverts commit 45c7952.

* Revert "fix(check): preserve shared logical table alternatives before joining"

This reverts commit 6f79ca0.

* Revert "Revert "fix(types): preserve optional targets during mutable field widening""

This reverts commit 17a51fa.

* Revert "Revert "fix(check): preserve shared logical table alternatives before joining""

This reverts commit ecf0297.

* Revert "Revert "test(check): prove independent logical record return context fix""

This reverts commit d1812f6.

* Revert "Revert "fix(check): preserve interim map views in fresh record fields""

This reverts commit f6e1df3.

* Revert "Revert "fix(check): keep supplied captures authoritative over collected writes""

This reverts commit f02c203.

* Revert "Revert "fix(check): contextualize fresh instance slots from declared method writes""

This reverts commit 4272bea.

* Revert "Revert "Revert "test(check): prove independent logical record return context fix"""

This reverts commit 57ec44a.

* Revert "Revert "Revert "fix(check): preserve shared logical table alternatives before joining"""

This reverts commit 46e55ab.

* Revert "Revert "Revert "fix(check): keep supplied captures authoritative over collected writes"""

This reverts commit 22c0ff7.

* test(check): retain nullable capture rejection after follow-up rollback

* Revert "Revert "fix(check): validate returns only at reachable flow points""

This reverts commit 03a94d5.

* Revert "Revert "fix(check): match literal discriminants through optional union context""

This reverts commit 46d27d4.

* Revert "Revert "fix(check): retain literal fields across alternative union contexts""

This reverts commit de0a9c7.

* fix(check): retain optional union selection without logical context extension

* fix(check): filter field writes before synthesizing captures

Field-write collection synthesized excluded targets before filtering its
results. This re-entered captured builder methods and made their callable
return facts alternate between one recursive snapshot and a same-identity
snapshot union. Apply the symbol filter before synthesis to break that
unrequested dependency.

Add a reduced execution_identity builder regression asserting convergence
and zero diagnostics in both gradual and strict modes. The failing history
transition is f02c203, restored at dc034dc in the release candidate.

Verified unchanged kickside diagnostics apart from the convergence warning,
build, vet, gofmt, golangci-lint, all Go packages (root with a 60m timeout),
runtime Lua tests, 17 Bee repros in both modes, and the zero-diagnostic Bee
gate. Runtime's websocket leak test passed on rerun after a goroutine-count
failure in the first run.

* test: add Bee false-positive regression fixtures
* fix(vm): close upvalues at the protected call boundary

A caught error closed the open upvalues of every frame, including the
frames that continue after pcall, xpcall, or PCall returns. Closures
created before the error then lost writes to locals of live functions.

Close upvalues only from the protected call base when the error is
caught, and close all upvalues of a coroutine that dies with an error.
PCall with an error handler now also closes the upvalues of the
unwound frames.

* perf(baselib): return preallocated type names from type()

type() wrapped the type name in a new LString on each call, and the
interface conversion allocated. Return values from a table built once.
2000 type() calls drop from 2003 to 3 allocations and run 16% faster.

* test: cover xpcall handlers, wrapped coroutines, Go errors and type names
* fix(coroutine): include Go stack in recovered panics

* feat(panic): report recovered coroutine panics

* fix(panic): contain reporter failures
* test(compiler): cover goto capture close boundaries

* fix(compiler): close goto captures at the resolved label boundary
…69)

* test(vm): cover nested break and suspended-state capture lifetimes

* fix(vm): close departed and suspended captures before register reuse

* test(vm): use explicit context in suspended-state regression
…#65)

* test: reproduce the array growth of a table on one large integer key

RawSetInt puts every key from 1 to MaxArrayIndex - 1 in the array part and
fills the gap with nil. One assignment t[40013095] = true makes an array of
40,013,095 slots (753 MB). The tests fail on this behaviour.

* fix: keep a sparse integer key in the hash part of a table

RawSetInt and RawSet filled the array part with nil up to any integer key
below MaxArrayIndex. One assignment t[40013095] = true made an array of
40,013,095 slots (753 MB). A key that is far past the end of the array now
goes to the hash part, the same as in Lua: the array part grows only while
it stays at least half full. When the array reaches a hashed key, the key
moves into the array, so the length and ipairs see one sequence. A read
past the end of the array looks in the hash part.

* test(table): cover sparse storage, iteration, promotion and retained values

* fix(table): bound sparse growth and preserve iteration and maximum keys

* test(table): keep promotion order cases lint-clean

* fix(table): preserve hash cursors and logical sequence boundaries

* fix(table): preserve empty numeric storage shape and release deleted keys

---------

Co-authored-by: Wolfy-J <wolfy.jd@gmail.com>
fix(table): keep next/pairs traversal valid when the current key is cleared
…e-main

fix(vm): handle native function keys and repeated close
fix(types): traverse shared graphs once during lint analysis
* fix(types): traverse deep graphs without recursive Go calls

* test(types): infer the deep graph variable type
fix(lua): unify numeric keys and correct integer arithmetic semantics
Co-authored-by: KickSide <dev@kickside.ai>
Signed-off-by: Wolfy-J <wolfy.jd@gmail.com>
…#80)

string.format handed raw Lua values to Go's fmt.Sprintf, so a conversion
never converted its argument: string.format("%.1f", 0) produced
"%!f(lua.LInteger=0)", %d of 3.0 printed a float, %x of -1 printed "-1",
%q used Go quoting, inf/nan printed Go spellings, %s widths counted runes,
missing arguments printed %!v(MISSING) and invalid specifiers passed through.

Each specification is now validated as lstrlib's checkformat does (per
conversion flags, two-digit width and precision) and its argument is
converted the way the reference implementation converts it:
luaL_checkinteger for c/d/i/u/o/x/X, luaL_checknumber for a/e/f/g, tostring
for s, and addliteral for q. Errors match Lua 5.4: no value, number has no
integer representation, invalid conversion, specifier '%q' cannot have
modifiers, value has no literal form.

Expectations come from the Lua 5.4 reference interpreter; a randomized
comparison of 2,895 specifier/value pairs against lua5.4 matches except %s
of floats, where %s is tostring and tostring's float spelling differs.
wolfy-j and others added 4 commits October 31, 2026 16:39
feat(debug): implement debug.sethook/gethook (line + count events)
feat(coverage): Go-side line coverage built on the debug hook (LCOV)
…te is recycled (#81)

* fix(vm): discard yield continuation when its frame unwinds or the state is recycled

* test(vm): exercise both stack implementations with isolated pools

---------

Co-authored-by: KickSide <dev@kickside.ai>
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.

The value of such a write now reads the table as its recursion
variable, and nested approximations of the table's previous type fold
into one recursion identity per table path, giving mu X. widen(T, X).
Widening a recursive table widens its body under the same identity,
and the length operator accepts recursive table types.
@wolfy-j
wolfy-j requested a review from skhaz November 7, 2026 19:27
Recursion identities are keyed by table path and allocation site, so a
rebound variable names a new table, and binding is by identity alone:
a structurally equal table is no longer folded into the recursive one.
Bindings reach references nested in other tables' recursive types, and
snapshots of one identity in a union join into one, so mutual
references converge to one recursive type per table. Nil writes and
writes through a self reference widen the recursive table itself.
@wolfy-j

wolfy-j commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #84: main now follows the v1.7 release line.

@wolfy-j wolfy-j closed this Nov 10, 2026
@wolfy-j
wolfy-j deleted the fix/typecheck-self-referential-table branch November 10, 2026 02:04
wolfy-j added a commit that referenced this pull request Nov 14, 2026
* fix(types): type self-referential tables as recursive types

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.

* fix(flow): distinguish sibling table paths when binding recursion
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.

3 participants