Skip to content

Fixed anyFunctionType leak #61980

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 1 commit 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
10 changes: 6 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39498,7 +39498,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getTypeOfSymbol(getSymbolOfDeclaration(node));
}

function contextuallyCheckFunctionExpressionOrObjectLiteralMethod(node: FunctionExpression | ArrowFunction | MethodDeclaration, checkMode?: CheckMode) {
function contextuallyCheckFunctionExpressionOrObjectLiteralMethod(node: FunctionExpression | ArrowFunction | MethodDeclaration, checkMode = CheckMode.Normal) {
const links = getNodeLinks(node);
// Check if function expression is contextually typed and assign parameter types if so.
if (!(links.flags & NodeCheckFlags.ContextChecked)) {
Expand All @@ -39516,7 +39516,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (contextualSignature) {
const inferenceContext = getInferenceContext(node);
let instantiatedContextualSignature: Signature | undefined;
if (checkMode && checkMode & CheckMode.Inferential) {
if (checkMode & CheckMode.Inferential) {
inferFromAnnotatedParametersAndReturn(signature, contextualSignature, inferenceContext!);
const restType = getEffectiveRestType(contextualSignature);
if (restType && restType.flags & TypeFlags.TypeParameter) {
Expand All @@ -39534,12 +39534,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
else if (contextualSignature && !node.typeParameters && contextualSignature.parameters.length > node.parameters.length) {
const inferenceContext = getInferenceContext(node);
if (checkMode && checkMode & CheckMode.Inferential) {
if (checkMode & CheckMode.Inferential) {
inferFromAnnotatedParametersAndReturn(signature, contextualSignature, inferenceContext!);
}
}
if (contextualSignature && !getReturnTypeFromAnnotation(node) && !signature.resolvedReturnType) {
const returnType = getReturnTypeFromBody(node, checkMode);
// `.resolvedReturnType` is cached indefinitely so the return type here has to be computed without `CheckMode.SkipContextSensitive`
// otherwise `anyFunctionType` could leak as part of the computed (and cached) return type
const returnType = getReturnTypeFromBody(node, checkMode & ~CheckMode.SkipContextSensitive);
if (!signature.resolvedReturnType) {
signature.resolvedReturnType = returnType;
}
Expand Down
115 changes: 115 additions & 0 deletions tests/baselines/reference/contextualTypingGenericFunction2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
contextualTypingGenericFunction2.ts(10,3): error TS2322: Type '<T>(params: T) => (a: boolean, b: unknown) => 1 | 0' is not assignable to type '(params: T) => (context: number, params: T) => number'.
Type '(a: boolean, b: unknown) => 1 | 0' is not assignable to type '(context: number, params: T) => number'.
Types of parameters 'a' and 'context' are incompatible.
Type 'number' is not assignable to type 'boolean'.
contextualTypingGenericFunction2.ts(18,3): error TS2322: Type '<T>(params: T) => (a: number, b: unknown) => boolean' is not assignable to type '(params: T) => (context: number, params: T) => number'.
Call signature return types '(a: number, b: unknown) => boolean' and '(context: number, params: T) => number' are incompatible.
Type 'boolean' is not assignable to type 'number'.
contextualTypingGenericFunction2.ts(26,3): error TS2322: Type '<T>(params: T) => (a: number, b: unknown) => boolean' is not assignable to type '(params: T) => (context: number, params: T) => number'.
Call signature return types '(a: number, b: unknown) => boolean' and '(context: number, params: T) => number' are incompatible.
Type 'boolean' is not assignable to type 'number'.
contextualTypingGenericFunction2.ts(39,3): error TS2322: Type '<T>(params: T) => (a: boolean, b: unknown) => 1 | 0' is not assignable to type '(params: unknown) => (context: number, params: unknown) => number'.
Type '(a: boolean, b: unknown) => 1 | 0' is not assignable to type '(context: number, params: unknown) => number'.
Types of parameters 'a' and 'context' are incompatible.
Type 'number' is not assignable to type 'boolean'.
contextualTypingGenericFunction2.ts(47,3): error TS2322: Type '<T>(params: T) => (a: number, b: unknown) => boolean' is not assignable to type '(params: T) => (context: number, params: T) => number'.
Call signature return types '(a: number, b: unknown) => boolean' and '(context: number, params: T) => number' are incompatible.
Type 'boolean' is not assignable to type 'number'.
contextualTypingGenericFunction2.ts(55,3): error TS2322: Type '<T>(params: T) => (a: number, b: unknown) => boolean' is not assignable to type '(params: T) => (context: number, params: T) => number'.
Call signature return types '(a: number, b: unknown) => boolean' and '(context: number, params: T) => number' are incompatible.
Type 'boolean' is not assignable to type 'number'.


==== contextualTypingGenericFunction2.ts (6 errors) ====
// https://github.com/microsoft/TypeScript/issues/61979

declare function fn<P>(config: {
callback: (params: P) => (context: number, params: P) => number;
unrelated?: (arg: string) => void;
}): (params: P) => number;

// should error
export const result1 = fn({
callback: <T,>(params: T) => {
~~~~~~~~
!!! error TS2322: Type '<T>(params: T) => (a: boolean, b: unknown) => 1 | 0' is not assignable to type '(params: T) => (context: number, params: T) => number'.
!!! error TS2322: Type '(a: boolean, b: unknown) => 1 | 0' is not assignable to type '(context: number, params: T) => number'.
!!! error TS2322: Types of parameters 'a' and 'context' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
!!! related TS6500 contextualTypingGenericFunction2.ts:4:3: The expected type comes from property 'callback' which is declared here on type '{ callback: (params: T) => (context: number, params: T) => number; unrelated?: ((arg: string) => void) | undefined; }'
return (a: boolean, b) => (a ? 1 : 0);
},
unrelated: (_) => {},
});

// should error
export const result2 = fn({
callback: <T,>(params: T) => {
~~~~~~~~
!!! error TS2322: Type '<T>(params: T) => (a: number, b: unknown) => boolean' is not assignable to type '(params: T) => (context: number, params: T) => number'.
!!! error TS2322: Call signature return types '(a: number, b: unknown) => boolean' and '(context: number, params: T) => number' are incompatible.
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
!!! related TS6500 contextualTypingGenericFunction2.ts:4:3: The expected type comes from property 'callback' which is declared here on type '{ callback: (params: T) => (context: number, params: T) => number; unrelated?: ((arg: string) => void) | undefined; }'
return (a, b): boolean => true;
},
unrelated: (_) => {},
});

// should error
export const result3 = fn({
callback: <T,>(params: T) => {
~~~~~~~~
!!! error TS2322: Type '<T>(params: T) => (a: number, b: unknown) => boolean' is not assignable to type '(params: T) => (context: number, params: T) => number'.
!!! error TS2322: Call signature return types '(a: number, b: unknown) => boolean' and '(context: number, params: T) => number' are incompatible.
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
!!! related TS6500 contextualTypingGenericFunction2.ts:4:3: The expected type comes from property 'callback' which is declared here on type '{ callback: (params: T) => (context: number, params: T) => number; unrelated?: ((arg: string) => void) | undefined; }'
return (a, b) => true;
},
unrelated: (_) => {},
});

declare function fn2<P>(config: {
callback: (params: P) => (context: number, params: P) => number;
unrelated?: (arg: string) => void;
}): any;

// should error
export const result4 = fn2({
callback: <T,>(params: T) => {
~~~~~~~~
!!! error TS2322: Type '<T>(params: T) => (a: boolean, b: unknown) => 1 | 0' is not assignable to type '(params: unknown) => (context: number, params: unknown) => number'.
!!! error TS2322: Type '(a: boolean, b: unknown) => 1 | 0' is not assignable to type '(context: number, params: unknown) => number'.
!!! error TS2322: Types of parameters 'a' and 'context' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
!!! related TS6500 contextualTypingGenericFunction2.ts:33:3: The expected type comes from property 'callback' which is declared here on type '{ callback: (params: unknown) => (context: number, params: unknown) => number; unrelated?: ((arg: string) => void) | undefined; }'
return (a: boolean, b) => (a ? 1 : 0);
},
unrelated: (_) => {},
});

// should error
export const result5 = fn({
callback: <T,>(params: T) => {
~~~~~~~~
!!! error TS2322: Type '<T>(params: T) => (a: number, b: unknown) => boolean' is not assignable to type '(params: T) => (context: number, params: T) => number'.
!!! error TS2322: Call signature return types '(a: number, b: unknown) => boolean' and '(context: number, params: T) => number' are incompatible.
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
!!! related TS6500 contextualTypingGenericFunction2.ts:4:3: The expected type comes from property 'callback' which is declared here on type '{ callback: (params: T) => (context: number, params: T) => number; unrelated?: ((arg: string) => void) | undefined; }'
return (a, b): boolean => true;
},
unrelated: (_) => {},
});

// should error
export const result6 = fn({
callback: <T,>(params: T) => {
~~~~~~~~
!!! error TS2322: Type '<T>(params: T) => (a: number, b: unknown) => boolean' is not assignable to type '(params: T) => (context: number, params: T) => number'.
!!! error TS2322: Call signature return types '(a: number, b: unknown) => boolean' and '(context: number, params: T) => number' are incompatible.
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
!!! related TS6500 contextualTypingGenericFunction2.ts:4:3: The expected type comes from property 'callback' which is declared here on type '{ callback: (params: T) => (context: number, params: T) => number; unrelated?: ((arg: string) => void) | undefined; }'
return (a, b) => true;
},
unrelated: (_) => {},
});

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

=== contextualTypingGenericFunction2.ts ===
// https://github.com/microsoft/TypeScript/issues/61979

declare function fn<P>(config: {
>fn : Symbol(fn, Decl(contextualTypingGenericFunction2.ts, 0, 0))
>P : Symbol(P, Decl(contextualTypingGenericFunction2.ts, 2, 20))
>config : Symbol(config, Decl(contextualTypingGenericFunction2.ts, 2, 23))

callback: (params: P) => (context: number, params: P) => number;
>callback : Symbol(callback, Decl(contextualTypingGenericFunction2.ts, 2, 32))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 3, 13))
>P : Symbol(P, Decl(contextualTypingGenericFunction2.ts, 2, 20))
>context : Symbol(context, Decl(contextualTypingGenericFunction2.ts, 3, 28))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 3, 44))
>P : Symbol(P, Decl(contextualTypingGenericFunction2.ts, 2, 20))

unrelated?: (arg: string) => void;
>unrelated : Symbol(unrelated, Decl(contextualTypingGenericFunction2.ts, 3, 66))
>arg : Symbol(arg, Decl(contextualTypingGenericFunction2.ts, 4, 15))

}): (params: P) => number;
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 5, 5))
>P : Symbol(P, Decl(contextualTypingGenericFunction2.ts, 2, 20))

// should error
export const result1 = fn({
>result1 : Symbol(result1, Decl(contextualTypingGenericFunction2.ts, 8, 12))
>fn : Symbol(fn, Decl(contextualTypingGenericFunction2.ts, 0, 0))

callback: <T,>(params: T) => {
>callback : Symbol(callback, Decl(contextualTypingGenericFunction2.ts, 8, 27))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 9, 13))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 9, 17))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 9, 13))

