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

Fixed instantiation expression type param leak in body-less arrows #61054

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 10 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20259,16 +20259,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const firstIdentifier = getFirstIdentifier(entityName);
if (!isThisIdentifier(firstIdentifier)) { // Don't attempt to analyze typeof this.xxx
const firstIdentifierSymbol = getResolvedSymbol(firstIdentifier);
const tpDeclaration = tp.symbol.declarations![0]; // There is exactly one declaration, otherwise `containsReference` is not called
const tpScope = tpDeclaration.kind === SyntaxKind.TypeParameter ? tpDeclaration.parent : // Type parameter is a regular type parameter, e.g. foo<T>
tp.isThisType ? tpDeclaration : // Type parameter is the this type, and its declaration is the class declaration.
undefined; // Type parameter's declaration was unrecognized, e.g. comes from JSDoc annotation.
const tpScope = getTypeParameterScope();
if (firstIdentifierSymbol.declarations && tpScope) {
return some(firstIdentifierSymbol.declarations, idDecl => isNodeDescendantOf(idDecl, tpScope)) ||
some((node as TypeQueryNode).typeArguments, containsReference);
}
}
return true;
case SyntaxKind.ExpressionWithTypeArguments:
return isNodeDescendantOf(node, getTypeParameterScope());
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
return !(node as FunctionLikeDeclaration).type && !!(node as FunctionLikeDeclaration).body ||
Expand All @@ -20278,6 +20277,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
return !!forEachChild(node, containsReference);
}

function getTypeParameterScope() {
const tpDeclaration = tp.symbol.declarations![0]; // There is exactly one declaration, otherwise `containsReference` is not called
return tpDeclaration.kind === SyntaxKind.TypeParameter ? tpDeclaration.parent : // Type parameter is a regular type parameter, e.g. foo<T>
tp.isThisType ? tpDeclaration : // Type parameter is the this type, and its declaration is the class declaration.
undefined; // Type parameter's declaration was unrecognized, e.g. comes from JSDoc annotation.
}
}

function getHomomorphicTypeVariable(type: MappedType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//// [tests/cases/compiler/instantiationExpressionNoTypeParameterLeak1.ts] ////

=== instantiationExpressionNoTypeParameterLeak1.ts ===
// https://github.com/microsoft/TypeScript/issues/61041

export const test1 = <X,>(g: <A>(x: X) => X) => g<string>;
>test1 : Symbol(test1, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 12))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 22))
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 26))
>A : Symbol(A, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 30))
>x : Symbol(x, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 33))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 22))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 22))
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 26))

export const output1 = test1<number>((y: number) => 1);
>output1 : Symbol(output1, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 3, 12))
>test1 : Symbol(test1, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 2, 12))
>y : Symbol(y, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 3, 38))

output1(1);
>output1 : Symbol(output1, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 3, 12))

export function test2<X>(g: <A>(x: X) => X) {
>test2 : Symbol(test2, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 4, 11))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 6, 22))
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 6, 25))
>A : Symbol(A, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 6, 29))
>x : Symbol(x, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 6, 32))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 6, 22))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 6, 22))

return g<string>;
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 6, 25))
}
export const output2 = test2<number>((y: number) => 1);
>output2 : Symbol(output2, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 9, 12))
>test2 : Symbol(test2, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 4, 11))
>y : Symbol(y, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 9, 38))

output2(1);
>output2 : Symbol(output2, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 9, 12))

export const test3 = <X,>(g: <A>() => (x: X) => X) => g<string>();
>test3 : Symbol(test3, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 12))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 22))
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 26))
>A : Symbol(A, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 30))
>x : Symbol(x, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 39))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 22))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 22))
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 26))

export const output3 = test3<number>(() => (y: number) => 1);
>output3 : Symbol(output3, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 13, 12))
>test3 : Symbol(test3, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 12, 12))
>y : Symbol(y, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 13, 44))

output3(1);
>output3 : Symbol(output3, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 13, 12))

export function test4<X>(g: <A>() => (x: X) => X) {
>test4 : Symbol(test4, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 14, 11))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 16, 22))
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 16, 25))
>A : Symbol(A, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 16, 29))
>x : Symbol(x, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 16, 38))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 16, 22))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 16, 22))

return g<string>();
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 16, 25))
}
export const output4 = test4<number>(() => (y: number) => 1);
>output4 : Symbol(output4, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 19, 12))
>test4 : Symbol(test4, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 14, 11))
>y : Symbol(y, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 19, 44))

output4(1);
>output4 : Symbol(output4, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 19, 12))

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

=== instantiationExpressionNoTypeParameterLeak1.ts ===
// https://github.com/microsoft/TypeScript/issues/61041

