Skip to content

Generic functions are never context sensitive #37811

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
Apr 6, 2020
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 @@ -14251,7 +14251,7 @@ namespace ts {

function hasContextSensitiveReturnExpression(node: FunctionLikeDeclaration) {
// TODO(anhans): A block should be context-sensitive if it has a context-sensitive return value.
return !getEffectiveReturnTypeNode(node) && !!node.body && node.body.kind !== SyntaxKind.Block && isContextSensitive(node.body);
return !node.typeParameters && !getEffectiveReturnTypeNode(node) && !!node.body && node.body.kind !== SyntaxKind.Block && isContextSensitive(node.body);
}

function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/genericFunctionsNotContextSensitive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [genericFunctionsNotContextSensitive.ts]
// Repro from #37110

const f = <F extends (...args: any[]) => <G>(x: G) => void>(_: F): F => _;

const a = f(<K extends string>(_: K) => _ => ({})); // <K extends string>(_: K) => <G>(_: G) => {}


//// [genericFunctionsNotContextSensitive.js]
"use strict";
// Repro from #37110
var f = function (_) { return _; };
var a = f(function (_) { return function (_) { return ({}); }; }); // <K extends string>(_: K) => <G>(_: G) => {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/genericFunctionsNotContextSensitive.ts ===
// Repro from #37110

const f = <F extends (...args: any[]) => <G>(x: G) => void>(_: F): F => _;
>f : Symbol(f, Decl(genericFunctionsNotContextSensitive.ts, 2, 5))
>F : Symbol(F, Decl(genericFunctionsNotContextSensitive.ts, 2, 11))
>args : Symbol(args, Decl(genericFunctionsNotContextSensitive.ts, 2, 22))
>G : Symbol(G, Decl(genericFunctionsNotContextSensitive.ts, 2, 42))
>x : Symbol(x, Decl(genericFunctionsNotContextSensitive.ts, 2, 45))
>G : Symbol(G, Decl(genericFunctionsNotContextSensitive.ts, 2, 42))
>_ : Symbol(_, Decl(genericFunctionsNotContextSensitive.ts, 2, 60))
>F : Symbol(F, Decl(genericFunctionsNotContextSensitive.ts, 2, 11))
>F : Symbol(F, Decl(genericFunctionsNotContextSensitive.ts, 2, 11))
>_ : Symbol(_, Decl(genericFunctionsNotContextSensitive.ts, 2, 60))

const a = f(<K extends string>(_: K) => _ => ({})); // <K extends string>(_: K) => <G>(_: G) => {}
>a : Symbol(a, Decl(genericFunctionsNotContextSensitive.ts, 4, 5))
>f : Symbol(f, Decl(genericFunctionsNotContextSensitive.ts, 2, 5))
>K : Symbol(K, Decl(genericFunctionsNotContextSensitive.ts, 4, 13))
>_ : Symbol(_, Decl(genericFunctionsNotContextSensitive.ts, 4, 31))
>K : Symbol(K, Decl(genericFunctionsNotContextSensitive.ts, 4, 13))
>_ : Symbol(_, Decl(genericFunctionsNotContextSensitive.ts, 4, 39))

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/genericFunctionsNotContextSensitive.ts ===
// Repro from #37110

const f = <F extends (...args: any[]) => <G>(x: G) => void>(_: F): F => _;
>f : <F extends (...args: any[]) => <G>(x: G) => void>(_: F) => F
><F extends (...args: any[]) => <G>(x: G) => void>(_: F): F => _ : <F extends (...args: any[]) => <G>(x: G) => void>(_: F) => F
>args : any[]
>x : G
>_ : F
>_ : F

const a = f(<K extends string>(_: K) => _ => ({})); // <K extends string>(_: K) => <G>(_: G) => {}
>a : <K extends string>(_: K) => <G>(_: G) => {}
>f(<K extends string>(_: K) => _ => ({})) : <K extends string>(_: K) => <G>(_: G) => {}
>f : <F extends (...args: any[]) => <G>(x: G) => void>(_: F) => F
><K extends string>(_: K) => _ => ({}) : <K extends string>(_: K) => <G>(_: G) => {}
>_ : K
>_ => ({}) : <G>(_: G) => {}
>_ : G
>({}) : {}
>{} : {}

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

// Repro from #37110

const f = <F extends (...args: any[]) => <G>(x: G) => void>(_: F): F => _;

const a = f(<K extends string>(_: K) => _ => ({})); // <K extends string>(_: K) => <G>(_: G) => {}