Skip to content

Detect comparisons between large unions or intersections #41574

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

Merged
merged 6 commits into from
Dec 10, 2020
Merged
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
33 changes: 32 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16892,7 +16892,6 @@ namespace ts {
return isIdenticalTo(source, target);
}


// We fastpath comparing a type parameter to exactly its constraint, as this is _super_ common,
// and otherwise, for type parameters in large unions, causes us to need to compare the union to itself,
// as we break down the _target_ union first, _then_ get the source constraint - so for every
Expand Down Expand Up @@ -16953,6 +16952,8 @@ namespace ts {
return Ternary.False;
}

traceUnionsOrIntersectionsTooLarge(source, target);

let result = Ternary.False;
const saveErrorInfo = captureErrorCalculationState();

Expand Down Expand Up @@ -17088,11 +17089,41 @@ namespace ts {
}
}

function traceUnionsOrIntersectionsTooLarge(source: Type, target: Type): void {
if (!tracing.isTracing()) {
return;
}

if ((source.flags & TypeFlags.UnionOrIntersection) && (target.flags & TypeFlags.UnionOrIntersection)) {
const sourceUnionOrIntersection = source as UnionOrIntersectionType;
const targetUnionOrIntersection = target as UnionOrIntersectionType;

if (sourceUnionOrIntersection.objectFlags & targetUnionOrIntersection.objectFlags & ObjectFlags.PrimitiveUnion) {
// There's a fast path for comparing primitive unions
return;
}

const sourceSize = sourceUnionOrIntersection.types.length;
const targetSize = targetUnionOrIntersection.types.length;
if (sourceSize * targetSize > 1E6) {
tracing.instant(tracing.Phase.CheckTypes, "traceUnionsOrIntersectionsTooLarge_DepthLimit", {
sourceId: source.id,
sourceSize,
targetId: target.id,
targetSize,
pos: errorNode?.pos,
end: errorNode?.end
});
}
}
}

function isIdenticalTo(source: Type, target: Type): Ternary {
const flags = source.flags & target.flags;
if (!(flags & TypeFlags.Substructure)) {
return Ternary.False;
}
traceUnionsOrIntersectionsTooLarge(source, target);
if (flags & TypeFlags.UnionOrIntersection) {
let result = eachTypeRelatedToSomeType(<UnionOrIntersectionType>source, <UnionOrIntersectionType>target);
if (result) {
Expand Down