Move inferFromTypes to use isDeeplyNestedGeneric

This commit is contained in:
Jason Freeman 2015-06-09 11:45:26 -07:00
parent 6490d67509
commit 9c4d30952b

View file

@ -4438,8 +4438,8 @@ module ts {
maybeStack[depth][id] = RelationComparisonResult.Succeeded;
depth++;
let saveExpandingFlags = expandingFlags;
if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack)) expandingFlags |= 1;
if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack)) expandingFlags |= 2;
if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack, depth)) expandingFlags |= 1;
if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack, depth)) expandingFlags |= 2;
let result: Ternary;
if (expandingFlags === 3) {
result = Ternary.Maybe;
@ -4475,27 +4475,6 @@ module ts {
return result;
}
// Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case
// when structural type comparisons have been started for 10 or more instantiations of the same generic type. It is possible,
// though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely expanding.
// Effectively, we will generate a false positive when two types are structurally equal to at least 10 levels, but unequal at
// some level beyond that.
function isDeeplyNestedGeneric(type: ObjectType, stack: ObjectType[]): boolean {
// We track type references (created by createTypeReference) and instantiated types (created by instantiateType)
if (type.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && depth >= 10) {
let symbol = type.symbol;
let count = 0;
for (let i = 0; i < depth; i++) {
let t = stack[i];
if (t.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && t.symbol === symbol) {
count++;
if (count >= 10) return true;
}
}
}
return false;
}
function propertiesRelatedTo(source: ObjectType, target: ObjectType, reportErrors: boolean): Ternary {
if (relation === identityRelation) {
return propertiesIdenticalTo(source, target);
@ -4814,6 +4793,27 @@ module ts {
}
}
// Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case
// when structural type comparisons have been started for 10 or more instantiations of the same generic type. It is possible,
// though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely expanding.
// Effectively, we will generate a false positive when two types are structurally equal to at least 10 levels, but unequal at
// some level beyond that.
function isDeeplyNestedGeneric(type: Type, stack: Type[], depth: number): boolean {
// We track type references (created by createTypeReference) and instantiated types (created by instantiateType)
if (type.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && depth >= 10) {
let symbol = type.symbol;
let count = 0;
for (let i = 0; i < depth; i++) {
let t = stack[i];
if (t.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && t.symbol === symbol) {
count++;
if (count >= 10) return true;
}
}
}
return false;
}
function isPropertyIdenticalTo(sourceProp: Symbol, targetProp: Symbol): boolean {
return compareProperties(sourceProp, targetProp, compareTypes) !== Ternary.False;
}
@ -5128,21 +5128,6 @@ module ts {
return false;
}
function isWithinDepthLimit(type: Type, stack: Type[]) {
if (depth >= 5) {
let target = (<TypeReference>type).target;
let count = 0;
for (let i = 0; i < depth; i++) {
let t = stack[i];
if (t.flags & TypeFlags.Reference && (<TypeReference>t).target === target) {
count++;
}
}
return count < 5;
}
return true;
}
function inferFromTypes(source: Type, target: Type) {
if (source === anyFunctionType) {
return;
@ -5210,7 +5195,7 @@ module ts {
else if (source.flags & TypeFlags.ObjectType && (target.flags & (TypeFlags.Reference | TypeFlags.Tuple) ||
(target.flags & TypeFlags.Anonymous) && target.symbol && target.symbol.flags & (SymbolFlags.Method | SymbolFlags.TypeLiteral))) {
// If source is an object type, and target is a type reference, a tuple type, the type of a method, or a type literal, infer from members
if (!isInProcess(source, target) && isWithinDepthLimit(source, sourceStack) && isWithinDepthLimit(target, targetStack)) {
if (!isInProcess(source, target) && !(isDeeplyNestedGeneric(source, sourceStack, depth) && isDeeplyNestedGeneric(target, targetStack, depth))) {
if (depth === 0) {
sourceStack = [];
targetStack = [];