Skip to content
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

Error on complex array literals #28707

Closed
wants to merge 4 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
61 changes: 36 additions & 25 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ namespace ts {
const autoType = createIntrinsicType(TypeFlags.Any, "any");
const wildcardType = createIntrinsicType(TypeFlags.Any, "any");
const errorType = createIntrinsicType(TypeFlags.Any, "error");
const overflowErrorType = createIntrinsicType(TypeFlags.Any, "error");
const unknownType = createIntrinsicType(TypeFlags.Unknown, "unknown");
const undefinedType = createIntrinsicType(TypeFlags.Undefined, "undefined");
const undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsWideningType, "undefined");
Expand Down Expand Up @@ -9024,18 +9025,6 @@ namespace ts {
return includes;
}

function isSubtypeOfAny(source: Type, targets: ReadonlyArray<Type>): boolean {
for (const target of targets) {
if (source !== target && isTypeSubtypeOf(source, target) && (
!(getObjectFlags(getTargetType(source)) & ObjectFlags.Class) ||
!(getObjectFlags(getTargetType(target)) & ObjectFlags.Class) ||
isTypeDerivedFrom(source, target))) {
return true;
}
}
return false;
}

function isSetOfLiteralsFromSameEnum(types: ReadonlyArray<Type>): boolean {
const first = types[0];
if (first.flags & TypeFlags.EnumLiteral) {
Expand All @@ -9052,17 +9041,34 @@ namespace ts {
return false;
}

function removeSubtypes(types: Type[]) {
function removeSubtypes(types: Type[], checkOverflow: boolean): boolean {
if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) {
return;
return true;
}
let i = types.length;
while (i > 0) {
let count = 0;
loop: while (i > 0) {
i--;
if (isSubtypeOfAny(types[i], types)) {
orderedRemoveItemAt(types, i);
const source = types[i];
for (const target of types) {
if (source !== target) {
if (isTypeSubtypeOf(source, target) && (
!(getObjectFlags(getTargetType(source)) & ObjectFlags.Class) ||
!(getObjectFlags(getTargetType(target)) & ObjectFlags.Class) ||
isTypeDerivedFrom(source, target))) {
orderedRemoveItemAt(types, i);
continue loop;
}
count++;
if (checkOverflow && count >= 500000) {
// If we reach 500000 failed subtype checks, we deem the union type
// too complex to represent. We indicate this by returning false.
return false;
}
}
}
}
return true;
}

function removeRedundantLiteralTypes(types: Type[], includes: TypeFlags) {
Expand Down Expand Up @@ -9109,7 +9115,10 @@ namespace ts {
}
break;
case UnionReduction.Subtype:
removeSubtypes(typeSet);
case UnionReduction.SubtypeOrError:
if (!removeSubtypes(typeSet, /*checkOverflow*/ unionReduction === UnionReduction.SubtypeOrError)) {
return overflowErrorType;
}
break;
}
if (typeSet.length === 0) {
Expand Down Expand Up @@ -17717,7 +17726,15 @@ namespace ts {
return createTupleType(elementTypes, minLength, hasRestElement);
}
}
return getArrayLiteralType(elementTypes, UnionReduction.Subtype);
if (!elementTypes.length) {
return createArrayType(strictNullChecks ? implicitNeverType : undefinedWideningType);
}
const elementType = getUnionType(elementTypes, UnionReduction.SubtypeOrError);
if (elementType === overflowErrorType) {
error(node, Diagnostics.Array_literal_type_is_too_complex_to_represent);
return createArrayType(errorType);
}
return createArrayType(elementType);
}

function getArrayLiteralTupleTypeIfApplicable(elementTypes: Type[], contextualType: Type | undefined, hasRestElement: boolean, elementCount = elementTypes.length) {
Expand Down Expand Up @@ -17746,12 +17763,6 @@ namespace ts {
}
}

function getArrayLiteralType(elementTypes: Type[], unionReduction = UnionReduction.Literal) {
return createArrayType(elementTypes.length ?
getUnionType(elementTypes, unionReduction) :
strictNullChecks ? implicitNeverType : undefinedWideningType);
}

function isNumericName(name: DeclarationName): boolean {
switch (name.kind) {
case SyntaxKind.ComputedPropertyName:
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2529,6 +2529,10 @@
"category": "Error",
"code": 2742
},
"Array literal type is too complex to represent.": {
"category": "Error",
"code": 2743
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3230,7 +3230,8 @@ namespace ts {
export const enum UnionReduction {
None = 0,
Literal,
Subtype
Subtype,
SubtypeOrError
}

// NOTE: If modifying this enum, must modify `TypeFormatFlags` too!
Expand Down
Loading