Skip to content

Skip nested references in relation checking #17947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5364,7 +5364,7 @@ namespace ts {
return <InterfaceTypeWithDeclaredMembers>type;
}

function getTypeWithThisArgument(type: Type, thisArgument?: Type): Type {
function getTypeWithThisArgument(type: Type, thisArgument: Type): Type {
if (getObjectFlags(type) & ObjectFlags.Reference) {
const target = (<TypeReference>type).target;
const typeArguments = (<TypeReference>type).typeArguments;
Expand Down Expand Up @@ -9192,6 +9192,40 @@ namespace ts {
return result;
}

function getTypePairKey(source: Type, target: Type) {
return relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id;
}

function alreadyComparingTypeReferences(source: Type, target: Type) {
if (!(getObjectFlags(source) & ObjectFlags.Reference) || !(getObjectFlags(target) & ObjectFlags.Reference)) {
return false;
}
const sourceArguments = (source as TypeReference).typeArguments;
const targetArguments = (source as TypeReference).typeArguments;

const onlyTypeParametersAsArguments =
arrayIsEqualTo(sourceArguments, targetArguments) &&
sourceArguments && sourceArguments.length > 0 &&
every(sourceArguments, t => t.flags & TypeFlags.TypeParameter && !getConstraintFromTypeParameter(t as TypeParameter));
if (!onlyTypeParametersAsArguments) {
return false;
}
const id = getTypePairKey((source as TypeReference).target, (target as TypeReference).target);
if (!maybeKeys) {
maybeKeys = [];
}
else {
for (let i = 0; i < maybeCount; i++) {
if (id === maybeKeys[i]) {
return true;
}
}
}
maybeKeys[maybeCount] = id;
maybeCount++;
return false;
}

// Determine if possibly recursive types are related. First, check if the result is already available in the global cache.
// Second, check if we have already started a comparison of the given two types in which case we assume the result to be true.
// Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are
Expand All @@ -9201,7 +9235,8 @@ namespace ts {
if (overflow) {
return Ternary.False;
}
const id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id;

const id = getTypePairKey(source, target);
const related = relation.get(id);
if (related !== undefined) {
if (reportErrors && related === RelationComparisonResult.Failed) {
Expand All @@ -9213,6 +9248,7 @@ namespace ts {
return related === RelationComparisonResult.Succeeded ? Ternary.True : Ternary.False;
}
}

if (!maybeKeys) {
maybeKeys = [];
sourceStack = [];
Expand All @@ -9231,6 +9267,10 @@ namespace ts {
}
}
const maybeStart = maybeCount;
if (alreadyComparingTypeReferences(source, target)) {
return Ternary.Maybe;
}

maybeKeys[maybeCount] = id;
maybeCount++;
sourceStack[depth] = source;
Expand Down Expand Up @@ -21262,7 +21302,7 @@ namespace ts {
checkExportsOnMergedDeclarations(node);
const symbol = getSymbolOfNode(node);
const type = <InterfaceType>getDeclaredTypeOfSymbol(symbol);
const typeWithThis = getTypeWithThisArgument(type);
const typeWithThis = getTypeWithThisArgument(type, type.thisType);
const staticType = <ObjectType>getTypeOfSymbol(symbol);
checkTypeParameterListsIdentical(symbol);
checkClassForDuplicateDeclarations(node);
Expand Down Expand Up @@ -21510,7 +21550,7 @@ namespace ts {
const firstInterfaceDecl = getDeclarationOfKind<InterfaceDeclaration>(symbol, SyntaxKind.InterfaceDeclaration);
if (node === firstInterfaceDecl) {
const type = <InterfaceType>getDeclaredTypeOfSymbol(symbol);
const typeWithThis = getTypeWithThisArgument(type);
const typeWithThis = getTypeWithThisArgument(type, type.thisType);
// run subsequent checks only if first set succeeded
if (checkInheritedPropertiesAreIdentical(type, node.name)) {
for (const baseType of getBaseTypes(type)) {
Expand Down
Loading