fix(0139): reject by-value self-referential types loudly (was a segfault)

A nominal aggregate that contains itself (or a mutual peer) BY VALUE has no
finite layout and infinite-recursed typeSizeBytes into a stack overflow —
for SOURCE enums/structs as well as comptime-constructed types.

New `checkInfiniteSize` pass (lower/decl.zig, Pass 1g — after type
registration, before body lowering): walks the by-VALUE containment graph
(pointer/slice/optional payloads break the cycle, so `*Self` stays valid);
on a back-edge it emits a loud diagnostic — "type 'X' is infinitely sized
(it contains itself by value); use a pointer ('*X') to break the cycle" —
and poisons the offending field to `.unresolved` so sizing can't recurse
before the build halts on the error. Covers source + declare/define types,
direct + mutual recursion.

examples/1178 locks the diagnostic; issue 0139 marked RESOLVED. This also
completes METATYPE PLAN F5's by-value-self-reference rejection. Full suite
green (675).
This commit is contained in:
agra
2026-06-16 22:24:31 +03:00
parent f845fc6413
commit 2f0905b407
7 changed files with 138 additions and 0 deletions

View File

@@ -1,5 +1,17 @@
# 0139 — by-value self-referential type segfaults (`typeSizeBytes` infinite recursion)
> **RESOLVED.** Root cause: `typeSizeBytes` (and the layout path) recursed into
> each by-value aggregate field with no cycle guard, so a by-value self/mutual
> reference looped to a stack overflow. Fix: a new `checkInfiniteSize` pass
> (`src/ir/lower/decl.zig`, Pass 1g — after type registration, before body
> lowering) walks the by-VALUE containment graph; on a back-edge it emits a loud
> diagnostic (`type 'X' is infinitely sized (it contains itself by value); use a
> pointer ('*X') to break the cycle`) and poisons the offending field to
> `.unresolved`, breaking the recursion before any sizing runs. A pointer / slice
> / optional payload breaks the cycle, so `*Self` recursion stays valid. Covers
> both source decls and comptime-constructed (`declare`/`define`) types.
> Regression test: `examples/1178-diagnostics-infinite-size-self-reference.sx`.
**Symptom** — a type whose field/variant payload is ITSELF *by value* (not behind
a pointer) crashes the compiler with a stack-overflow segfault instead of a loud
"infinite size" diagnostic. Observed: `Segmentation fault` inside