Skip to content

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

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 29 additions & 15 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20254,21 +20254,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.Identifier:
return !tp.isThisType && isPartOfTypeNode(node) && maybeTypeParameterReference(node) &&
getTypeFromTypeNodeWorker(node as TypeNode) === tp; // use worker because we're looking for === equality
case SyntaxKind.TypeQuery:
const entityName = (node as TypeQueryNode).exprName;
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.
if (firstIdentifierSymbol.declarations && tpScope) {
return some(firstIdentifierSymbol.declarations, idDecl => isNodeDescendantOf(idDecl, tpScope)) ||
some((node as TypeQueryNode).typeArguments, containsReference);
}
}
return true;
case SyntaxKind.TypeQuery: {
const { exprName, typeArguments } = node as TypeQueryNode;
return entityNameWithTypeArgumentsContainsReference(exprName, typeArguments);
}
case SyntaxKind.ExpressionWithTypeArguments: {
const { expression, typeArguments } = node as ExpressionWithTypeArguments;
return isEntityName(expression) ? entityNameWithTypeArgumentsContainsReference(expression, typeArguments) : isNodeDescendantOf(node, getTypeParameterScope());
}
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
return !(node as FunctionLikeDeclaration).type && !!(node as FunctionLikeDeclaration).body ||
Expand All @@ -20278,6 +20271,27 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
return !!forEachChild(node, containsReference);
}

function entityNameWithTypeArgumentsContainsReference(entityName: EntityName, typeArguments: NodeArray<TypeNode> | undefined) {
const firstIdentifier = getFirstIdentifier(entityName);
if (isThisIdentifier(firstIdentifier)) { // Don't attempt to analyze typeof this.xxx
return true;
}
const firstIdentifierSymbol = getResolvedSymbol(firstIdentifier);
const tpScope = getTypeParameterScope();
if (!firstIdentifierSymbol.declarations || !tpScope) {
return true;
}
return some(firstIdentifierSymbol.declarations, idDecl => isNodeDescendantOf(idDecl, tpScope)) ||
some(typeArguments, 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,118 @@
//// [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))

export declare function test5<X>(g: <A>(x: X) => X): typeof g<string>;
>test5 : Symbol(test5, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 20, 11))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 22, 30))
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 22, 33))
>A : Symbol(A, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 22, 37))
>x : Symbol(x, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 22, 40))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 22, 30))
>X : Symbol(X, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 22, 30))
>g : Symbol(g, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 22, 33))

export const output5 = test5<number>((y: number) => 1);
>output5 : Symbol(output5, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 23, 12))
>test5 : Symbol(test5, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 20, 11))
>y : Symbol(y, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 23, 38))

output5(1);
>output5 : Symbol(output5, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 23, 12))

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

export const output6 = test6<number>((y: number) => 1);
>output6 : Symbol(output6, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 27, 12))
>test6 : Symbol(test6, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 26, 12))
>y : Symbol(y, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 27, 38))

output6(1);
>output6 : Symbol(output6, Decl(instantiationExpressionNoTypeParameterLeak1.ts, 27, 12))

Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
//// [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
> : ^

export declare function test5<X>(g: <A>(x: X) => X): typeof g<string>;
>test5 : <X>(g: <A>(x: X) => X) => typeof g<string>
> : ^ ^^ ^^ ^^^^^
>g : <A>(x: X) => X
> : ^ ^^ ^^ ^^^^^
>x : X
> : ^
>g : <A>(x: X) => X
> : ^ ^^ ^^ ^^^^^

export const output5 = test5<number>((y: number) => 1);
>output5 : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test5<number>((y: number) => 1) : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>test5 : <X>(g: <A>(x: X) => X) => typeof g<string>
> : ^ ^^ ^^ ^^^^^
>(y: number) => 1 : (y: number) => number
> : ^ ^^ ^^^^^^^^^^^
>y : number
> : ^^^^^^
>1 : 1
> : ^

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

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

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

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

Loading
Loading