Skip to content

Propagate outer type parameters of single signature types #57403

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 5 commits into from
Mar 27, 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
84 changes: 67 additions & 17 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6317,6 +6317,7 @@ export const enum ObjectFlags {
ContainsSpread = 1 << 21, // Object literal contains spread operation
ObjectRestType = 1 << 22, // Originates in object rest declaration
InstantiationExpressionType = 1 << 23, // Originates in instantiation expression
SingleSignatureType = 1 << 27, // A single signature type extracted from a potentially broader type
/** @internal */
IsClassInstanceClone = 1 << 24, // Type is a clone of a class instance type
// Flags that require TypeFlags.Object and ObjectFlags.Reference
Expand Down Expand Up @@ -6527,6 +6528,12 @@ export interface AnonymousType extends ObjectType {
instantiations?: Map<string, Type>; // Instantiations of generic type alias (undefined if non-generic)
}

/** @internal */
// A SingleSignatureType may have bespoke outer type parameters to handle free type variable inferences
export interface SingleSignatureType extends AnonymousType {
outerTypeParameters?: TypeParameter[];
}

/** @internal */
export interface InstantiationExpressionType extends AnonymousType {
node: NodeWithTypeArguments;
Expand Down Expand Up @@ -6812,6 +6819,8 @@ export interface Signature {
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
/** @internal */
instantiations?: Map<string, Signature>; // Generic signature instantiation cache
/** @internal */
implementationSignatureCache?: Signature; // Copy of the signature with fresh type parameters to use in checking the body of a potentially self-referential generic function (deferred)
}

export const enum IndexKind {
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7242,6 +7242,7 @@ declare namespace ts {
ContainsSpread = 2097152,
ObjectRestType = 4194304,
InstantiationExpressionType = 8388608,
SingleSignatureType = 134217728,
}
interface ObjectType extends Type {
objectFlags: ObjectFlags;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//// [tests/cases/compiler/genericCallWithinOwnBodyCastTypeParameterIdentity.ts] ////

//// [genericCallWithinOwnBodyCastTypeParameterIdentity.ts]
interface Thenable<Value> {
then<V>(
onFulfilled: (value: Value) => V | Thenable<V>,
): Thenable<V>;
}

const toThenable = <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) =>
(input: Input): Thenable<Result> => {
const result = fn(input)
return {
then<V>(onFulfilled: (value: Result) => V | Thenable<V>) {
return toThenable<V, Result>(onFulfilled)(result as Result)
}
};
}

const toThenableInferred = <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) =>
(input: Input): Thenable<Result> => {
const result = fn(input)
return {
then(onFulfilled) {
return toThenableInferred(onFulfilled)(result as Result)
}
};
}


//// [genericCallWithinOwnBodyCastTypeParameterIdentity.js]
"use strict";
var toThenable = function (fn) {
return function (input) {
var result = fn(input);
return {
then: function (onFulfilled) {
return toThenable(onFulfilled)(result);
}
};
};
};
var toThenableInferred = function (fn) {
return function (input) {
var result = fn(input);
return {
then: function (onFulfilled) {
return toThenableInferred(onFulfilled)(result);
}
};
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//// [tests/cases/compiler/genericCallWithinOwnBodyCastTypeParameterIdentity.ts] ////

=== genericCallWithinOwnBodyCastTypeParameterIdentity.ts ===
interface Thenable<Value> {
>Thenable : Symbol(Thenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 0))
>Value : Symbol(Value, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 19))

then<V>(
>then : Symbol(Thenable.then, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 27))
>V : Symbol(V, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 1, 9))

onFulfilled: (value: Value) => V | Thenable<V>,
>onFulfilled : Symbol(onFulfilled, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 1, 12))
>value : Symbol(value, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 2, 22))
>Value : Symbol(Value, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 19))
>V : Symbol(V, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 1, 9))
>Thenable : Symbol(Thenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 0))
>V : Symbol(V, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 1, 9))

): Thenable<V>;
>Thenable : Symbol(Thenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 0))
>V : Symbol(V, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 1, 9))
}

