Skip to content

A generic function's instance calls itself with its declared return type - #835

Merged
ASDAlexander77 merged 2 commits into
mainfrom
fix-generic-self-recursion
Nov 13, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
fix-generic-self-recursion

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

A generic function's instance that calls itself (countDown<string> from its own body) failed with "not all generic types can be identified" whenever the call's result was used. The instance's type went into the function type map only after its return type was discovered, and discovery needs the self-call's type first.

  • mlirGenFunctionPrototype: an instance with a declared return type is registered under its full name before discovery, so a self-call (direct, f<T>(...), a generic method, or mutual recursion) finds its type.
  • instantiateSpecializedFunction: an instance with no declared return type that calls itself now gets a clear error: "'' calls itself, and its return type is found from its body: declare its return type".
  • Tests: 00generic_self_recursion.ts (compile/jit, plus own), generic_self_recursion_untyped_error.ts (expected error, added to the verify-ownership skip list).

Testing: full ctest on Windows and Linux. The only failure was the ownership-verifier shard on the new error-by-design file, which is now on the skip list (8/8 verifier shards pass). DefaultLib tests.ps1 passes for gc, rc, none and own.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 13, 2026 00:37
`function countDown<T>(value: T, n: number): number { ... return countDown(value, n - 1) + 1; }`
failed with "can't instantiate function. 'countDown' not all generic types can be identified":
an instance's type went into the function type map only after its body was discovered, and the
body's call of itself found no type, so its result had nothing to add to. A call whose result was
not used compiled. A function at the top level is registered by the pass over the module's
declarations before its body is discovered; an instance is made only when it is called.

Now an instance whose return type is declared is registered with it before its body is
discovered. One whose return type is found from the body still can't call itself (TypeScript
gives such a function `any`); that now says "'countUp' calls itself, and its return type is found
from its body: declare its return type".

Tests: 00generic_self_recursion.ts (a call of itself, with a type argument, a generic method, two
calling each other, returning T and T[]; gc, own, rc, none, compile and JIT) and
generic_self_recursion_untyped_error.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… by design

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 6aa5963 into main Nov 13, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-generic-self-recursion branch November 13, 2026 01:36
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.

1 participant