return (a: boolean, b) => (a ? 1 : 0);
>a : Symbol(a, Decl(contextualTypingGenericFunction2.ts, 10, 12))
>b : Symbol(b, Decl(contextualTypingGenericFunction2.ts, 10, 23))
>a : Symbol(a, Decl(contextualTypingGenericFunction2.ts, 10, 12))

},
unrelated: (_) => {},
>unrelated : Symbol(unrelated, Decl(contextualTypingGenericFunction2.ts, 11, 4))
>_ : Symbol(_, Decl(contextualTypingGenericFunction2.ts, 12, 14))

});

// should error
export const result2 = fn({
>result2 : Symbol(result2, Decl(contextualTypingGenericFunction2.ts, 16, 12))
>fn : Symbol(fn, Decl(contextualTypingGenericFunction2.ts, 0, 0))

callback: <T,>(params: T) => {
>callback : Symbol(callback, Decl(contextualTypingGenericFunction2.ts, 16, 27))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 17, 13))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 17, 17))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 17, 13))

return (a, b): boolean => true;
>a : Symbol(a, Decl(contextualTypingGenericFunction2.ts, 18, 12))
>b : Symbol(b, Decl(contextualTypingGenericFunction2.ts, 18, 14))

},
unrelated: (_) => {},
>unrelated : Symbol(unrelated, Decl(contextualTypingGenericFunction2.ts, 19, 4))
>_ : Symbol(_, Decl(contextualTypingGenericFunction2.ts, 20, 14))

});