const toThenable = <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) =>
>toThenable : Symbol(toThenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 5))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 20))
>Input : Symbol(Input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 27))
>fn : Symbol(fn, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 35))
>input : Symbol(input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 40))
>Input : Symbol(Input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 27))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 20))
>Thenable : Symbol(Thenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 0))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 20))

(input: Input): Thenable<Result> => {
>input : Symbol(input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 7, 5))
>Input : Symbol(Input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 27))
>Thenable : Symbol(Thenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 0))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 20))

const result = fn(input)
>result : Symbol(result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 8, 13))
>fn : Symbol(fn, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 35))
>input : Symbol(input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 7, 5))

return {
then<V>(onFulfilled: (value: Result) => V | Thenable<V>) {
>then : Symbol(then, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 9, 16))
>V : Symbol(V, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 10, 17))
>onFulfilled : Symbol(onFulfilled, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 10, 20))
>value : Symbol(value, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 10, 34))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 20))
>V : Symbol(V, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 10, 17))
>Thenable : Symbol(Thenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 0))
>V : Symbol(V, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 10, 17))

return toThenable<V, Result>(onFulfilled)(result as Result)
>toThenable : Symbol(toThenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 5))
>V : Symbol(V, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 10, 17))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 20))
>onFulfilled : Symbol(onFulfilled, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 10, 20))
>result : Symbol(result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 8, 13))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 6, 20))
}
};
}

const toThenableInferred = <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) =>
>toThenableInferred : Symbol(toThenableInferred, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 5))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 28))
>Input : Symbol(Input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 35))
>fn : Symbol(fn, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 43))
>input : Symbol(input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 48))
>Input : Symbol(Input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 35))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 28))
>Thenable : Symbol(Thenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 0))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 28))

(input: Input): Thenable<Result> => {
>input : Symbol(input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 17, 5))
>Input : Symbol(Input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 35))
>Thenable : Symbol(Thenable, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 0, 0))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 28))

const result = fn(input)
>result : Symbol(result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 18, 13))
>fn : Symbol(fn, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 43))
>input : Symbol(input, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 17, 5))

return {
then(onFulfilled) {
>then : Symbol(then, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 19, 16))
>onFulfilled : Symbol(onFulfilled, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 20, 17))

return toThenableInferred(onFulfilled)(result as Result)
>toThenableInferred : Symbol(toThenableInferred, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 5))
>onFulfilled : Symbol(onFulfilled, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 20, 17))
>result : Symbol(result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 18, 13))
>Result : Symbol(Result, Decl(genericCallWithinOwnBodyCastTypeParameterIdentity.ts, 16, 28))
}
};
}

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

=== genericCallWithinOwnBodyCastTypeParameterIdentity.ts ===
interface Thenable<Value> {
then<V>(
>then : <V>(onFulfilled: (value: Value) => V | Thenable<V>) => Thenable<V>

onFulfilled: (value: Value) => V | Thenable<V>,
>onFulfilled : (value: Value) => V | Thenable<V>
>value : Value

): Thenable<V>;
}

const toThenable = <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) =>
>toThenable : <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) => (input: Input) => Thenable<Result>
><Result, Input>(fn: (input: Input) => Result | Thenable<Result>) => (input: Input): Thenable<Result> => { const result = fn(input) return { then<V>(onFulfilled: (value: Result) => V | Thenable<V>) { return toThenable<V, Result>(onFulfilled)(result as Result) } }; } : <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) => (input: Input) => Thenable<Result>
>fn : (input: Input) => Result | Thenable<Result>
>input : Input

