Repository navigation
A generic function's instance calls itself with its declared return type - #835
Merged
Merged
Conversation
`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>
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.
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".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