Fix stack overflow in error generation for recursive mapped types#63215
Open
hamzax180 wants to merge 1 commit intomicrosoft:mainfrom
Open
Fix stack overflow in error generation for recursive mapped types#63215hamzax180 wants to merge 1 commit intomicrosoft:mainfrom
hamzax180 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Author
|
@microsoft-github-policy-service agree |
5dfda97 to
ac5ee61
Compare
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.
Fixes #63090
Bug Description
When a mapped type contains a deeply recursive or circular reference, TypeScript correctly identifies it as an error. However, when generating the diagnostic message (
Type_of_property_0_circularly_references_itself_in_mapped_type_1), the compiler calls typeToString(mappedType) to format the error text.Evaluating typeToString(mappedType) triggers property resolution, which evaluates the symbol's type by calling getTypeOfMappedSymbol again. Since
symbol.links.typehasn't been cached with theerrorTypeyet, this hits the same circularity, which attempts to throw another error, triggering typeToString again, leading to an infinite cycle and an eventualRangeError: Maximum call stack size exceeded.Fix Explanation
This PR breaks the infinite recursion by eagerly caching
symbol.links.type = errorType;immediately before calling error(...) and typeToString(mappedType).When typeToString evaluates the mapped type, it now sees the cached
errorTypefor the symbol and safely halts recursion, generating the correct error message without blowing up the call stack.Testing
Added the provided reproduction from the issue as a compiler test case:
tests/cases/compiler/issue63090.tshereby baseline-acceptso the baseline error generation accurately outputs the circularity error instead of crashing.