// should error
export const result3 = fn({
>result3 : Symbol(result3, Decl(contextualTypingGenericFunction2.ts, 24, 12))
>fn : Symbol(fn, Decl(contextualTypingGenericFunction2.ts, 0, 0))

callback: <T,>(params: T) => {
>callback : Symbol(callback, Decl(contextualTypingGenericFunction2.ts, 24, 27))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 25, 13))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 25, 17))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 25, 13))

return (a, b) => true;
>a : Symbol(a, Decl(contextualTypingGenericFunction2.ts, 26, 12))
>b : Symbol(b, Decl(contextualTypingGenericFunction2.ts, 26, 14))

},
unrelated: (_) => {},
>unrelated : Symbol(unrelated, Decl(contextualTypingGenericFunction2.ts, 27, 4))
>_ : Symbol(_, Decl(contextualTypingGenericFunction2.ts, 28, 14))

});

declare function fn2<P>(config: {
>fn2 : Symbol(fn2, Decl(contextualTypingGenericFunction2.ts, 29, 3))
>P : Symbol(P, Decl(contextualTypingGenericFunction2.ts, 31, 21))
>config : Symbol(config, Decl(contextualTypingGenericFunction2.ts, 31, 24))

callback: (params: P) => (context: number, params: P) => number;
>callback : Symbol(callback, Decl(contextualTypingGenericFunction2.ts, 31, 33))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 32, 13))
>P : Symbol(P, Decl(contextualTypingGenericFunction2.ts, 31, 21))
>context : Symbol(context, Decl(contextualTypingGenericFunction2.ts, 32, 28))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 32, 44))
>P : Symbol(P, Decl(contextualTypingGenericFunction2.ts, 31, 21))

