Skip to content

Enforce a size limit in getSpreadType #40755

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 2 commits into from
Sep 30, 2020
Merged
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
9 changes: 7 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13394,6 +13394,7 @@ namespace ts {
function checkCrossProductUnion(types: readonly Type[]) {
const size = reduceLeft(types, (n, t) => n * (t.flags & TypeFlags.Union ? (<UnionType>t).types.length : t.flags & TypeFlags.Never ? 0 : 1), 1);
if (size >= 100000) {
tracing.instant(tracing.Phase.Check, "checkCrossProductUnion_DepthLimit", { typeIds: types.map(t => t.id), size });
error(currentNode, Diagnostics.Expression_produces_a_union_type_that_is_too_complex_to_represent);
return false;
}
Expand Down Expand Up @@ -14458,14 +14459,18 @@ namespace ts {
if (merged) {
return getSpreadType(merged, right, symbol, objectFlags, readonly);
}
return mapType(left, t => getSpreadType(t, right, symbol, objectFlags, readonly));
return checkCrossProductUnion([left, right])
? mapType(left, t => getSpreadType(t, right, symbol, objectFlags, readonly))
: errorType;
}
if (right.flags & TypeFlags.Union) {
const merged = tryMergeUnionOfObjectTypeAndEmptyObject(right as UnionType, readonly);
if (merged) {
return getSpreadType(left, merged, symbol, objectFlags, readonly);
}
return mapType(right, t => getSpreadType(left, t, symbol, objectFlags, readonly));
return checkCrossProductUnion([left, right])
? mapType(right, t => getSpreadType(left, t, symbol, objectFlags, readonly))
: errorType;
}
if (right.flags & (TypeFlags.BooleanLike | TypeFlags.NumberLike | TypeFlags.BigIntLike | TypeFlags.StringLike | TypeFlags.EnumLike | TypeFlags.NonPrimitive | TypeFlags.Index)) {
return left;
Expand Down
170 changes: 170 additions & 0 deletions tests/baselines/reference/objectSpreadRepeatedComplexity.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
tests/cases/conformance/types/spread/objectSpreadRepeatedComplexity.ts(3,12): error TS2590: Expression produces a union type that is too complex to represent.


==== tests/cases/conformance/types/spread/objectSpreadRepeatedComplexity.ts (1 errors) ====
function f(cnd: Record<number, boolean>){
// Type is a union of 2^(n-1) members, where n is the number of spread objects
return {
~
// Without this one, it collapses to {} ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...(cnd[1] &&
~~~~~~~~~~~~~~~~~~~~~
cnd[2] && {
~~~~~~~~~~~~~~~~~~~~~~~
prop0: 0,
~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~


// With one prop each, it collapses to a single object (#34853?)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...(cnd[3] && {
~~~~~~~~~~~~~~~~~~~~~~~
prop3a: 1,
~~~~~~~~~~~~~~~~~~~~~~
prop3b: 1,
~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[4] && {
~~~~~~~~~~~~~~~~~~~~~~~
prop4a: 1,
~~~~~~~~~~~~~~~~~~~~~~
prop4b: 1,
~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[5] && {
~~~~~~~~~~~~~~~~~~~~~~~
prop5a: 1,
~~~~~~~~~~~~~~~~~~~~~~
prop5b: 1,
~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[6] && {
~~~~~~~~~~~~~~~~~~~~~~~
prop6a: 1,
~~~~~~~~~~~~~~~~~~~~~~
prop6b: 1,
~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[7] && {
~~~~~~~~~~~~~~~~~~~~~~~
prop7a: 1,
~~~~~~~~~~~~~~~~~~~~~~
prop7b: 1,
~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[8] && {
~~~~~~~~~~~~~~~~~~~~~~~
prop8a: 1,
~~~~~~~~~~~~~~~~~~~~~~
prop8b: 1,
~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[9] && {
~~~~~~~~~~~~~~~~~~~~~~~
prop9a: 1,
~~~~~~~~~~~~~~~~~~~~~~
prop9b: 1,
~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[10] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop10a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop10b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[11] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop11a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop11b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[12] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop12a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop12b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[13] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop13a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop13b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[14] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop14a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop14b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[15] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop15a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop15b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[16] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop16a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop16b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[17] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop17a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop17b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[18] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop18a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop18b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[19] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop19a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop19b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
...(cnd[20] && {
~~~~~~~~~~~~~~~~~~~~~~~~
prop20a: 1,
~~~~~~~~~~~~~~~~~~~~~~~
prop20b: 1,
~~~~~~~~~~~~~~~~~~~~~~~
}),
~~~~~~~~~~~
};
~~~~~
!!! error TS2590: Expression produces a union type that is too complex to represent.
}
160 changes: 160 additions & 0 deletions tests/baselines/reference/objectSpreadRepeatedComplexity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
//// [objectSpreadRepeatedComplexity.ts]
function f(cnd: Record<number, boolean>){
// Type is a union of 2^(n-1) members, where n is the number of spread objects
return {
// Without this one, it collapses to {} ?
...(cnd[1] &&
cnd[2] && {
prop0: 0,
}),

// With one prop each, it collapses to a single object (#34853?)
...(cnd[3] && {
prop3a: 1,
prop3b: 1,
}),
...(cnd[4] && {
prop4a: 1,
prop4b: 1,
}),
...(cnd[5] && {
prop5a: 1,
prop5b: 1,
}),
...(cnd[6] && {
prop6a: 1,
prop6b: 1,
}),
...(cnd[7] && {
prop7a: 1,
prop7b: 1,
}),
...(cnd[8] && {
prop8a: 1,
prop8b: 1,
}),
...(cnd[9] && {
prop9a: 1,
prop9b: 1,
}),
...(cnd[10] && {
prop10a: 1,
prop10b: 1,
}),
...(cnd[11] && {
prop11a: 1,
prop11b: 1,
}),
...(cnd[12] && {
prop12a: 1,
prop12b: 1,
}),
...(cnd[13] && {
prop13a: 1,
prop13b: 1,
}),
...(cnd[14] && {
prop14a: 1,
prop14b: 1,
}),
...(cnd[15] && {
prop15a: 1,
prop15b: 1,
}),
...(cnd[16] && {
prop16a: 1,
prop16b: 1,
}),
...(cnd[17] && {
prop17a: 1,
prop17b: 1,
}),
...(cnd[18] && {
prop18a: 1,
prop18b: 1,
}),
...(cnd[19] && {
prop19a: 1,
prop19b: 1,
}),
...(cnd[20] && {
prop20a: 1,
prop20b: 1,
}),
};
}

//// [objectSpreadRepeatedComplexity.js]
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function f(cnd) {
// Type is a union of 2^(n-1) members, where n is the number of spread objects
return __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, (cnd[1] &&
cnd[2] && {
prop0: 0
})), (cnd[3] && {
prop3a: 1,
prop3b: 1
})), (cnd[4] && {
prop4a: 1,
prop4b: 1
})), (cnd[5] && {
prop5a: 1,
prop5b: 1
})), (cnd[6] && {
prop6a: 1,
prop6b: 1
})), (cnd[7] && {
prop7a: 1,
prop7b: 1
})), (cnd[8] && {
prop8a: 1,
prop8b: 1
})), (cnd[9] && {
prop9a: 1,
prop9b: 1
})), (cnd[10] && {
prop10a: 1,
prop10b: 1
})), (cnd[11] && {
prop11a: 1,
prop11b: 1
})), (cnd[12] && {
prop12a: 1,
prop12b: 1
})), (cnd[13] && {
prop13a: 1,
prop13b: 1
})), (cnd[14] && {
prop14a: 1,
prop14b: 1
})), (cnd[15] && {
prop15a: 1,
prop15b: 1
})), (cnd[16] && {
prop16a: 1,
prop16b: 1
})), (cnd[17] && {
prop17a: 1,
prop17b: 1
})), (cnd[18] && {
prop18a: 1,
prop18b: 1
})), (cnd[19] && {
prop19a: 1,
prop19b: 1
})), (cnd[20] && {
prop20a: 1,
prop20b: 1
}));
}
Loading