export const test1 = <X,>(g: <A>(x: X) => X) => g<string>;
>test1 : <X>(g: <A>(x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
><X,>(g: <A>(x: X) => X) => g<string> : <X>(g: <A>(x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
>g : <A>(x: X) => X
> : ^ ^^ ^^ ^^^^^
>x : X
> : ^
>g<string> : (x: X) => X
> : ^ ^^ ^^^^^
>g : <A>(x: X) => X
> : ^ ^^ ^^ ^^^^^

export const output1 = test1<number>((y: number) => 1);
>output1 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test1<number>((y: number) => 1) : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test1 : <X>(g: <A>(x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
>(y: number) => 1 : (y: number) => number
> : ^ ^^ ^^^^^^^^^^^
>y : number
> : ^^^^^^
>1 : 1
> : ^

output1(1);
>output1(1) : number
> : ^^^^^^
>output1 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>1 : 1
> : ^

export function test2<X>(g: <A>(x: X) => X) {
>test2 : <X>(g: <A>(x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
>g : <A>(x: X) => X
> : ^ ^^ ^^ ^^^^^
>x : X
> : ^

return g<string>;
>g<string> : (x: X) => X
> : ^ ^^ ^^^^^
>g : <A>(x: X) => X
> : ^ ^^ ^^ ^^^^^
}
export const output2 = test2<number>((y: number) => 1);
>output2 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test2<number>((y: number) => 1) : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test2 : <X>(g: <A>(x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
>(y: number) => 1 : (y: number) => number
> : ^ ^^ ^^^^^^^^^^^
>y : number
> : ^^^^^^
>1 : 1
> : ^

output2(1);
>output2(1) : number
> : ^^^^^^
>output2 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>1 : 1
> : ^

export const test3 = <X,>(g: <A>() => (x: X) => X) => g<string>();
>test3 : <X>(g: <A>() => (x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
><X,>(g: <A>() => (x: X) => X) => g<string>() : <X>(g: <A>() => (x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
>g : <A>() => (x: X) => X
> : ^ ^^^^^^^
>x : X
> : ^
>g<string>() : (x: X) => X
> : ^ ^^ ^^^^^
>g : <A>() => (x: X) => X
> : ^ ^^^^^^^

export const output3 = test3<number>(() => (y: number) => 1);
>output3 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test3<number>(() => (y: number) => 1) : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test3 : <X>(g: <A>() => (x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
>() => (y: number) => 1 : () => (y: number) => number
> : ^^^^^^^ ^^ ^^^^^^^^^^^
>(y: number) => 1 : (y: number) => number
> : ^ ^^ ^^^^^^^^^^^
>y : number
> : ^^^^^^
>1 : 1
> : ^

output3(1);
>output3(1) : number
> : ^^^^^^
>output3 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>1 : 1
> : ^

export function test4<X>(g: <A>() => (x: X) => X) {
>test4 : <X>(g: <A>() => (x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
>g : <A>() => (x: X) => X
> : ^ ^^^^^^^
>x : X
> : ^

return g<string>();
>g<string>() : (x: X) => X
> : ^ ^^ ^^^^^
>g : <A>() => (x: X) => X
> : ^ ^^^^^^^
}
export const output4 = test4<number>(() => (y: number) => 1);
>output4 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test4<number>(() => (y: number) => 1) : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test4 : <X>(g: <A>() => (x: X) => X) => (x: X) => X
> : ^ ^^ ^^ ^^^^^^ ^^ ^^^^^
>() => (y: number) => 1 : () => (y: number) => number
> : ^^^^^^^ ^^ ^^^^^^^^^^^
>(y: number) => 1 : (y: number) => number
> : ^ ^^ ^^^^^^^^^^^
>y : number
> : ^^^^^^
>1 : 1
> : ^

output4(1);
>output4(1) : number
> : ^^^^^^
>output4 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>1 : 1
> : ^

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

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

export const test1 = <X,>(g: <A>(x: X) => X) => g<string>;
export const output1 = test1<number>((y: number) => 1);
output1(1);
Comment on lines +6 to +8
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 leaked type param was filtered out by the call to isTypeParameterPossiblyReferenced in getObjectTypeInstantiation. It was crucial for the bug that a body-less arrow function returned the instantiation expression. With a block function body isTypeParameterPossiblyReferenced would already return true.


export function test2<X>(g: <A>(x: X) => X) {
return g<string>;
}
export const output2 = test2<number>((y: number) => 1);
output2(1);

export const test3 = <X,>(g: <A>() => (x: X) => X) => g<string>();
export const output3 = test3<number>(() => (y: number) => 1);
output3(1);

export function test4<X>(g: <A>() => (x: X) => X) {
return g<string>();
}
export const output4 = test4<number>(() => (y: number) => 1);
output4(1);
Loading