(input: Input): Thenable<Result> => {
>(input: Input): Thenable<Result> => { const result = fn(input) return { then<V>(onFulfilled: (value: Result) => V | Thenable<V>) { return toThenable<V, Result>(onFulfilled)(result as Result) } }; } : (input: Input) => Thenable<Result>
>input : Input

const result = fn(input)
>result : Result | Thenable<Result>
>fn(input) : Result | Thenable<Result>
>fn : (input: Input) => Result | Thenable<Result>
>input : Input

return {
>{ then<V>(onFulfilled: (value: Result) => V | Thenable<V>) { return toThenable<V, Result>(onFulfilled)(result as Result) } } : { then<V>(onFulfilled: (value: Result) => V | Thenable<V>): Thenable<V>; }

then<V>(onFulfilled: (value: Result) => V | Thenable<V>) {
>then : <V>(onFulfilled: (value: Result) => V | Thenable<V>) => Thenable<V>
>onFulfilled : (value: Result) => V | Thenable<V>
>value : Result

return toThenable<V, Result>(onFulfilled)(result as Result)
>toThenable<V, Result>(onFulfilled)(result as Result) : Thenable<V>
>toThenable<V, Result>(onFulfilled) : (input: Result) => Thenable<V>
>toThenable : <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) => (input: Input) => Thenable<Result>
>onFulfilled : (value: Result) => V | Thenable<V>
>result as Result : Result
>result : Result | Thenable<Result>
}
};
}

const toThenableInferred = <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) =>
>toThenableInferred : <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) => (input: Input) => Thenable<Result>
><Result, Input>(fn: (input: Input) => Result | Thenable<Result>) => (input: Input): Thenable<Result> => { const result = fn(input) return { then(onFulfilled) { return toThenableInferred(onFulfilled)(result as Result) } }; } : <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) => (input: Input) => Thenable<Result>
>fn : (input: Input) => Result | Thenable<Result>
>input : Input

(input: Input): Thenable<Result> => {
>(input: Input): Thenable<Result> => { const result = fn(input) return { then(onFulfilled) { return toThenableInferred(onFulfilled)(result as Result) } }; } : (input: Input) => Thenable<Result>
>input : Input

const result = fn(input)
>result : Result | Thenable<Result>
>fn(input) : Result | Thenable<Result>
>fn : (input: Input) => Result | Thenable<Result>
>input : Input

return {
>{ then(onFulfilled) { return toThenableInferred(onFulfilled)(result as Result) } } : { then<V>(onFulfilled: (value: Result) => V | Thenable<V>): Thenable<V>; }

then(onFulfilled) {
>then : <V>(onFulfilled: (value: Result) => V | Thenable<V>) => Thenable<V>
>onFulfilled : (value: Result) => V | Thenable<V>

return toThenableInferred(onFulfilled)(result as Result)
>toThenableInferred(onFulfilled)(result as Result) : Thenable<V>
>toThenableInferred(onFulfilled) : (input: Result) => Thenable<V>
>toThenableInferred : <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) => (input: Input) => Thenable<Result>
>onFulfilled : (value: Result) => V | Thenable<V>
>result as Result : Result
>result : Result | Thenable<Result>
}
};
}

14 changes: 14 additions & 0 deletions tests/baselines/reference/nestedGenericSpreadInference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [tests/cases/compiler/nestedGenericSpreadInference.ts] ////

//// [nestedGenericSpreadInference.ts]
declare function wrap<X>(x: X): { x: X };
declare function call<A extends unknown[], T>(x: { x: (...args: A) => T }, ...args: A): T;

// This should be of type `number` - ideally, it also would not error.
const leak = call(wrap(<T>(x: T) => x), 1);


//// [nestedGenericSpreadInference.js]
"use strict";
// This should be of type `number` - ideally, it also would not error.
var leak = call(wrap(function (x) { return x; }), 1);
34 changes: 34 additions & 0 deletions tests/baselines/reference/nestedGenericSpreadInference.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/nestedGenericSpreadInference.ts] ////

=== nestedGenericSpreadInference.ts ===
declare function wrap<X>(x: X): { x: X };
>wrap : Symbol(wrap, Decl(nestedGenericSpreadInference.ts, 0, 0))
>X : Symbol(X, Decl(nestedGenericSpreadInference.ts, 0, 22))
>x : Symbol(x, Decl(nestedGenericSpreadInference.ts, 0, 25))
>X : Symbol(X, Decl(nestedGenericSpreadInference.ts, 0, 22))
>x : Symbol(x, Decl(nestedGenericSpreadInference.ts, 0, 33))
>X : Symbol(X, Decl(nestedGenericSpreadInference.ts, 0, 22))