unrelated?: (arg: string) => void;
>unrelated : Symbol(unrelated, Decl(contextualTypingGenericFunction2.ts, 32, 66))
>arg : Symbol(arg, Decl(contextualTypingGenericFunction2.ts, 33, 15))

}): any;

// should error
export const result4 = fn2({
>result4 : Symbol(result4, Decl(contextualTypingGenericFunction2.ts, 37, 12))
>fn2 : Symbol(fn2, Decl(contextualTypingGenericFunction2.ts, 29, 3))

callback: <T,>(params: T) => {
>callback : Symbol(callback, Decl(contextualTypingGenericFunction2.ts, 37, 28))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 38, 13))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 38, 17))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 38, 13))

return (a: boolean, b) => (a ? 1 : 0);
>a : Symbol(a, Decl(contextualTypingGenericFunction2.ts, 39, 12))
>b : Symbol(b, Decl(contextualTypingGenericFunction2.ts, 39, 23))
>a : Symbol(a, Decl(contextualTypingGenericFunction2.ts, 39, 12))

},
unrelated: (_) => {},
>unrelated : Symbol(unrelated, Decl(contextualTypingGenericFunction2.ts, 40, 4))
>_ : Symbol(_, Decl(contextualTypingGenericFunction2.ts, 41, 14))

});

// should error
export const result5 = fn({
>result5 : Symbol(result5, Decl(contextualTypingGenericFunction2.ts, 45, 12))
>fn : Symbol(fn, Decl(contextualTypingGenericFunction2.ts, 0, 0))

callback: <T,>(params: T) => {
>callback : Symbol(callback, Decl(contextualTypingGenericFunction2.ts, 45, 27))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 46, 13))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 46, 17))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 46, 13))

return (a, b): boolean => true;
>a : Symbol(a, Decl(contextualTypingGenericFunction2.ts, 47, 12))
>b : Symbol(b, Decl(contextualTypingGenericFunction2.ts, 47, 14))

},
unrelated: (_) => {},
>unrelated : Symbol(unrelated, Decl(contextualTypingGenericFunction2.ts, 48, 4))
>_ : Symbol(_, Decl(contextualTypingGenericFunction2.ts, 49, 14))

});

// should error
export const result6 = fn({
>result6 : Symbol(result6, Decl(contextualTypingGenericFunction2.ts, 53, 12))
>fn : Symbol(fn, Decl(contextualTypingGenericFunction2.ts, 0, 0))

callback: <T,>(params: T) => {
>callback : Symbol(callback, Decl(contextualTypingGenericFunction2.ts, 53, 27))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 54, 13))
>params : Symbol(params, Decl(contextualTypingGenericFunction2.ts, 54, 17))
>T : Symbol(T, Decl(contextualTypingGenericFunction2.ts, 54, 13))

return (a, b) => true;
>a : Symbol(a, Decl(contextualTypingGenericFunction2.ts, 55, 12))
>b : Symbol(b, Decl(contextualTypingGenericFunction2.ts, 55, 14))

},
unrelated: (_) => {},
>unrelated : Symbol(unrelated, Decl(contextualTypingGenericFunction2.ts, 56, 4))
>_ : Symbol(_, Decl(contextualTypingGenericFunction2.ts, 57, 14))

});

Loading