Skip to content

Fixed crash on circular local type arguments when outer ones are present too #59089

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
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
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16298,7 +16298,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function getTypeArguments(type: TypeReference): readonly Type[] {
if (!type.resolvedTypeArguments) {
if (!pushTypeResolution(type, TypeSystemPropertyName.ResolvedTypeArguments)) {
return type.target.localTypeParameters?.map(() => errorType) || emptyArray;
return concatenate(type.target.outerTypeParameters, type.target.localTypeParameters?.map(() => errorType)) || emptyArray;
}
const node = type.node;
const typeArguments = !node ? emptyArray :
Expand All @@ -16309,7 +16309,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
type.resolvedTypeArguments ??= type.mapper ? instantiateTypes(typeArguments, type.mapper) : typeArguments;
}
else {
type.resolvedTypeArguments ??= type.target.localTypeParameters?.map(() => errorType) || emptyArray;
type.resolvedTypeArguments ??= concatenate(type.target.outerTypeParameters, type.target.localTypeParameters?.map(() => errorType) || emptyArray);
error(
type.node || currentNode,
type.target.symbol ? Diagnostics.Type_arguments_for_0_circularly_reference_themselves : Diagnostics.Tuple_type_arguments_circularly_reference_themselves,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
circularTypeArgumentsLocalAndOuterNoCrash1.ts(5,12): error TS4109: Type arguments for 'NumArray' circularly reference themselves.


==== circularTypeArgumentsLocalAndOuterNoCrash1.ts (1 errors) ====
// https://github.com/microsoft/TypeScript/issues/59062

function f<_T, _S>() {
interface NumArray<T extends number> extends Array<T> {}
type X = NumArray<X extends {} ? number : number>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS4109: Type arguments for 'NumArray' circularly reference themselves.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/circularTypeArgumentsLocalAndOuterNoCrash1.ts] ////

=== circularTypeArgumentsLocalAndOuterNoCrash1.ts ===
// https://github.com/microsoft/TypeScript/issues/59062

function f<_T, _S>() {
>f : Symbol(f, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 0, 0))
>_T : Symbol(_T, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 2, 11))
>_S : Symbol(_S, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 2, 14))

interface NumArray<T extends number> extends Array<T> {}
>NumArray : Symbol(NumArray, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 2, 22))
>T : Symbol(T, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 3, 21))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 3, 21))

type X = NumArray<X extends {} ? number : number>;
>X : Symbol(X, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 3, 58))
>NumArray : Symbol(NumArray, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 2, 22))
>X : Symbol(X, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 3, 58))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/compiler/circularTypeArgumentsLocalAndOuterNoCrash1.ts] ////

=== circularTypeArgumentsLocalAndOuterNoCrash1.ts ===
// https://github.com/microsoft/TypeScript/issues/59062

function f<_T, _S>() {
>f : <_T, _S>() => void
> : ^ ^^ ^^^^^^^^^^^

interface NumArray<T extends number> extends Array<T> {}
type X = NumArray<X extends {} ? number : number>;
>X : NumArray<any>
> : ^^^^^^^^^^^^^
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/59062

function f<_T, _S>() {
interface NumArray<T extends number> extends Array<T> {}
type X = NumArray<X extends {} ? number : number>;
}