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

Fix crash when serializing the return type of a generic call to Array.prototype.flat #38904

Merged
merged 4 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 28 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4404,8 +4404,8 @@ namespace ts {
context.inferTypeParameters = (<ConditionalType>type).root.inferTypeParameters;
const extendsTypeNode = typeToTypeNodeHelper((<ConditionalType>type).extendsType, context);
context.inferTypeParameters = saveInferTypeParameters;
const trueTypeNode = typeToTypeNodeHelper(getTrueTypeFromConditionalType(<ConditionalType>type), context);
const falseTypeNode = typeToTypeNodeHelper(getFalseTypeFromConditionalType(<ConditionalType>type), context);
const trueTypeNode = typeToTypeNodeOrCircularityEllision(getTrueTypeFromConditionalType(<ConditionalType>type));
const falseTypeNode = typeToTypeNodeOrCircularityEllision(getFalseTypeFromConditionalType(<ConditionalType>type));
weswigham marked this conversation as resolved.
Show resolved Hide resolved
context.approximateLength += 15;
return createConditionalTypeNode(checkTypeNode, extendsTypeNode, trueTypeNode, falseTypeNode);
}
Expand All @@ -4415,6 +4415,21 @@ namespace ts {

return Debug.fail("Should be unreachable.");


function typeToTypeNodeOrCircularityEllision(type: Type) {
weswigham marked this conversation as resolved.
Show resolved Hide resolved
if (type.flags & TypeFlags.Union) {
weswigham marked this conversation as resolved.
Show resolved Hide resolved
if (context.visitedTypes && context.visitedTypes.has("" + getTypeId(type))) {
if (!(context.flags & NodeBuilderFlags.AllowAnonymousIdentifier)) {
context.encounteredError = true;
context.tracker?.reportCyclicStructureError?.();
weswigham marked this conversation as resolved.
Show resolved Hide resolved
}
return createElidedInformationPlaceholder(context);
}
return visitAndTransformType(type, type => typeToTypeNodeHelper(type, context));
}
return typeToTypeNodeHelper(type, context);
}

function createMappedTypeNodeFromType(type: MappedType) {
Debug.assert(!!(type.flags & TypeFlags.Object));
const readonlyToken = type.declaration.readonlyToken ? <ReadonlyToken | PlusToken | MinusToken>createToken(type.declaration.readonlyToken.kind) : undefined;
Expand Down Expand Up @@ -12913,10 +12928,12 @@ namespace ts {
return links.resolvedType;
}

function createIndexedAccessType(objectType: Type, indexType: Type) {
function createIndexedAccessType(objectType: Type, indexType: Type, aliasSymbol: Symbol | undefined, aliasTypeArguments: readonly Type[] | undefined) {
const type = <IndexedAccessType>createType(TypeFlags.IndexedAccess);
type.objectType = objectType;
type.indexType = indexType;
type.aliasSymbol = aliasSymbol;
type.aliasTypeArguments = aliasTypeArguments;
return type;
}

Expand Down Expand Up @@ -13251,11 +13268,11 @@ namespace ts {
return instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper);
}

function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression): Type {
return getIndexedAccessTypeOrUndefined(objectType, indexType, accessNode, AccessFlags.None) || (accessNode ? errorType : unknownType);
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[]): Type {
return getIndexedAccessTypeOrUndefined(objectType, indexType, accessNode, AccessFlags.None, aliasSymbol, aliasTypeArguments) || (accessNode ? errorType : unknownType);
}

function getIndexedAccessTypeOrUndefined(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, accessFlags = AccessFlags.None): Type | undefined {
function getIndexedAccessTypeOrUndefined(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, accessFlags = AccessFlags.None, aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[]): Type | undefined {
if (objectType === wildcardType || indexType === wildcardType) {
return wildcardType;
}
Expand All @@ -13277,7 +13294,7 @@ namespace ts {
const id = objectType.id + "," + indexType.id;
let type = indexedAccessTypes.get(id);
if (!type) {
indexedAccessTypes.set(id, type = createIndexedAccessType(objectType, indexType));
indexedAccessTypes.set(id, type = createIndexedAccessType(objectType, indexType, aliasSymbol, aliasTypeArguments));
}
return type;
}
Expand Down Expand Up @@ -13305,7 +13322,7 @@ namespace ts {
if (wasMissingProp) {
return undefined;
}
return accessFlags & AccessFlags.Writing ? getIntersectionType(propTypes) : getUnionType(propTypes);
return accessFlags & AccessFlags.Writing ? getIntersectionType(propTypes, aliasSymbol, aliasTypeArguments) : getUnionType(propTypes, UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
}
return getPropertyTypeForIndexType(objectType, apparentObjectType, indexType, indexType, /* supressNoImplicitAnyError */ false, accessNode, accessFlags | AccessFlags.CacheSymbol);
}
Expand All @@ -13315,7 +13332,8 @@ namespace ts {
if (!links.resolvedType) {
const objectType = getTypeFromTypeNode(node.objectType);
const indexType = getTypeFromTypeNode(node.indexType);
const resolved = getIndexedAccessType(objectType, indexType, node);
const potentialAlias = getAliasSymbolForTypeNode(node);
const resolved = getIndexedAccessType(objectType, indexType, node, potentialAlias, getTypeArgumentsForAliasSymbol(potentialAlias));
links.resolvedType = resolved.flags & TypeFlags.IndexedAccess &&
(<IndexedAccessType>resolved).objectType === objectType &&
(<IndexedAccessType>resolved).indexType === indexType ?
Expand Down Expand Up @@ -14466,7 +14484,7 @@ namespace ts {
return getIndexType(instantiateType((<IndexType>type).type, mapper));
}
if (flags & TypeFlags.IndexedAccess) {
return getIndexedAccessType(instantiateType((<IndexedAccessType>type).objectType, mapper), instantiateType((<IndexedAccessType>type).indexType, mapper));
return getIndexedAccessType(instantiateType((<IndexedAccessType>type).objectType, mapper), instantiateType((<IndexedAccessType>type).indexType, mapper), /*accessNode*/ undefined, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
}
if (flags & TypeFlags.Conditional) {
return getConditionalTypeInstantiation(<ConditionalType>type, combineTypeMappers((<ConditionalType>type).mapper, mapper));
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3533,6 +3533,10 @@
"category": "Error",
"code": 5087
},
"The inferred type of '{0}' references a type with a cyclic structure which cannot be trivially serialized. A type annotation is necessary.": {
"category": "Error",
"code": 5088
},

"Generates a sourcemap for each corresponding '.d.ts' file.": {
"category": "Message",
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace ts {
trackSymbol,
reportInaccessibleThisError,
reportInaccessibleUniqueSymbolError,
reportCyclicStructureError,
reportPrivateInBaseOfClassExpression,
reportLikelyUnsafeImportRequiredError,
moduleResolverHost: host,
Expand Down Expand Up @@ -175,6 +176,13 @@ namespace ts {
}
}

function reportCyclicStructureError() {
if (errorNameNode) {
context.addDiagnostic(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_0_references_a_type_with_a_cyclic_structure_which_cannot_be_trivially_serialized_A_type_annotation_is_necessary,
declarationNameToString(errorNameNode)));
}
}

function reportInaccessibleThisError() {
if (errorNameNode) {
context.addDiagnostic(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_0_references_an_inaccessible_1_type_A_type_annotation_is_necessary,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6481,6 +6481,7 @@ namespace ts {
reportInaccessibleThisError?(): void;
reportPrivateInBaseOfClassExpression?(propertyName: string): void;
reportInaccessibleUniqueSymbolError?(): void;
reportCyclicStructureError?(): void;
reportLikelyUnsafeImportRequiredError?(specifier: string): void;
moduleResolverHost?: ModuleSpecifierResolutionHost & { getCommonSourceDirectory(): string };
trackReferencedAmbientModule?(decl: ModuleDeclaration, symbol: Symbol): void;
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/arrayFlatNoCrashInference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [arrayFlatNoCrashInference.ts]
function foo<T>(arr: T[], depth: number) {
return arr.flat(depth);
}

//// [arrayFlatNoCrashInference.js]
"use strict";
function foo(arr, depth) {
return arr.flat(depth);
}
14 changes: 14 additions & 0 deletions tests/baselines/reference/arrayFlatNoCrashInference.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/arrayFlatNoCrashInference.ts ===
function foo<T>(arr: T[], depth: number) {
>foo : Symbol(foo, Decl(arrayFlatNoCrashInference.ts, 0, 0))
>T : Symbol(T, Decl(arrayFlatNoCrashInference.ts, 0, 13))
>arr : Symbol(arr, Decl(arrayFlatNoCrashInference.ts, 0, 16))
>T : Symbol(T, Decl(arrayFlatNoCrashInference.ts, 0, 13))
>depth : Symbol(depth, Decl(arrayFlatNoCrashInference.ts, 0, 25))

return arr.flat(depth);
>arr.flat : Symbol(Array.flat, Decl(lib.es2019.array.d.ts, --, --))
>arr : Symbol(arr, Decl(arrayFlatNoCrashInference.ts, 0, 16))
>flat : Symbol(Array.flat, Decl(lib.es2019.array.d.ts, --, --))
>depth : Symbol(depth, Decl(arrayFlatNoCrashInference.ts, 0, 25))
}
13 changes: 13 additions & 0 deletions tests/baselines/reference/arrayFlatNoCrashInference.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/arrayFlatNoCrashInference.ts ===
function foo<T>(arr: T[], depth: number) {
>foo : <T>(arr: T[], depth: number) => FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
>arr : T[]
>depth : number

return arr.flat(depth);
>arr.flat(depth) : FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
>arr.flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]
>arr : T[]
>flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]
>depth : number
}
14 changes: 14 additions & 0 deletions tests/baselines/reference/arrayFlatNoCrashInferenceDeclarations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [arrayFlatNoCrashInferenceDeclarations.ts]
function foo<T>(arr: T[], depth: number) {
return arr.flat(depth);
}

//// [arrayFlatNoCrashInferenceDeclarations.js]
"use strict";
function foo(arr, depth) {
return arr.flat(depth);
}


//// [arrayFlatNoCrashInferenceDeclarations.d.ts]
declare function foo<T>(arr: T[], depth: number): FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[];
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/arrayFlatNoCrashInferenceDeclarations.ts ===
function foo<T>(arr: T[], depth: number) {
>foo : Symbol(foo, Decl(arrayFlatNoCrashInferenceDeclarations.ts, 0, 0))
>T : Symbol(T, Decl(arrayFlatNoCrashInferenceDeclarations.ts, 0, 13))
>arr : Symbol(arr, Decl(arrayFlatNoCrashInferenceDeclarations.ts, 0, 16))
>T : Symbol(T, Decl(arrayFlatNoCrashInferenceDeclarations.ts, 0, 13))
>depth : Symbol(depth, Decl(arrayFlatNoCrashInferenceDeclarations.ts, 0, 25))

return arr.flat(depth);
>arr.flat : Symbol(Array.flat, Decl(lib.es2019.array.d.ts, --, --))
>arr : Symbol(arr, Decl(arrayFlatNoCrashInferenceDeclarations.ts, 0, 16))
>flat : Symbol(Array.flat, Decl(lib.es2019.array.d.ts, --, --))
>depth : Symbol(depth, Decl(arrayFlatNoCrashInferenceDeclarations.ts, 0, 25))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/arrayFlatNoCrashInferenceDeclarations.ts ===
function foo<T>(arr: T[], depth: number) {
>foo : <T>(arr: T[], depth: number) => FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
>arr : T[]
>depth : number

return arr.flat(depth);
>arr.flat(depth) : FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
>arr.flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]
>arr : T[]
>flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]
>depth : number
}
16 changes: 8 additions & 8 deletions tests/baselines/reference/bigintWithLib.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ tests/cases/compiler/bigintWithLib.ts(16,33): error TS2769: No overload matches
Type 'IteratorYieldResult<number>' is not assignable to type 'IteratorResult<bigint, any>'.
Type 'IteratorYieldResult<number>' is not assignable to type 'IteratorYieldResult<bigint>'.
Type 'number' is not assignable to type 'bigint'.
Overload 3 of 3, '(buffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): BigInt64Array', gave the following error.
Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
Overload 3 of 3, '(buffer: ArrayBufferLike, byteOffset?: number, length?: number): BigInt64Array', gave the following error.
Argument of type 'number[]' is not assignable to parameter of type 'ArrayBufferLike'.
Type 'number[]' is missing the following properties from type 'SharedArrayBuffer': byteLength, [Symbol.species], [Symbol.toStringTag]
tests/cases/compiler/bigintWithLib.ts(21,13): error TS2540: Cannot assign to 'length' because it is a read-only property.
tests/cases/compiler/bigintWithLib.ts(28,35): error TS2769: No overload matches this call.
Overload 1 of 3, '(length?: number): BigUint64Array', gave the following error.
Argument of type 'number[]' is not assignable to parameter of type 'number'.
Overload 2 of 3, '(array: Iterable<bigint>): BigUint64Array', gave the following error.
Argument of type 'number[]' is not assignable to parameter of type 'Iterable<bigint>'.
Overload 3 of 3, '(buffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): BigUint64Array', gave the following error.
Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
Overload 3 of 3, '(buffer: ArrayBufferLike, byteOffset?: number, length?: number): BigUint64Array', gave the following error.
Argument of type 'number[]' is not assignable to parameter of type 'ArrayBufferLike'.
Type 'number[]' is not assignable to type 'SharedArrayBuffer'.
tests/cases/compiler/bigintWithLib.ts(33,13): error TS2540: Cannot assign to 'length' because it is a read-only property.
tests/cases/compiler/bigintWithLib.ts(40,25): error TS2345: Argument of type 'number' is not assignable to parameter of type 'bigint'.
Expand Down Expand Up @@ -56,8 +56,8 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type 'nu
!!! error TS2769: Type 'IteratorYieldResult<number>' is not assignable to type 'IteratorResult<bigint, any>'.
!!! error TS2769: Type 'IteratorYieldResult<number>' is not assignable to type 'IteratorYieldResult<bigint>'.
!!! error TS2769: Type 'number' is not assignable to type 'bigint'.
!!! error TS2769: Overload 3 of 3, '(buffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): BigInt64Array', gave the following error.
!!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
!!! error TS2769: Overload 3 of 3, '(buffer: ArrayBufferLike, byteOffset?: number, length?: number): BigInt64Array', gave the following error.
!!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBufferLike'.
!!! error TS2769: Type 'number[]' is missing the following properties from type 'SharedArrayBuffer': byteLength, [Symbol.species], [Symbol.toStringTag]
bigIntArray = new BigInt64Array(new ArrayBuffer(80));
bigIntArray = new BigInt64Array(new ArrayBuffer(80), 8);
Expand All @@ -79,8 +79,8 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type 'nu
!!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'number'.
!!! error TS2769: Overload 2 of 3, '(array: Iterable<bigint>): BigUint64Array', gave the following error.
!!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'Iterable<bigint>'.
!!! error TS2769: Overload 3 of 3, '(buffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): BigUint64Array', gave the following error.
!!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
!!! error TS2769: Overload 3 of 3, '(buffer: ArrayBufferLike, byteOffset?: number, length?: number): BigUint64Array', gave the following error.
!!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBufferLike'.
!!! error TS2769: Type 'number[]' is not assignable to type 'SharedArrayBuffer'.
bigUintArray = new BigUint64Array(new ArrayBuffer(80));
bigUintArray = new BigUint64Array(new ArrayBuffer(80), 8);
Expand Down

Large diffs are not rendered by default.

Loading