Skip to content

Commit 95bf71f

Browse files
committed
Use canonicalized forms when comparing signatures
1 parent 6ae7617 commit 95bf71f

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/compiler/checker.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6632,11 +6632,31 @@ namespace ts {
66326632
}
66336633

66346634
function getErasedSignature(signature: Signature): Signature {
6635-
if (!signature.typeParameters) return signature;
6636-
if (!signature.erasedSignatureCache) {
6637-
signature.erasedSignatureCache = instantiateSignature(signature, createTypeEraser(signature.typeParameters), /*eraseTypeParameters*/ true);
6638-
}
6639-
return signature.erasedSignatureCache;
6635+
return signature.typeParameters ?
6636+
signature.erasedSignatureCache || (signature.erasedSignatureCache = createErasedSignature(signature)) :
6637+
signature;
6638+
}
6639+
6640+
function createErasedSignature(signature: Signature) {
6641+
// Create an instantiation of the signature where all type arguments are the any type.
6642+
return instantiateSignature(signature, createTypeEraser(signature.typeParameters), /*eraseTypeParameters*/ true);
6643+
}
6644+
6645+
function getCanonicalSignature(signature: Signature): Signature {
6646+
return signature.typeParameters ?
6647+
signature.canonicalSignatureCache || (signature.canonicalSignatureCache = createCanonicalSignature(signature)) :
6648+
signature;
6649+
}
6650+
6651+
function createCanonicalSignature(signature: Signature) {
6652+
// Create an instantiation of the signature where each unconstrained type parameter is replaced with
6653+
// its original. When a generic class or interface is instantiated, each generic method in the class or
6654+
// interface is instantiated with a fresh set of cloned type parameters (which we need to handle scenarios
6655+
// where different generations of the same type parameter are in scope). This leads to a lot of new type
6656+
// identities, and potentially a lot of work comparing those identities, so here we create an instantiation
6657+
// that reverts back to the original type identities for all unconstrained type parameters.
6658+
const canonicalTypeArguments = map(signature.typeParameters, tp => tp.target && !getConstraintOfTypeParameter(tp.target) ? tp.target : tp);
6659+
return instantiateSignature(signature, createTypeMapper(signature.typeParameters, canonicalTypeArguments), /*eraseTypeParameters*/ true);
66406660
}
66416661

66426662
function getOrCreateTypeFromSignature(signature: Signature): ObjectType {
@@ -8473,6 +8493,7 @@ namespace ts {
84738493
return Ternary.False;
84748494
}
84758495

8496+
target = getCanonicalSignature(target);
84768497
if (source.typeParameters) {
84778498
source = instantiateSignatureInContextOf(source, target, /*contextualMapper*/ undefined, compareTypes);
84788499
}

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,6 +3447,8 @@ namespace ts {
34473447
/* @internal */
34483448
erasedSignatureCache?: Signature; // Erased version of signature (deferred)
34493449
/* @internal */
3450+
canonicalSignatureCache?: Signature; // Canonical version of signature (deferred)
3451+
/* @internal */
34503452
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
34513453
/* @internal */
34523454
typePredicate?: TypePredicate;

0 commit comments

Comments
 (0)