[WIP] Fix issue 63192 in TypeScript repository#2907
Open
Conversation
…les (#63192) Add a reentrancy guard in getInitialTypeOfVariableDeclaration to detect when flow analysis circularly computes the initial type of the same variable declaration. This occurs when destructuring a loop variable with default values causes flow analysis to re-evaluate the destructuring source expression, which in turn needs the destructured bindings. When circularity is detected, return errorType to break the cycle, which allows the existing type resolution circularity mechanism (pushTypeResolution/popTypeResolution) to report the appropriate "referenced directly or indirectly in its own initializer" error. Co-authored-by: RyanCavanaugh <[email protected]>
Co-authored-by: RyanCavanaugh <[email protected]>
Add circularity guards for two distinct cycle paths triggered by destructuring loop variables with default values (TypeScript#63192): 1. getInitialTypeOfVariableDeclaration: Guards against a cycle where flow analysis for a variable evaluates the initializer of a const variable declaration, which in turn needs the type of a binding element whose parent type depends on the destructuring source, which triggers flow analysis again. 2. tryGetNameFromEntityNameExpression: Guards against a cycle where isMatchingReference needs the type of a binding element to compute a property name for an element access expression (e.g. children[index]), which triggers parent type inference, which evaluates the initializer, which triggers flow analysis again. Both guards use NodeCheckFlagsResolvingInitialType, a reentrancy flag on the AST node. This flag is needed (rather than reusing the existing pushTypeResolution mechanism) because the cycle occurs entirely within flow analysis paths that bypass the normal getTypeOfSymbol entry point. In the `let x = x` case, circularity is caught by pushTypeResolution in getTypeOfVariableOrParameterOrPropertyWorker because `x` has no type annotation and its type must be inferred, causing the cycle to re-enter symbol type resolution. In the destructuring loop case, the destructuring source variable (`node`) has an explicit type annotation, so getTypeOfSymbol returns immediately without entering type resolution. The cycle is entirely within flow analysis, never touching pushTypeResolution. Co-authored-by: RyanCavanaugh <[email protected]>
Co-authored-by: RyanCavanaugh <[email protected]>
Member
|
@ahejlsberg the code comments here are a bit much but does this look like a correct CFA fix? |
Member
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.
NodeCheckFlagsResolvingInitialTypereentrancy guard ingetInitialTypeOfVariableDeclaration(cycle path 1)NodeCheckFlagsResolvingInitialTypereentrancy guard intryGetNameFromEntityNameExpression(cycle path 2)💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.