Skip to content

Commit 620f184

Browse files
committed
Make flow analysis of a reference independent of other references
When one reference hit the flow analysis depth limit, flow analysis was disabled for every later reference in the same function or module body, so the type of a reference depended on whether another reference in the body had been analyzed before it. Each reference now gives up only when its own analysis hits the limit. Shared flow node results are cached for the duration of one flow analysis, including incomplete results computed under a loop back edge. Once that loop's back edge has been exited and the loop label has a complete type, they are stale. Reusing them made a reference's type depend on which reference was analyzed first: in exhaustiveSwitchCheckCircularity, querying the reference inside the loop before the switch expression produced `never` instead of "bbb". An incomplete shared result is now reused only while the loop that was innermost when it was computed is still being analyzed.
1 parent 8525b16 commit 620f184

5 files changed

Lines changed: 2016 additions & 2019 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,6 @@ type Checker struct {
845845
flowLoopStack []FlowLoopInfo
846846
sharedFlows []SharedFlow
847847
antecedentTypes []*Type
848-
flowAnalysisDisabled bool
849848
flowInvocationCount int
850849
flowTypeCache map[*ast.Node]*Type
851850
lastFlowNode *ast.FlowNode
@@ -3867,13 +3866,7 @@ func (c *Checker) checkBlock(node *ast.Node) {
38673866
if node.Kind == ast.KindBlock {
38683867
c.checkGrammarStatementInAmbientContext(node)
38693868
}
3870-
if ast.IsFunctionOrModuleBlock(node) {
3871-
saveFlowAnalysisDisabled := c.flowAnalysisDisabled
3872-
c.checkSourceElements(node.Statements())
3873-
c.flowAnalysisDisabled = saveFlowAnalysisDisabled
3874-
} else {
3875-
c.checkSourceElements(node.Statements())
3876-
}
3869+
c.checkSourceElements(node.Statements())
38773870
if node.Locals().Len() != 0 {
38783871
c.registerForUnusedIdentifiersCheck(node)
38793872
}

‎tsc/internal/checker/flow.go‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ func (c *Checker) getFlowTypeOfReference(reference *ast.Node, declaredType *Type
7979
}
8080

8181
func (c *Checker) getFlowTypeOfReferenceEx(reference *ast.Node, declaredType *Type, initialType *Type, flowContainer *ast.Node, flowNode *ast.FlowNode) *Type {
82-
if c.flowAnalysisDisabled {
83-
return c.errorType
84-
}
8582
if flowNode == nil {
8683
flowNode = getFlowNodeOfNode(reference)
8784
if flowNode == nil {
@@ -117,11 +114,11 @@ func (c *Checker) getFlowTypeOfReferenceEx(reference *ast.Node, declaredType *Ty
117114
func (c *Checker) getTypeAtFlowNode(f *FlowState, flow *ast.FlowNode) FlowType {
118115
if f.depth == 2000 {
119116
// We have made 2000 recursive invocations. To avoid overflowing the call stack we report an error
120-
// and disable further control flow analysis in the containing function or module body.
117+
// and give up on this reference. Each reference hits the limit on its own, so its type does not depend
118+
// on which other references were analyzed first.
121119
if tr := c.tracer; tr != nil {
122120
tr.Instant(tracing.PhaseCheckTypes, "getTypeAtFlowNode_DepthLimit", map[string]any{"depth": f.depth})
123121
}
124-
c.flowAnalysisDisabled = true
125122
c.reportFlowControlError(f.reference)
126123
return FlowType{t: c.errorType}
127124
}
@@ -134,9 +131,9 @@ func (c *Checker) getTypeAtFlowNode(f *FlowState, flow *ast.FlowNode) FlowType {
134131
// the same getFlowTypeOfReference invocation. A node is considered shared when it is the
135132
// antecedent of more than one node.
136133
for i := f.sharedFlowStart; i < len(c.sharedFlows); i++ {
137-
if c.sharedFlows[i].flow == flow {
134+
if s := &c.sharedFlows[i]; s.flow == flow {
138135
f.depth--
139-
return c.sharedFlows[i].flowType
136+
return s.flowType
140137
}
141138
}
142139
sharedFlow = flow
@@ -1364,12 +1361,19 @@ func (c *Checker) getTypeAtFlowLoopLabel(f *FlowState, flow *ast.FlowNode) FlowT
13641361
} else {
13651362
// All but the first antecedent are the looping control flow paths that lead
13661363
// back to the loop junction. We track these on the flow loop stack.
1364+
sharedFlowStart := len(c.sharedFlows)
13671365
c.flowLoopStack = append(c.flowLoopStack, FlowLoopInfo{key: key, types: antecedentTypes})
13681366
saveFlowTypeCache := c.flowTypeCache
13691367
c.flowTypeCache = nil
13701368
flowType = c.getTypeAtFlowNode(f, list.Flow)
13711369
c.flowTypeCache = saveFlowTypeCache
13721370
c.flowLoopStack = c.flowLoopStack[:len(c.flowLoopStack)-1]
1371+
// An incomplete type computed under the back edge reflects the loop's
1372+
// partial types, so it is not valid once the back edge is exited.
1373+
// Reusing it would make the result depend on which reference was
1374+
// analyzed first.
1375+
kept := slices.DeleteFunc(c.sharedFlows[sharedFlowStart:], func(s SharedFlow) bool { return s.flowType.incomplete })
1376+
c.sharedFlows = c.sharedFlows[:sharedFlowStart+len(kept)]
13731377
// If we see a value appear in the cache it is a sign that control flow analysis
13741378
// was restarted and completed by checkExpressionCached. We can simply pick up
13751379
// the resulting type and bail out.

‎tsc/testdata/baselines/reference/compiler/exhaustiveSwitchCheckCircularity(checkorder=typesfirst).types‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function f() {
3636
else if (isNever(foo)) { // Error expected
3737
>isNever(foo) : boolean
3838
>isNever : (n: never) => boolean
39-
>foo : never
39+
>foo : "bbb"
4040

4141
break;
4242
}

0 commit comments

Comments
 (0)