Repository navigation
Make type guards continue to narrow within classes/nested function declarations #5626
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
Wesley Wigham (weswigham)
merged 4 commits into
microsoft:master
from
weswigham:type-guard-narrowing
Nov 19, 2015
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| //// [typeGuardInClass.ts] | ||
| let x: string | number; | ||
|
|
||
| if (typeof x === "string") { | ||
| let n = class { | ||
| constructor() { | ||
| let y: string = x; | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| let m = class { | ||
| constructor() { | ||
| let y: number = x; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| //// [typeGuardInClass.js] | ||
| var x; | ||
| if (typeof x === "string") { | ||
| var n = (function () { | ||
| function class_1() { | ||
| var y = x; | ||
| } | ||
| return class_1; | ||
| })(); | ||
| } | ||
| else { | ||
| var m = (function () { | ||
| function class_2() { | ||
| var y = x; | ||
| } | ||
| return class_2; | ||
| })(); | ||
| } |
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,29 @@ | ||
| === tests/cases/conformance/expressions/typeGuards/typeGuardInClass.ts === | ||
| let x: string | number; | ||
| >x : Symbol(x, Decl(typeGuardInClass.ts, 0, 3)) | ||
|
|
||
| if (typeof x === "string") { | ||
| >x : Symbol(x, Decl(typeGuardInClass.ts, 0, 3)) | ||
|
|
||
| let n = class { | ||
| >n : Symbol(n, Decl(typeGuardInClass.ts, 3, 7)) | ||
|
|
||
| constructor() { | ||
| let y: string = x; | ||
| >y : Symbol(y, Decl(typeGuardInClass.ts, 5, 15)) | ||
| >x : Symbol(x, Decl(typeGuardInClass.ts, 0, 3)) | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| let m = class { | ||
| >m : Symbol(m, Decl(typeGuardInClass.ts, 10, 7)) | ||
|
|
||
| constructor() { | ||
| let y: number = x; | ||
| >y : Symbol(y, Decl(typeGuardInClass.ts, 12, 15)) | ||
| >x : Symbol(x, Decl(typeGuardInClass.ts, 0, 3)) | ||
| } | ||
| } | ||
| } | ||
|
|
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,34 @@ | ||
| === tests/cases/conformance/expressions/typeGuards/typeGuardInClass.ts === | ||
| let x: string | number; | ||
| >x : string | number | ||
|
|
||
| if (typeof x === "string") { | ||
| >typeof x === "string" : boolean | ||
| >typeof x : string | ||
| >x : string | number | ||
| >"string" : string | ||
|
|
||
| let n = class { | ||
| >n : typeof (Anonymous class) | ||
| >class { constructor() { let y: string = x; } } : typeof (Anonymous class) | ||
|
|
||
| constructor() { | ||
| let y: string = x; | ||
| >y : string | ||
| >x : string | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| let m = class { | ||
| >m : typeof (Anonymous class) | ||
| >class { constructor() { let y: number = x; } } : typeof (Anonymous class) | ||
|
|
||
| constructor() { | ||
| let y: number = x; | ||
| >y : number | ||
| >x : number | ||
| } | ||
| } | ||
| } | ||
|
|
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
16 changes: 16 additions & 0 deletions
16
tests/cases/conformance/expressions/typeGuards/typeGuardInClass.ts
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,16 @@ | ||
| let x: string | number; | ||
|
|
||
| if (typeof x === "string") { | ||
| let n = class { | ||
| constructor() { | ||
| let y: string = x; | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| let m = class { | ||
| constructor() { | ||
| let y: number = x; | ||
| } | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So now
getNarrowedTypeOfSymbolwalks up the parent chain twice, and further than it used to. Is there any performance impact?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getDeclarationOfKinddoesn't walk up any tree, it just iterates over a symbol's declarations.getDeclarationContainerwalks out of a binding pattern (which is unavoidable, but also not highly nested) but then just doesnode.parent.parent.parentornode.parent. I'm not seeing a secondary walk here at all, just static overhead. Now as for weather it's worth doing this just to bail early rather than walking to the top of file... no idea. It probably depends on how deeply nested your code gets. I'll run some perf tests with and without the bailout.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are walking once to find
topthen another time until you hittop. we can put them together, and walk up once, and just stop when you find what would betopotherwise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finding
topdoesn't involve a walk (beyond moving out of a binding pattern in the declaration), though?Finding
topis a matter of going from here:to the enclosing block.
The actual walk up starts at the node to narrow at:
and can either go up to the top level of the file/namespace (though this could traverse a lot of excess nodes if this is the only terminating condition), or can stop at the location defined by
top- the scope within which the symbol in question is defined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I meant
getDeclarationContainerbut put the comment one line too high by mistake.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah,
getDeclarationContainerisn't a heavy walk. It's a static 2 or 4 references up (based on declaration kind) after walking out of the binding pattern in the declaration. I can't merge these walks as Mohamed Hegazy (@mhegazy) suggests because I'm not walking the same section of the tree twice (declaration -> up to declaration container vs use site -> up to declaration container - I mean, I could merge them, but I'd have to recur into all siblings at each level looking for the declaration, which seems really bad). Ifkis the stack of nodes between the file/module container and the node we're narrowing at (which is how many nodes I would walk in the simple case without looking for the declaration container of the symbol's declaration), andnis the number of nodes I walk up when computing narrowing before stopping attop, andmis the number of nodes I walk up when computing the declaration container to computetop(which is to say the recursive depth of the binding pattern), I'm wagering that walking overm+nis practically more performant than thank, given thatmhas a tendency to be very small andkhas a tendency to be very large.Now, if it turns out that the declaration container is the top level of the namespace/module, then calculating it was extra work we didn't need to do as
n=kand we've walked overm+knodes rather than potentially justknodes.Really, this could be avoided if a declaration just kept a reference somewhere to its container, but that would be awkward, I suppose.
In effect, I'm trying to assert that
getDeclarationContaineris close enough to constant time in all real-world cases, that its usage is warranted to be able to bail out of our narrowing walk up the tree early.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current changes vs master baseline (5 iterations of perf test):
It causes minimal/no changes to overall perf in real-world scenarios. (Though I imagine this is the kind of change were you'd only see perf changes in a micro-benchmark anyway)