Skip to content

Fixed regression in signature instantiation #59121

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
Jul 9, 2024
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15958,7 +15958,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function createSignatureTypeMapper(signature: Signature, typeArguments: readonly Type[] | undefined): TypeMapper {
return createTypeMapper(signature.typeParameters!, typeArguments);
return createTypeMapper(sameMap(signature.typeParameters!, tp => tp.mapper ? instantiateType(tp, tp.mapper) : tp), typeArguments);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the signature might already be a product of instantiateSignature that was called without eraseTypeParameters and which thus cloned type parameters. Those cloned parameters are instantiated there in the arguments list using the available mapper but the same mapper wasn't later used when erasing type parameters from that result. This led to the mapper-instantiated cloned type parameter being left non-instantiated after erasing type parameters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% certain, but it may be better to do sameMap here just because that would avoid extra arrays when nothing actually needs to be mapped (but maybe it doesn't happen that often).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I pushed out a commit to use sameMap here

}

function getErasedSignature(signature: Signature): Signature {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [tests/cases/compiler/excessiveStackDepthFlatArray.ts] ////

=== Performance Stats ===
Instantiation count: 1,000
Instantiation count: 1,000 -> 2,500

=== index.tsx ===
interface MiddlewareArray<T> extends Array<T> {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [tests/cases/compiler/genericCallInferenceConditionalType1.ts] ////

=== genericCallInferenceConditionalType1.ts ===
// https://github.com/microsoft/TypeScript/issues/59108

declare const f: <T>(f: (x: T) => unknown) => (x: T) => unknown;
>f : Symbol(f, Decl(genericCallInferenceConditionalType1.ts, 2, 13))
>T : Symbol(T, Decl(genericCallInferenceConditionalType1.ts, 2, 18))
>f : Symbol(f, Decl(genericCallInferenceConditionalType1.ts, 2, 21))
>x : Symbol(x, Decl(genericCallInferenceConditionalType1.ts, 2, 25))
>T : Symbol(T, Decl(genericCallInferenceConditionalType1.ts, 2, 18))
>x : Symbol(x, Decl(genericCallInferenceConditionalType1.ts, 2, 47))
>T : Symbol(T, Decl(genericCallInferenceConditionalType1.ts, 2, 18))

declare const g: <T extends unknown>(x: { foo: T }) => unknown;
>g : Symbol(g, Decl(genericCallInferenceConditionalType1.ts, 3, 13))
>T : Symbol(T, Decl(genericCallInferenceConditionalType1.ts, 3, 18))
>x : Symbol(x, Decl(genericCallInferenceConditionalType1.ts, 3, 37))
>foo : Symbol(foo, Decl(genericCallInferenceConditionalType1.ts, 3, 41))
>T : Symbol(T, Decl(genericCallInferenceConditionalType1.ts, 3, 18))

const h = f(g);
>h : Symbol(h, Decl(genericCallInferenceConditionalType1.ts, 5, 5))
>f : Symbol(f, Decl(genericCallInferenceConditionalType1.ts, 2, 13))
>g : Symbol(g, Decl(genericCallInferenceConditionalType1.ts, 3, 13))

type FirstParameter<T> = T extends (x: infer P) => unknown ? P : unknown;
>FirstParameter : Symbol(FirstParameter, Decl(genericCallInferenceConditionalType1.ts, 5, 15))
>T : Symbol(T, Decl(genericCallInferenceConditionalType1.ts, 7, 20))
>T : Symbol(T, Decl(genericCallInferenceConditionalType1.ts, 7, 20))
>x : Symbol(x, Decl(genericCallInferenceConditionalType1.ts, 7, 36))
>P : Symbol(P, Decl(genericCallInferenceConditionalType1.ts, 7, 44))
>P : Symbol(P, Decl(genericCallInferenceConditionalType1.ts, 7, 44))

type X = FirstParameter<typeof h>["foo"];
>X : Symbol(X, Decl(genericCallInferenceConditionalType1.ts, 7, 73))
>FirstParameter : Symbol(FirstParameter, Decl(genericCallInferenceConditionalType1.ts, 5, 15))
>h : Symbol(h, Decl(genericCallInferenceConditionalType1.ts, 5, 5))

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

=== genericCallInferenceConditionalType1.ts ===
// https://github.com/microsoft/TypeScript/issues/59108

declare const f: <T>(f: (x: T) => unknown) => (x: T) => unknown;
>f : <T>(f: (x: T) => unknown) => (x: T) => unknown
> : ^ ^^ ^^ ^^^^^
>f : (x: T) => unknown
> : ^ ^^ ^^^^^
>x : T
> : ^
>x : T
> : ^

declare const g: <T extends unknown>(x: { foo: T }) => unknown;
>g : <T extends unknown>(x: { foo: T; }) => unknown
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
>x : { foo: T; }
> : ^^^^^^^ ^^^
>foo : T
> : ^

const h = f(g);
>h : <T extends unknown>(x: { foo: T; }) => unknown
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^^^^^^^
>f(g) : <T extends unknown>(x: { foo: T; }) => unknown
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^^^^^^^
>f : <T>(f: (x: T) => unknown) => (x: T) => unknown
> : ^ ^^ ^^ ^^^^^
>g : <T extends unknown>(x: { foo: T; }) => unknown
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^

type FirstParameter<T> = T extends (x: infer P) => unknown ? P : unknown;
>FirstParameter : FirstParameter<T>
> : ^^^^^^^^^^^^^^^^^
>x : P
> : ^

type X = FirstParameter<typeof h>["foo"];
>X : unknown
> : ^^^^^^^
>h : <T extends unknown>(x: { foo: T; }) => unknown
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^^^^^^^

13 changes: 13 additions & 0 deletions tests/cases/compiler/genericCallInferenceConditionalType1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @strict: true
// @noEmit: true

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

declare const f: <T>(f: (x: T) => unknown) => (x: T) => unknown;
declare const g: <T extends unknown>(x: { foo: T }) => unknown;

const h = f(g);

type FirstParameter<T> = T extends (x: infer P) => unknown ? P : unknown;

type X = FirstParameter<typeof h>["foo"];