{"action":"created","comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/comments/52146344","id":52146344,"diff_hunk":"@@ -472,10 +483,19 @@ namespace ts {\n     // @kind(SyntaxKind.StaticKeyword)\r\n     export interface Modifier extends Node { }\r\n \r\n+    /* @internal */\r\n+    export enum NarrowingState {\r\n+        Uninitialized,\r\n+        Narrowing,\r\n+        Done,\r\n+        Failed\r\n+    }\r\n     // @kind(SyntaxKind.Identifier)\r\n     export interface Identifier extends PrimaryExpression {\r\n         text: string;                                  // Text of identifier (with escapes converted to characters)\r\n         originalKeywordKind?: SyntaxKind;              // Original syntaxKind which get set so that we can report an error later\r\n+        /* @internal */ narrowingState?: NarrowingState;\r\n+        /* @internal */ localType?: Type;\r","path":"src/compiler/types.ts","position":43,"original_position":43,"commit_id":"6c7c678bbffcff0f1287da067b8e0dbc0043ee75","original_commit_id":"6c7c678bbffcff0f1287da067b8e0dbc0043ee75","user":{"login":"ivogabe","id":4706418,"avatar_url":"https://avatars.githubusercontent.com/u/4706418?v=3","gravatar_id":"","url":"https://api.github.com/users/ivogabe","html_url":"https://github.com/ivogabe","followers_url":"https://api.github.com/users/ivogabe/followers","following_url":"https://api.github.com/users/ivogabe/following{/other_user}","gists_url":"https://api.github.com/users/ivogabe/gists{/gist_id}","starred_url":"https://api.github.com/users/ivogabe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivogabe/subscriptions","organizations_url":"https://api.github.com/users/ivogabe/orgs","repos_url":"https://api.github.com/users/ivogabe/repos","events_url":"https://api.github.com/users/ivogabe/events{/privacy}","received_events_url":"https://api.github.com/users/ivogabe/received_events","type":"User","site_admin":false},"body":"Should this be here or in `NodeLinks`?","created_at":"2016-02-08T09:25:02Z","updated_at":"2016-02-08T09:25:02Z","html_url":"https://github.com/Microsoft/TypeScript/pull/6959#discussion_r52146344","pull_request_url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6959","_links":{"self":{"href":"https://api.github.com/repos/Microsoft/TypeScript/pulls/comments/52146344"},"html":{"href":"https://github.com/Microsoft/TypeScript/pull/6959#discussion_r52146344"},"pull_request":{"href":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6959"}}},"pull_request":{"url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6959","id":58555557,"html_url":"https://github.com/Microsoft/TypeScript/pull/6959","diff_url":"https://github.com/Microsoft/TypeScript/pull/6959.diff","patch_url":"https://github.com/Microsoft/TypeScript/pull/6959.patch","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6959","number":6959,"state":"open","locked":false,"title":"Control flow based type guards","user":{"login":"ivogabe","id":4706418,"avatar_url":"https://avatars.githubusercontent.com/u/4706418?v=3","gravatar_id":"","url":"https://api.github.com/users/ivogabe","html_url":"https://github.com/ivogabe","followers_url":"https://api.github.com/users/ivogabe/followers","following_url":"https://api.github.com/users/ivogabe/following{/other_user}","gists_url":"https://api.github.com/users/ivogabe/gists{/gist_id}","starred_url":"https://api.github.com/users/ivogabe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivogabe/subscriptions","organizations_url":"https://api.github.com/users/ivogabe/orgs","repos_url":"https://api.github.com/users/ivogabe/repos","events_url":"https://api.github.com/users/ivogabe/events{/privacy}","received_events_url":"https://api.github.com/users/ivogabe/received_events","type":"User","site_admin":false},"body":"As discussed in #5168, here's my PR that adds flow based type guards. This is still work in process. The compiler can compile itself, but one of the tests doesn't terminate.\r\n\r\nRelated issues: #1764, #1769, #2388\r\n\r\nHere's a quick overview of the approach I took. During binding, every node gets references to flow markers that preceded the node in the control flow. A flow marker is either a type guard or some node, such as an identifier or a while loop. This represents the control flow graph.\r\n\r\nIn `checker.ts`, the type of a variable is calculated as the union of the types after the previous occurrences. The type after a node is in case of an assignment narrowed by the assigned type (see below) and otherwise the same as before the node.\r\n\r\nAt an assignment `x = y`, where `y` can be any expression, the following happens. If the type of `x` is a union type, the constituent parts are filtered. The local type of `x` will be the union of all constituent parts to which `y` is assignable. If this yields an empty union type, the initial type of `x` is used instead. If `x` is not a union type, the local type after the assignment will be the initial type.\r\n\r\nSince the incoming flow of a `catch` block can come from anywhere in the `try` block, I decided to fall back to the initial type as a precaution. I can relax that rule if desired, for instance by looking at assignments in the `try` block.\r\n\r\nTodo:\r\n- [ ] Fix test where compiler doesn't terminate\r\n- [ ] Add tests\r\n- [ ] Clean up & comments\r\n\r\nQuestions:\r\n1. Not every `Node` has an `id` property (at runtime it is `undefined`). Is that correct or a bug? I currently use `flowIndex` (which I will remove later on) in `getPreviousOccurencies`, but `id` would be better.\r\n2. One test currently doesn't terminate. The process aborts because it's out of memory (even when I increase the old space to 8 GB). Given that the call stack contains `createFileDiagnostic`, I think the compiler is trying to create an infinite amount of diagnostics.. Does someone have an idea why that's happening?","created_at":"2016-02-07T20:05:29Z","updated_at":"2016-02-08T09:25:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ead94626ae6bd11622e2a4e200395d8269ba2c3e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6959/commits","review_comments_url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6959/comments","review_comment_url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6959/comments","statuses_url":"https://api.github.com/repos/Microsoft/TypeScript/statuses/6c7c678bbffcff0f1287da067b8e0dbc0043ee75","head":{"label":"ivogabe:guards","ref":"guards","sha":"6c7c678bbffcff0f1287da067b8e0dbc0043ee75","user":{"login":"ivogabe","id":4706418,"avatar_url":"https://avatars.githubusercontent.com/u/4706418?v=3","gravatar_id":"","url":"https://api.github.com/users/ivogabe","html_url":"https://github.com/ivogabe","followers_url":"https://api.github.com/users/ivogabe/followers","following_url":"https://api.github.com/users/ivogabe/following{/other_user}","gists_url":"https://api.github.com/users/ivogabe/gists{/gist_id}","starred_url":"https://api.github.com/users/ivogabe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivogabe/subscriptions","organizations_url":"https://api.github.com/users/ivogabe/orgs","repos_url":"https://api.github.com/users/ivogabe/repos","events_url":"https://api.github.com/users/ivogabe/events{/privacy}","received_events_url":"https://api.github.com/users/ivogabe/received_events","type":"User","site_admin":false},"repo":{"id":22783192,"name":"TypeScript","full_name":"ivogabe/TypeScript","owner":{"login":"ivogabe","id":4706418,"avatar_url":"https://avatars.githubusercontent.com/u/4706418?v=3","gravatar_id":"","url":"https://api.github.com/users/ivogabe","html_url":"https://github.com/ivogabe","followers_url":"https://api.github.com/users/ivogabe/followers","following_url":"https://api.github.com/users/ivogabe/following{/other_user}","gists_url":"https://api.github.com/users/ivogabe/gists{/gist_id}","starred_url":"https://api.github.com/users/ivogabe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivogabe/subscriptions","organizations_url":"https://api.github.com/users/ivogabe/orgs","repos_url":"https://api.github.com/users/ivogabe/repos","events_url":"https://api.github.com/users/ivogabe/events{/privacy}","received_events_url":"https://api.github.com/users/ivogabe/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ivogabe/TypeScript","description":"TypeScript is a superset of JavaScript that compiles to clean JavaScript output.","fork":true,"url":"https://api.github.com/repos/ivogabe/TypeScript","forks_url":"https://api.github.com/repos/ivogabe/TypeScript/forks","keys_url":"https://api.github.com/repos/ivogabe/TypeScript/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ivogabe/TypeScript/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ivogabe/TypeScript/teams","hooks_url":"https://api.github.com/repos/ivogabe/TypeScript/hooks","issue_events_url":"https://api.github.com/repos/ivogabe/TypeScript/issues/events{/number}","events_url":"https://api.github.com/repos/ivogabe/TypeScript/events","assignees_url":"https://api.github.com/repos/ivogabe/TypeScript/assignees{/user}","branches_url":"https://api.github.com/repos/ivogabe/TypeScript/branches{/branch}","tags_url":"https://api.github.com/repos/ivogabe/TypeScript/tags","blobs_url":"https://api.github.com/repos/ivogabe/TypeScript/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ivogabe/TypeScript/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ivogabe/TypeScript/git/refs{/sha}","trees_url":"https://api.github.com/repos/ivogabe/TypeScript/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ivogabe/TypeScript/statuses/{sha}","languages_url":"https://api.github.com/repos/ivogabe/TypeScript/languages","stargazers_url":"https://api.github.com/repos/ivogabe/TypeScript/stargazers","contributors_url":"https://api.github.com/repos/ivogabe/TypeScript/contributors","subscribers_url":"https://api.github.com/repos/ivogabe/TypeScript/subscribers","subscription_url":"https://api.github.com/repos/ivogabe/TypeScript/subscription","commits_url":"https://api.github.com/repos/ivogabe/TypeScript/commits{/sha}","git_commits_url":"https://api.github.com/repos/ivogabe/TypeScript/git/commits{/sha}","comments_url":"https://api.github.com/repos/ivogabe/TypeScript/comments{/number}","issue_comment_url":"https://api.github.com/repos/ivogabe/TypeScript/issues/comments{/number}","contents_url":"https://api.github.com/repos/ivogabe/TypeScript/contents/{+path}","compare_url":"https://api.github.com/repos/ivogabe/TypeScript/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ivogabe/TypeScript/merges","archive_url":"https://api.github.com/repos/ivogabe/TypeScript/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ivogabe/TypeScript/downloads","issues_url":"https://api.github.com/repos/ivogabe/TypeScript/issues{/number}","pulls_url":"https://api.github.com/repos/ivogabe/TypeScript/pulls{/number}","milestones_url":"https://api.github.com/repos/ivogabe/TypeScript/milestones{/number}","notifications_url":"https://api.github.com/repos/ivogabe/TypeScript/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ivogabe/TypeScript/labels{/name}","releases_url":"https://api.github.com/repos/ivogabe/TypeScript/releases{/id}","deployments_url":"https://api.github.com/repos/ivogabe/TypeScript/deployments","created_at":"2014-08-09T09:30:31Z","updated_at":"2014-08-17T10:04:35Z","pushed_at":"2016-02-08T09:18:59Z","git_url":"git://github.com/ivogabe/TypeScript.git","ssh_url":"git@github.com:ivogabe/TypeScript.git","clone_url":"https://github.com/ivogabe/TypeScript.git","svn_url":"https://github.com/ivogabe/TypeScript","homepage":"http://www.typescriptlang.org","size":206420,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"Microsoft:master","ref":"master","sha":"d27d10ce2ff013d66ff73890726b06587bda18fa","user":{"login":"Microsoft","id":6154722,"avatar_url":"https://avatars.githubusercontent.com/u/6154722?v=3","gravatar_id":"","url":"https://api.github.com/users/Microsoft","html_url":"https://github.com/Microsoft","followers_url":"https://api.github.com/users/Microsoft/followers","following_url":"https://api.github.com/users/Microsoft/following{/other_user}","gists_url":"https://api.github.com/users/Microsoft/gists{/gist_id}","starred_url":"https://api.github.com/users/Microsoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Microsoft/subscriptions","organizations_url":"https://api.github.com/users/Microsoft/orgs","repos_url":"https://api.github.com/users/Microsoft/repos","events_url":"https://api.github.com/users/Microsoft/events{/privacy}","received_events_url":"https://api.github.com/users/Microsoft/received_events","type":"Organization","site_admin":false},"repo":{"id":20929025,"name":"TypeScript","full_name":"Microsoft/TypeScript","owner":{"login":"Microsoft","id":6154722,"avatar_url":"https://avatars.githubusercontent.com/u/6154722?v=3","gravatar_id":"","url":"https://api.github.com/users/Microsoft","html_url":"https://github.com/Microsoft","followers_url":"https://api.github.com/users/Microsoft/followers","following_url":"https://api.github.com/users/Microsoft/following{/other_user}","gists_url":"https://api.github.com/users/Microsoft/gists{/gist_id}","starred_url":"https://api.github.com/users/Microsoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Microsoft/subscriptions","organizations_url":"https://api.github.com/users/Microsoft/orgs","repos_url":"https://api.github.com/users/Microsoft/repos","events_url":"https://api.github.com/users/Microsoft/events{/privacy}","received_events_url":"https://api.github.com/users/Microsoft/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Microsoft/TypeScript","description":"TypeScript is a superset of JavaScript that compiles to clean JavaScript output.","fork":false,"url":"https://api.github.com/repos/Microsoft/TypeScript","forks_url":"https://api.github.com/repos/Microsoft/TypeScript/forks","keys_url":"https://api.github.com/repos/Microsoft/TypeScript/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Microsoft/TypeScript/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Microsoft/TypeScript/teams","hooks_url":"https://api.github.com/repos/Microsoft/TypeScript/hooks","issue_events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/events{/number}","events_url":"https://api.github.com/repos/Microsoft/TypeScript/events","assignees_url":"https://api.github.com/repos/Microsoft/TypeScript/assignees{/user}","branches_url":"https://api.github.com/repos/Microsoft/TypeScript/branches{/branch}","tags_url":"https://api.github.com/repos/Microsoft/TypeScript/tags","blobs_url":"https://api.github.com/repos/Microsoft/TypeScript/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Microsoft/TypeScript/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Microsoft/TypeScript/git/refs{/sha}","trees_url":"https://api.github.com/repos/Microsoft/TypeScript/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Microsoft/TypeScript/statuses/{sha}","languages_url":"https://api.github.com/repos/Microsoft/TypeScript/languages","stargazers_url":"https://api.github.com/repos/Microsoft/TypeScript/stargazers","contributors_url":"https://api.github.com/repos/Microsoft/TypeScript/contributors","subscribers_url":"https://api.github.com/repos/Microsoft/TypeScript/subscribers","subscription_url":"https://api.github.com/repos/Microsoft/TypeScript/subscription","commits_url":"https://api.github.com/repos/Microsoft/TypeScript/commits{/sha}","git_commits_url":"https://api.github.com/repos/Microsoft/TypeScript/git/commits{/sha}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/comments{/number}","issue_comment_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments{/number}","contents_url":"https://api.github.com/repos/Microsoft/TypeScript/contents/{+path}","compare_url":"https://api.github.com/repos/Microsoft/TypeScript/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Microsoft/TypeScript/merges","archive_url":"https://api.github.com/repos/Microsoft/TypeScript/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Microsoft/TypeScript/downloads","issues_url":"https://api.github.com/repos/Microsoft/TypeScript/issues{/number}","pulls_url":"https://api.github.com/repos/Microsoft/TypeScript/pulls{/number}","milestones_url":"https://api.github.com/repos/Microsoft/TypeScript/milestones{/number}","notifications_url":"https://api.github.com/repos/Microsoft/TypeScript/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/labels{/name}","releases_url":"https://api.github.com/repos/Microsoft/TypeScript/releases{/id}","deployments_url":"https://api.github.com/repos/Microsoft/TypeScript/deployments","created_at":"2014-06-17T15:28:39Z","updated_at":"2016-02-08T09:05:42Z","pushed_at":"2016-02-08T09:19:00Z","git_url":"git://github.com/Microsoft/TypeScript.git","ssh_url":"git@github.com:Microsoft/TypeScript.git","clone_url":"https://github.com/Microsoft/TypeScript.git","svn_url":"https://github.com/Microsoft/TypeScript","homepage":"http://www.typescriptlang.org","size":471463,"stargazers_count":9254,"watchers_count":9254,"language":"TypeScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1126,"mirror_url":null,"open_issues_count":1009,"forks":1126,"open_issues":1009,"watchers":9254,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6959"},"html":{"href":"https://github.com/Microsoft/TypeScript/pull/6959"},"issue":{"href":"https://api.github.com/repos/Microsoft/TypeScript/issues/6959"},"comments":{"href":"https://api.github.com/repos/Microsoft/TypeScript/issues/6959/comments"},"review_comments":{"href":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6959/comments"},"review_comment":{"href":"https://api.github.com/repos/Microsoft/TypeScript/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6959/commits"},"statuses":{"href":"https://api.github.com/repos/Microsoft/TypeScript/statuses/6c7c678bbffcff0f1287da067b8e0dbc0043ee75"}}}}