Skip to content

Commit a354916

Browse files
committed
Share awaited and promised types with callers that report errors
Callers passing an error node computed awaited and promised types in their own context, so their errors belong to their node, but this also meant they never reused a cached result. Awaiting in async functions passes an error node, so most awaited types were computed again at every use. A promised type reports errors only when there is none, so a result is shared with every caller. An awaited type can report errors alongside a result, for a union some of whose members cannot be awaited, so only a result whose computation reported nothing is cached. Callers with an error node then reuse it, and compute in their own context only when there is no such result. PERF-PLACEHOLDER
1 parent bd108f5 commit a354916

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ type Checker struct {
615615
compareSymbols func(*ast.Symbol, *ast.Symbol) int
616616
compareSymbolChains func([]*ast.Symbol, []*ast.Symbol) int
617617
globalIDs map[globalKey]globalID
618+
awaitedTypeErrors uint32 // Errors computing awaited types have reported or would report; see getAwaitedTypeNoAliasEx
618619
resolutionSerials uint32
619620
resolutions []resolution
620621
instantiationStack []*Type
@@ -31855,14 +31856,18 @@ func (c *Checker) getPromisedTypeOfPromiseEx(t *Type, errorNode *ast.Node, thisT
3185531856
if IsTypeAny(t) {
3185631857
return nil
3185731858
}
31858-
if errorNode != nil {
31859-
// Errors belong to errorNode; see getIterationTypesOfIterable.
31860-
return c.getPromisedTypeOfPromiseWorker(t, errorNode, thisTypeForErrorOut)
31861-
}
3186231859
key := CachedTypeKey{kind: CachedTypeKindPromisedTypeOfPromise, typeId: t.id}
3186331860
if cached, _ := c.cachedTypes.get(c, key); cached != nil {
3186431861
return cached
3186531862
}
31863+
if errorNode != nil {
31864+
// Errors belong to errorNode; see getIterationTypesOfIterable. Only
31865+
// a missing result reports any, so a result is shared all the same.
31866+
if result := c.getPromisedTypeOfPromiseEx(t, nil /*errorNode*/, thisTypeForErrorOut); result != nil {
31867+
return result
31868+
}
31869+
return c.getPromisedTypeOfPromiseWorker(t, errorNode, thisTypeForErrorOut)
31870+
}
3186631871
c.cachedTypes.begin(c, key)
3186731872
defer c.endMemo()
3186831873
result := c.getPromisedTypeOfPromiseWorker(t, nil /*errorNode*/, thisTypeForErrorOut)
@@ -34231,19 +34236,27 @@ func (c *Checker) getAwaitedTypeNoAliasEx(t *Type, errorNode *ast.Node, diagnost
3423134236
if c.isAwaitedTypeInstantiation(t) {
3423234237
return t
3423334238
}
34234-
if errorNode != nil {
34235-
// Errors belong to errorNode; see getIterationTypesOfIterable.
34236-
return c.getAwaitedTypeNoAliasWorker(t, errorNode, diagnosticMessage, args...)
34237-
}
3423834239
// If we've already cached an awaited type, return a possible `Awaited<T>` for it.
3423934240
key := CachedTypeKey{kind: CachedTypeKindAwaitedType, typeId: t.id}
3424034241
if awaitedType, _ := c.cachedTypes.get(c, key); awaitedType != nil {
3424134242
return awaitedType
3424234243
}
34244+
if errorNode != nil {
34245+
// Errors belong to errorNode; see getIterationTypesOfIterable. Only a
34246+
// result computing which reports nothing is cached, so it is shared
34247+
// all the same.
34248+
if c.getAwaitedTypeNoAliasEx(t, nil /*errorNode*/, nil /*diagnosticMessage*/) != nil {
34249+
if awaitedType, _ := c.cachedTypes.get(c, key); awaitedType != nil {
34250+
return awaitedType
34251+
}
34252+
}
34253+
return c.getAwaitedTypeNoAliasWorker(t, errorNode, diagnosticMessage, args...)
34254+
}
3424334255
c.cachedTypes.begin(c, key)
3424434256
defer c.endMemo()
34257+
errors := c.awaitedTypeErrors
3424534258
awaitedType := c.getAwaitedTypeNoAliasWorker(t, nil /*errorNode*/, nil /*diagnosticMessage*/)
34246-
if awaitedType != nil {
34259+
if awaitedType != nil && c.awaitedTypeErrors == errors {
3424734260
c.cachedTypes.set(c, key, awaitedType)
3424834261
}
3424934262
return awaitedType
@@ -34253,6 +34266,7 @@ func (c *Checker) getAwaitedTypeNoAliasWorker(t *Type, errorNode *ast.Node, diag
3425334266
// For a union, get a union of the awaited types of each constituent.
3425434267
if t.flags&TypeFlagsUnion != 0 {
3425534268
if c.isAwaitingType(t) {
34269+
c.awaitedTypeErrors++
3425634270
if errorNode != nil {
3425734271
c.error(errorNode, diagnostics.Type_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method)
3425834272
}
@@ -34304,6 +34318,7 @@ func (c *Checker) getAwaitedTypeNoAliasWorker(t *Type, errorNode *ast.Node, diag
3430434318
// onrejected: (error: any) => any): BadPromiseA;
3430534319
// }
3430634320
//
34321+
c.awaitedTypeErrors++
3430734322
if errorNode != nil {
3430834323
c.error(errorNode, diagnostics.Type_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method)
3430934324
}
@@ -34332,6 +34347,7 @@ func (c *Checker) getAwaitedTypeNoAliasWorker(t *Type, errorNode *ast.Node, diag
3433234347
// function, they would need to wrap it in some other value. If they want it to
3433334348
// be treated as a promise, they can cast to <any>.
3433434349
if c.isThenableType(t) {
34350+
c.awaitedTypeErrors++
3433534351
if errorNode != nil {
3433634352
var diagnostic *ast.Diagnostic
3433734353
if thisTypeForError != nil {

0 commit comments

Comments
 (0)