Skip to content

Commit 3cfd6d2

Browse files
authored
Optimize eagerly calculating CouldContainTypeVariables during object type instantiation (#54538)
1 parent 3c4c060 commit 3cfd6d2

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5585,7 +5585,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
55855585
function createIntrinsicType(kind: TypeFlags, intrinsicName: string, objectFlags = ObjectFlags.None): IntrinsicType {
55865586
const type = createType(kind) as IntrinsicType;
55875587
type.intrinsicName = intrinsicName;
5588-
type.objectFlags = objectFlags;
5588+
type.objectFlags = objectFlags | ObjectFlags.CouldContainTypeVariablesComputed | ObjectFlags.IsGenericTypeComputed | ObjectFlags.IsUnknownLikeUnionComputed | ObjectFlags.IsNeverIntersectionComputed;
55895589
return type;
55905590
}
55915591

@@ -18798,17 +18798,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1879818798
result = target.objectFlags & ObjectFlags.Reference ? createDeferredTypeReference((type as DeferredTypeReference).target, (type as DeferredTypeReference).node, newMapper, newAliasSymbol, newAliasTypeArguments) :
1879918799
target.objectFlags & ObjectFlags.Mapped ? instantiateMappedType(target as MappedType, newMapper, newAliasSymbol, newAliasTypeArguments) :
1880018800
instantiateAnonymousType(target, newMapper, newAliasSymbol, newAliasTypeArguments);
18801-
// If none of the type arguments for the outer type parameters contain type variables, it follows
18802-
// that the instantiated type doesn't reference type variables.
18803-
if (result.flags & TypeFlags.ObjectFlagsType && !((result as ObjectFlagsType).objectFlags & ObjectFlags.CouldContainTypeVariablesComputed)) {
18804-
const resultCouldContainTypeVariables = some(typeArguments, couldContainTypeVariables);
18805-
// The above check may have caused the result's objectFlags to update if the result is referenced via typeArguments.
18806-
if (!((result as ObjectFlagsType).objectFlags & ObjectFlags.CouldContainTypeVariablesComputed)) {
18807-
(result as ObjectFlagsType).objectFlags |= ObjectFlags.CouldContainTypeVariablesComputed |
18808-
(resultCouldContainTypeVariables ? ObjectFlags.CouldContainTypeVariables : 0);
18801+
target.instantiations.set(id, result); // Set cached result early in case we recursively invoke instantiation while eagerly computing type variable visibility below
18802+
const resultObjectFlags = getObjectFlags(result);
18803+
if (result.flags & TypeFlags.ObjectFlagsType && !(resultObjectFlags & ObjectFlags.CouldContainTypeVariablesComputed)) {
18804+
const resultCouldContainTypeVariables = some(typeArguments, couldContainTypeVariables); // one of the input type arguments might be or contain the result
18805+
if (!(getObjectFlags(result) & ObjectFlags.CouldContainTypeVariablesComputed)) {
18806+
// if `result` is one of the object types we tried to make (it may not be, due to how `instantiateMappedType` works), we can carry forward the type variable containment check from the input type arguments
18807+
if (resultObjectFlags & (ObjectFlags.Mapped | ObjectFlags.Anonymous | ObjectFlags.Reference)) {
18808+
(result as ObjectFlagsType).objectFlags |= ObjectFlags.CouldContainTypeVariablesComputed | (resultCouldContainTypeVariables ? ObjectFlags.CouldContainTypeVariables : 0);
18809+
}
18810+
// If none of the type arguments for the outer type parameters contain type variables, it follows
18811+
// that the instantiated type doesn't reference type variables.
18812+
// Intrinsics have `CouldContainTypeVariablesComputed` pre-set, so this should only cover unions and intersections resulting from `instantiateMappedType`
18813+
else {
18814+
(result as ObjectFlagsType).objectFlags |= !resultCouldContainTypeVariables ? ObjectFlags.CouldContainTypeVariablesComputed : 0;
18815+
}
1880918816
}
1881018817
}
18811-
target.instantiations.set(id, result);
1881218818
}
1881318819
return result;
1881418820
}

0 commit comments

Comments
 (0)