declare function call<A extends unknown[], T>(x: { x: (...args: A) => T }, ...args: A): T;
>call : Symbol(call, Decl(nestedGenericSpreadInference.ts, 0, 41))
>A : Symbol(A, Decl(nestedGenericSpreadInference.ts, 1, 22))
>T : Symbol(T, Decl(nestedGenericSpreadInference.ts, 1, 42))
>x : Symbol(x, Decl(nestedGenericSpreadInference.ts, 1, 46))
>x : Symbol(x, Decl(nestedGenericSpreadInference.ts, 1, 50))
>args : Symbol(args, Decl(nestedGenericSpreadInference.ts, 1, 55))
>A : Symbol(A, Decl(nestedGenericSpreadInference.ts, 1, 22))
>T : Symbol(T, Decl(nestedGenericSpreadInference.ts, 1, 42))
>args : Symbol(args, Decl(nestedGenericSpreadInference.ts, 1, 74))
>A : Symbol(A, Decl(nestedGenericSpreadInference.ts, 1, 22))
>T : Symbol(T, Decl(nestedGenericSpreadInference.ts, 1, 42))

// This should be of type `number` - ideally, it also would not error.
const leak = call(wrap(<T>(x: T) => x), 1);
>leak : Symbol(leak, Decl(nestedGenericSpreadInference.ts, 4, 5))
>call : Symbol(call, Decl(nestedGenericSpreadInference.ts, 0, 41))
>wrap : Symbol(wrap, Decl(nestedGenericSpreadInference.ts, 0, 0))
>T : Symbol(T, Decl(nestedGenericSpreadInference.ts, 4, 24))
>x : Symbol(x, Decl(nestedGenericSpreadInference.ts, 4, 27))
>T : Symbol(T, Decl(nestedGenericSpreadInference.ts, 4, 24))
>x : Symbol(x, Decl(nestedGenericSpreadInference.ts, 4, 27))

27 changes: 27 additions & 0 deletions tests/baselines/reference/nestedGenericSpreadInference.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/compiler/nestedGenericSpreadInference.ts] ////

=== nestedGenericSpreadInference.ts ===
declare function wrap<X>(x: X): { x: X };
>wrap : <X>(x: X) => { x: X;}
>x : X
>x : X

declare function call<A extends unknown[], T>(x: { x: (...args: A) => T }, ...args: A): T;
>call : <A extends unknown[], T>(x: { x: (...args: A) => T; }, ...args: A) => T
>x : { x: (...args: A) => T; }
>x : (...args: A) => T
>args : A
>args : A

// This should be of type `number` - ideally, it also would not error.
const leak = call(wrap(<T>(x: T) => x), 1);
>leak : number
>call(wrap(<T>(x: T) => x), 1) : number
>call : <A extends unknown[], T>(x: { x: (...args: A) => T; }, ...args: A) => T
>wrap(<T>(x: T) => x) : { x: (x: A[0]) => A[0]; }
>wrap : <X>(x: X) => { x: X; }
><T>(x: T) => x : <T>(x: T) => T
>x : T
>x : T
>1 : 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @strict: true
interface Thenable<Value> {
then<V>(
onFulfilled: (value: Value) => V | Thenable<V>,
): Thenable<V>;
}

const toThenable = <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) =>
(input: Input): Thenable<Result> => {
const result = fn(input)
return {
then<V>(onFulfilled: (value: Result) => V | Thenable<V>) {
return toThenable<V, Result>(onFulfilled)(result as Result)
}
};
}

const toThenableInferred = <Result, Input>(fn: (input: Input) => Result | Thenable<Result>) =>
(input: Input): Thenable<Result> => {
const result = fn(input)
return {
then(onFulfilled) {
return toThenableInferred(onFulfilled)(result as Result)
}
};
}
6 changes: 6 additions & 0 deletions tests/cases/compiler/nestedGenericSpreadInference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @strict: true
declare function wrap<X>(x: X): { x: X };
declare function call<A extends unknown[], T>(x: { x: (...args: A) => T }, ...args: A): T;

// This should be of type `number` - ideally, it also would not error.
const leak = call(wrap(<T>(x: T) => x), 1);