Skip to content

Substitute call arguments into emit_unfold let type annotations - #2288

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
EpsilonPhoenix:fix/unfold-subst-callee-types
Nov 24, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
EpsilonPhoenix:fix/unfold-subst-callee-types

Conversation

@EpsilonPhoenix

Copy link
Copy Markdown
Contributor

User-visible failure

Pure, safe Bend programs (passing bend --verdict) can crash with SIGSEGV (bend: memory fault) when compiled to native C if a function with an erased type parameter (e.g. def caller(-A: Type, ...)) calls a flat polymorphic function whose erased type parameter shares the same name and index (e.g. def dup(-A: Data, b: Bool, +x: A)).

Even without a colliding caller parameter name, unfolding a polymorphic flat function leaks raw callee type variables into caller Ann nodes, causing facts_hot to fall back to fact_add("open") and degrading unboxed layouts.

Root cause

In bend2/comp.ts, emit_unfold previously annotated unfolded let-bound arguments using tele_unbind(m.tld!.T).doms:

const doms = tele_unbind(m.tld!.T).doms;
const lets = fs.map((a, i) => !m.live[i] || term_const(a)
  || term_strip(a).$ === "Var" ? null
  : { p: probe("a"), v: Bend.Ann(a, doms[i][2]) });

tele_unbind leaves the callee's telescope variables unsubstituted (e.g. Var("A", 0)). When the caller binds the resulting let and later shares it (bind_pop), facts_hot(sc.def, b.A, true) looks up Var("A", 0) in the caller's telescope (tele_unbind(tld_of(def).T).doms[w.i]). If the caller also has an erased -A at index 0 (e.g. Unit), facts_hot marks the caller's -A argument hot instead of the actual instantiated type (Node):

  • Node's constructors (Leaf, Bin) remain cold (CID_T[CID_Bin].hot == 0), skipping rfc_seal on constructor children and emitting term_loc(...) instead of ctr_take(...) when matching.
  • When the unfolded body shares the bound Node (term_keep), it becomes RFC-wrapped, so subsequent matching reads the RFC redirect word + adjacent heap memory as constructor fields and faults (SIGSEGV).

Minimal change

  • bend2/comp.ts (emit_unfold): walk m.tld!.T step-by-step via ty_all(T) and substitute each folded argument T = f.B(a) before annotating Bend.Ann(a, f.A).
  • tests/reg/unfold_poly_hot.bend: regression test (passing --verdict) covering colliding caller -A at index 0, forwarding a caller's own erased type parameter at index 1 with a non-Var expression, and a dependent parameter type instantiated by a folded argument.

Before / after evidence

On tests/reg/unfold_poly_hot.bend (main at 966bbb74):

  • Before fix:
    • Generated C has CID_Unit hot ({ 0, 1, 0 }), CID_Leaf/CID_Bin cold ({ 1, 0, 0 }, { 2, 0, 0 }), and unsealed Bin children.
    • Native C binary crashes with bend: memory fault (machine stack overflow?) / Segmentation fault (core dumped) (exit code 139).
  • After fix:
    • Generated C has CID_Unit cold ({ 0, 0, 0 }), CID_Leaf/CID_Bin hot ({ 1, 1, 0 }, { 2, 1, 0 }), and rfc_seal on Bin children.
    • bun bend2/main.ts tests/reg/unfold_poly_hot.bend --verdict -> ALL PROOFS CHECK.
    • Bun JS, Node JS, native C (-O3 and -O3 -DBEND_POISON, --threads 1 and --threads 4) all output 94 (exit 0).

Validation & limits

  • Verified on Linux x86_64 (bun 1.3.12, node v24.14.1, clang 23.1.5, Lean v4.34.0):
    • bun gates/repo.ts: PASS: 56 / 56 (bend2/comp.ts: 65,989 / 66,000 ttok, +9 tokens).
    • tests/reg/unfold_poly_hot.bend: --verdict, JS (Bun & Node), C (-O3 and -DBEND_POISON, --threads 1 & --threads 4).
    • tests/reg/*.bend and tests/rfc/*.bend sweep (JS + native C): 126 passed, 0 failed (7 skipped for platform/foreign requirements).
  • Not verified: macOS / Mac-mini cluster (gates/test.ts, gates/perf.ts), Metal, and CUDA device lanes.

nicolas-abril added a commit to EpsilonPhoenix/bend that referenced this pull request Nov 24, 2026
The spin call stays a braced block and its Term o is declared before the
arguments' aliases, as on main, so a spin's text keeps its length and
SPIN_FAR picks the same INLINE or FAR it did. Only the spins that lose their
h/q cache now differ in the generated C.

The tokens that costs, and 9 more for bendlang#2288 to land beside this, come from
merged declarations and one braceless early return in nearby TypeScript,
none of which changes output.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril
nicolas-abril merged commit 3d5bf43 into bendlang:main Nov 24, 2026
1 check passed
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Nov 24, 2026
, bendlang#2224-bendlang#2226, bendlang#2231, bendlang#2285, bendlang#2286, bendlang#2288 and others)

One conflict, in emit_unfold: bendlang#2288 annotates an unfolded call's lets with
the argument types instantiated by the earlier arguments; bendlang#2162's deep unfold
keeps its arguments unannotated there and annotates them the same way after
folding (its type walk renamed D, beside bendlang#2288's T). Both kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@EpsilonPhoenix
EpsilonPhoenix deleted the fix/unfold-subst-callee-types branch November 24, 2026 18:47
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