Extend letrec pre-declare to ternary-wrapped self/mutual references - #56
Merged
Conversation
The letrec pre-declare (self-reference: v0.8.1, mutual recursion: v0.9.0) only recognized a DIRECT `name = function(...) ...` RHS -- `name = cond ? function(...) ...name(...)... : function(...) ...` still fell through to Op::LoadFree, staying correct but interpreted. Generalizes the eligibility check: collectLetrecCandidateLiterals recursively determines whether an RHS is GUARANTEED to evaluate to a closure (a bare literal, or a ternary -- or nested ternaries -- whose branches all recursively qualify), returning every reachable FunctionLiteral node so each can be independently checked for a self/sibling reference after compiling. A ternary can produce two structurally distinct closures, not just one, so this replaces the single "was the last emitted instruction Op::MakeClosure" check with a proper per-candidate lookup into chunk_.closureSites by node identity. Deliberately still gated on "unambiguously always a closure" -- a mixed or uncertain RHS shape (a call, a non-ternary conditional, etc.) doesn't qualify, keeping `let(x = x + 1)`-style shadowing semantics untouched. Verified: ternary-wrapped self-reference and mutual recursion both resolve correctly and run compiled (200,000-deep tail recursion in ~0.1s). While investigating scale, found and ruled out a pre-existing, unrelated crash: a NON-tail-recursive function (closure or not, letrec or not) grows a real native C++ stack frame per call when compiled -- confirmed present in stock v0.9.0 for a plain top-level FunctionDeclaration with zero closures involved, well outside the scope of this change. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This was referenced Jul 31, 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.
Summary
name = function(...) ...RHS —name = cond ? function(...) ...name(...)... : function(...) ...still fell through toOp::LoadFree, staying correct but permanently interpreted.collectLetrecCandidateLiterals: recursively determines whether an RHS is guaranteed to evaluate to a closure (a bare literal, or a ternary — or nested ternaries — whose branches all recursively qualify), returning every reachableFunctionLiteralnode so each can be independently checked for a self/sibling reference after compiling (a ternary can produce two structurally distinct closures, not just one — this replaces the previous "was the last emitted instructionOp::MakeClosure" check with a proper per-candidate lookup by node identity).let(x = x + 1)-style shadowing semantics untouched.A separate finding, out of scope for this PR
While verifying at scale, I found (and ruled out as unrelated) a pre-existing crash: a non-tail-recursive function — closure or not, letrec or not — grows a real native C++ stack frame per call when compiled, with no depth guard, and segfaults around ~4000 levels deep. Confirmed present in stock v0.9.0 for a plain top-level
function count_to(n) = n<=0?0:1+count_to(n-1);with zero closures involved; the interpreted path (OSCAD_BYTECODE_VM=0) handles the same script correctly via its own recursion guard. Not touched here — flagging for a possible follow-up.Test plan
TernaryWrappedSelfReferenceResolvesAsUpvalue(200,000-deep tail recursion, ~0.1s, proving it runs compiled) andTernaryWrappedMutualRecursionResolvesCorrectly.count_to,f_1arg,reduce(), 2-way/3-way mutual recursion,let(x=x+1)shadowing,accumulate()/while()) unaffected.🤖 Generated with Claude Code