-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Avoid inferring return and yield types from unreachable statements #55601
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
Andarist
wants to merge
11
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/avoid-aggregating-unreachable-returns-and-yields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fa99df8
Avoid inferring return and yield types from unreachable statements
Andarist 53d3491
Merge remote-tracking branch 'origin/main' into fix/avoid-aggregating…
Andarist f04bc03
dont gate it on allowUnreachableCode
Andarist 341a04b
add asserts-based tests
Andarist 74a1c06
use `isReachableFlowNode` check
Andarist 22d5da7
fix `typeFromSingleReturnExpression`
Andarist 8835aae
update baselines
Andarist c60318e
handle getters returning never
Andarist ce446e8
update editorServices
Andarist 90d7e6b
require explicit `never` annotation on getter
Andarist 0d72373
Merge remote-tracking branch 'origin/main' into fix/avoid-aggregating…
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,7 +82,7 @@ class C { | |
| } | ||
| } | ||
| label() { | ||
| >label : () => number | ||
| >label : () => void | ||
|
|
||
| for(;;) { | ||
| label: var x = 1 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/baselines/reference/unreachableReturnStatementsVsInferredReturnTypes.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //// [tests/cases/compiler/unreachableReturnStatementsVsInferredReturnTypes.ts] //// | ||
|
|
||
| //// [unreachableReturnStatementsVsInferredReturnTypes.ts] | ||
| export function g() { | ||
| let x; | ||
| x = 1; | ||
| return x; | ||
| return x; | ||
| } | ||
|
|
||
| export function h() { | ||
| return 1; | ||
| let y; | ||
| y = 1; | ||
| return y; | ||
| } | ||
|
|
||
| export function i() { | ||
| let x: string | number | boolean; | ||
| x = 1; | ||
| return x; | ||
|
|
||
| x = "foo"; | ||
| return x; | ||
| } | ||
|
|
||
|
|
||
| //// [unreachableReturnStatementsVsInferredReturnTypes.js] | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.i = exports.h = exports.g = void 0; | ||
| function g() { | ||
| var x; | ||
| x = 1; | ||
| return x; | ||
| return x; | ||
| } | ||
| exports.g = g; | ||
| function h() { | ||
| return 1; | ||
| var y; | ||
| y = 1; | ||
| return y; | ||
| } | ||
| exports.h = h; | ||
| function i() { | ||
| var x; | ||
| x = 1; | ||
| return x; | ||
| x = "foo"; | ||
| return x; | ||
| } | ||
| exports.i = i; | ||
|
|
||
|
|
||
| //// [unreachableReturnStatementsVsInferredReturnTypes.d.ts] | ||
| export declare function g(): number; | ||
| export declare function h(): number; | ||
| export declare function i(): number; |
52 changes: 52 additions & 0 deletions
52
tests/baselines/reference/unreachableReturnStatementsVsInferredReturnTypes.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| //// [tests/cases/compiler/unreachableReturnStatementsVsInferredReturnTypes.ts] //// | ||
|
|
||
| === unreachableReturnStatementsVsInferredReturnTypes.ts === | ||
| export function g() { | ||
| >g : Symbol(g, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 0, 0)) | ||
|
|
||
| let x; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 1, 5)) | ||
|
|
||
| x = 1; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 1, 5)) | ||
|
|
||
| return x; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 1, 5)) | ||
|
|
||
| return x; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 1, 5)) | ||
| } | ||
|
|
||
| export function h() { | ||
| >h : Symbol(h, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 5, 1)) | ||
|
|
||
| return 1; | ||
| let y; | ||
| >y : Symbol(y, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 9, 5)) | ||
|
|
||
| y = 1; | ||
| >y : Symbol(y, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 9, 5)) | ||
|
|
||
| return y; | ||
| >y : Symbol(y, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 9, 5)) | ||
| } | ||
|
|
||
| export function i() { | ||
| >i : Symbol(i, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 12, 1)) | ||
|
|
||
| let x: string | number | boolean; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 15, 5)) | ||
|
|
||
| x = 1; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 15, 5)) | ||
|
|
||
| return x; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 15, 5)) | ||
|
|
||
| x = "foo"; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 15, 5)) | ||
|
|
||
| return x; | ||
| >x : Symbol(x, Decl(unreachableReturnStatementsVsInferredReturnTypes.ts, 15, 5)) | ||
| } | ||
|
|
62 changes: 62 additions & 0 deletions
62
tests/baselines/reference/unreachableReturnStatementsVsInferredReturnTypes.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| //// [tests/cases/compiler/unreachableReturnStatementsVsInferredReturnTypes.ts] //// | ||
|
|
||
| === unreachableReturnStatementsVsInferredReturnTypes.ts === | ||
| export function g() { | ||
| >g : () => number | ||
|
|
||
| let x; | ||
| >x : any | ||
|
|
||
| x = 1; | ||
| >x = 1 : 1 | ||
| >x : any | ||
| >1 : 1 | ||
|
|
||
| return x; | ||
| >x : number | ||
|
|
||
| return x; | ||
| >x : any | ||
| } | ||
|
|
||
| export function h() { | ||
| >h : () => number | ||
|
|
||
| return 1; | ||
| >1 : 1 | ||
|
|
||
| let y; | ||
| >y : any | ||
|
|
||
| y = 1; | ||
| >y = 1 : 1 | ||
| >y : any | ||
| >1 : 1 | ||
|
|
||
| return y; | ||
| >y : any | ||
| } | ||
|
|
||
| export function i() { | ||
| >i : () => number | ||
|
|
||
| let x: string | number | boolean; | ||
| >x : string | number | boolean | ||
|
|
||
| x = 1; | ||
| >x = 1 : 1 | ||
| >x : string | number | boolean | ||
| >1 : 1 | ||
|
|
||
| return x; | ||
| >x : number | ||
|
|
||
| x = "foo"; | ||
| >x = "foo" : "foo" | ||
| >x : string | number | boolean | ||
| >"foo" : "foo" | ||
|
|
||
| return x; | ||
| >x : string | number | boolean | ||
| } | ||
|
|
73 changes: 73 additions & 0 deletions
73
tests/baselines/reference/unreachableYieldExpressionsVsInferredYieldTypes.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| //// [tests/cases/compiler/unreachableYieldExpressionsVsInferredYieldTypes.ts] //// | ||
|
|
||
| //// [unreachableYieldExpressionsVsInferredYieldTypes.ts] | ||
| export function* g() { | ||
| let x; | ||
| x = 1; | ||
| yield x; | ||
| return 'foo'; | ||
| yield x; | ||
| } | ||
|
|
||
| export function* h() { | ||
| return 'foo'; | ||
| let y; | ||
| y = 1; | ||
| yield y; | ||
| } | ||
|
|
||
| export function* i() { | ||
| yield true; | ||
| return 'foo'; | ||
| let y; | ||
| y = 1; | ||
| yield y; | ||
| } | ||
|
|
||
| export function* j() { | ||
| let x: string | number | boolean; | ||
| x = 1; | ||
| yield x; | ||
| return true; | ||
|
|
||
| x = "foo"; | ||
| yield x; | ||
| } | ||
|
|
||
|
|
||
| //// [unreachableYieldExpressionsVsInferredYieldTypes.js] | ||
| export function* g() { | ||
| let x; | ||
| x = 1; | ||
| yield x; | ||
| return 'foo'; | ||
| yield x; | ||
| } | ||
| export function* h() { | ||
| return 'foo'; | ||
| let y; | ||
| y = 1; | ||
| yield y; | ||
| } | ||
| export function* i() { | ||
| yield true; | ||
| return 'foo'; | ||
| let y; | ||
| y = 1; | ||
| yield y; | ||
| } | ||
| export function* j() { | ||
| let x; | ||
| x = 1; | ||
| yield x; | ||
| return true; | ||
| x = "foo"; | ||
| yield x; | ||
| } | ||
|
|
||
|
|
||
| //// [unreachableYieldExpressionsVsInferredYieldTypes.d.ts] | ||
| export declare function g(): Generator<number, string, unknown>; | ||
| export declare function h(): Generator<never, string, unknown>; | ||
| export declare function i(): Generator<boolean, string, unknown>; | ||
| export declare function j(): Generator<number, boolean, unknown>; |
70 changes: 70 additions & 0 deletions
70
tests/baselines/reference/unreachableYieldExpressionsVsInferredYieldTypes.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| //// [tests/cases/compiler/unreachableYieldExpressionsVsInferredYieldTypes.ts] //// | ||
|
|
||
| === unreachableYieldExpressionsVsInferredYieldTypes.ts === | ||
| export function* g() { | ||
| >g : Symbol(g, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 0, 0)) | ||
|
|
||
| let x; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 1, 5)) | ||
|
|
||
| x = 1; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 1, 5)) | ||
|
|
||
| yield x; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 1, 5)) | ||
|
|
||
| return 'foo'; | ||
| yield x; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 1, 5)) | ||
| } | ||
|
|
||
| export function* h() { | ||
| >h : Symbol(h, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 6, 1)) | ||
|
|
||
| return 'foo'; | ||
| let y; | ||
| >y : Symbol(y, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 10, 5)) | ||
|
|
||
| y = 1; | ||
| >y : Symbol(y, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 10, 5)) | ||
|
|
||
| yield y; | ||
| >y : Symbol(y, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 10, 5)) | ||
| } | ||
|
|
||
| export function* i() { | ||
| >i : Symbol(i, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 13, 1)) | ||
|
|
||
| yield true; | ||
| return 'foo'; | ||
| let y; | ||
| >y : Symbol(y, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 18, 5)) | ||
|
|
||
| y = 1; | ||
| >y : Symbol(y, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 18, 5)) | ||
|
|
||
| yield y; | ||
| >y : Symbol(y, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 18, 5)) | ||
| } | ||
|
|
||
| export function* j() { | ||
| >j : Symbol(j, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 21, 1)) | ||
|
|
||
| let x: string | number | boolean; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 24, 5)) | ||
|
|
||
| x = 1; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 24, 5)) | ||
|
|
||
| yield x; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 24, 5)) | ||
|
|
||
| return true; | ||
|
|
||
| x = "foo"; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 24, 5)) | ||
|
|
||
| yield x; | ||
| >x : Symbol(x, Decl(unreachableYieldExpressionsVsInferredYieldTypes.ts, 24, 5)) | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.