-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Control flow analysis for element access with variable index #57847
Merged
+367
−14
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aed5fc0
CFA for obj[key] where key is constant variable or parameter
ahejlsberg 8676121
Accept new baselines
ahejlsberg 2ce7c15
Add tests
ahejlsberg 95818c0
Fully compare references
ahejlsberg aa68fd8
Minor optimization
ahejlsberg 8a277ec
Restore original resolveEntityName call
ahejlsberg 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 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
134 changes: 134 additions & 0 deletions
134
tests/baselines/reference/controlFlowComputedPropertyNames.symbols
This file contains 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,134 @@ | ||
//// [tests/cases/conformance/controlFlow/controlFlowComputedPropertyNames.ts] //// | ||
|
||
=== controlFlowComputedPropertyNames.ts === | ||
function f1(obj: Record<string, unknown>, key: string) { | ||
>f1 : Symbol(f1, Decl(controlFlowComputedPropertyNames.ts, 0, 0)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 0, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 0, 41)) | ||
|
||
if (typeof obj[key] === "string") { | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 0, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 0, 41)) | ||
|
||
obj[key].toUpperCase(); | ||
>obj[key].toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 0, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 0, 41)) | ||
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
} | ||
|
||
function f2(obj: Record<string, string | undefined>, key: string) { | ||
>f2 : Symbol(f2, Decl(controlFlowComputedPropertyNames.ts, 4, 1)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 6, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 6, 52)) | ||
|
||
if (obj[key] !== undefined) { | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 6, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 6, 52)) | ||
>undefined : Symbol(undefined) | ||
|
||
obj[key].toUpperCase(); | ||
>obj[key].toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 6, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 6, 52)) | ||
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
let key2 = key + key; | ||
>key2 : Symbol(key2, Decl(controlFlowComputedPropertyNames.ts, 10, 7)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 6, 52)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 6, 52)) | ||
|
||
if (obj[key2] !== undefined) { | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 6, 12)) | ||
>key2 : Symbol(key2, Decl(controlFlowComputedPropertyNames.ts, 10, 7)) | ||
>undefined : Symbol(undefined) | ||
|
||
obj[key2].toUpperCase(); | ||
>obj[key2].toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 6, 12)) | ||
>key2 : Symbol(key2, Decl(controlFlowComputedPropertyNames.ts, 10, 7)) | ||
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
const key3 = key + key; | ||
>key3 : Symbol(key3, Decl(controlFlowComputedPropertyNames.ts, 14, 9)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 6, 52)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 6, 52)) | ||
|
||
if (obj[key3] !== undefined) { | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 6, 12)) | ||
>key3 : Symbol(key3, Decl(controlFlowComputedPropertyNames.ts, 14, 9)) | ||
>undefined : Symbol(undefined) | ||
|
||
obj[key3].toUpperCase(); | ||
>obj[key3].toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 6, 12)) | ||
>key3 : Symbol(key3, Decl(controlFlowComputedPropertyNames.ts, 14, 9)) | ||
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
} | ||
|
||
type Thing = { a?: string, b?: number, c?: number }; | ||
>Thing : Symbol(Thing, Decl(controlFlowComputedPropertyNames.ts, 18, 1)) | ||
>a : Symbol(a, Decl(controlFlowComputedPropertyNames.ts, 20, 14)) | ||
>b : Symbol(b, Decl(controlFlowComputedPropertyNames.ts, 20, 26)) | ||
>c : Symbol(c, Decl(controlFlowComputedPropertyNames.ts, 20, 38)) | ||
|
||
function f3(obj: Thing, key: keyof Thing) { | ||
>f3 : Symbol(f3, Decl(controlFlowComputedPropertyNames.ts, 20, 52)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 22, 12)) | ||
>Thing : Symbol(Thing, Decl(controlFlowComputedPropertyNames.ts, 18, 1)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 22, 23)) | ||
>Thing : Symbol(Thing, Decl(controlFlowComputedPropertyNames.ts, 18, 1)) | ||
|
||
if (obj[key] !== undefined) { | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 22, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 22, 23)) | ||
>undefined : Symbol(undefined) | ||
|
||
if (typeof obj[key] === "string") { | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 22, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 22, 23)) | ||
|
||
obj[key].toUpperCase(); | ||
>obj[key].toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 22, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 22, 23)) | ||
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
if (typeof obj[key] === "number") { | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 22, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 22, 23)) | ||
|
||
obj[key].toFixed(); | ||
>obj[key].toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 22, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 22, 23)) | ||
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
} | ||
} | ||
|
||
function f4<K extends string>(obj: Record<K, string | undefined>, key: K) { | ||
>f4 : Symbol(f4, Decl(controlFlowComputedPropertyNames.ts, 31, 1)) | ||
>K : Symbol(K, Decl(controlFlowComputedPropertyNames.ts, 33, 12)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 33, 30)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>K : Symbol(K, Decl(controlFlowComputedPropertyNames.ts, 33, 12)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 33, 65)) | ||
>K : Symbol(K, Decl(controlFlowComputedPropertyNames.ts, 33, 12)) | ||
|
||
if (obj[key]) { | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 33, 30)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 33, 65)) | ||
|
||
obj[key].toUpperCase(); | ||
>obj[key].toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
>obj : Symbol(obj, Decl(controlFlowComputedPropertyNames.ts, 33, 30)) | ||
>key : Symbol(key, Decl(controlFlowComputedPropertyNames.ts, 33, 65)) | ||
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
} | ||
|
163 changes: 163 additions & 0 deletions
163
tests/baselines/reference/controlFlowComputedPropertyNames.types
This file contains 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,163 @@ | ||
//// [tests/cases/conformance/controlFlow/controlFlowComputedPropertyNames.ts] //// | ||
|
||
=== controlFlowComputedPropertyNames.ts === | ||
function f1(obj: Record<string, unknown>, key: string) { | ||
>f1 : (obj: Record<string, unknown>, key: string) => void | ||
>obj : Record<string, unknown> | ||
>key : string | ||
|
||
if (typeof obj[key] === "string") { | ||
>typeof obj[key] === "string" : boolean | ||
>typeof obj[key] : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>obj[key] : unknown | ||
>obj : Record<string, unknown> | ||
>key : string | ||
>"string" : "string" | ||
|
||
obj[key].toUpperCase(); | ||
>obj[key].toUpperCase() : string | ||
>obj[key].toUpperCase : () => string | ||
>obj[key] : string | ||
>obj : Record<string, unknown> | ||
>key : string | ||
>toUpperCase : () => string | ||
} | ||
} | ||
|
||
function f2(obj: Record<string, string | undefined>, key: string) { | ||
>f2 : (obj: Record<string, string | undefined>, key: string) => void | ||
>obj : Record<string, string | undefined> | ||
>key : string | ||
|
||
if (obj[key] !== undefined) { | ||
>obj[key] !== undefined : boolean | ||
>obj[key] : string | undefined | ||
>obj : Record<string, string | undefined> | ||
>key : string | ||
>undefined : undefined | ||
|
||
obj[key].toUpperCase(); | ||
>obj[key].toUpperCase() : string | ||
>obj[key].toUpperCase : () => string | ||
>obj[key] : string | ||
>obj : Record<string, string | undefined> | ||
>key : string | ||
>toUpperCase : () => string | ||
} | ||
let key2 = key + key; | ||
>key2 : string | ||
>key + key : string | ||
>key : string | ||
>key : string | ||
|
||
if (obj[key2] !== undefined) { | ||
>obj[key2] !== undefined : boolean | ||
>obj[key2] : string | undefined | ||
>obj : Record<string, string | undefined> | ||
>key2 : string | ||
>undefined : undefined | ||
|
||
obj[key2].toUpperCase(); | ||
>obj[key2].toUpperCase() : string | ||
>obj[key2].toUpperCase : () => string | ||
>obj[key2] : string | ||
>obj : Record<string, string | undefined> | ||
>key2 : string | ||
>toUpperCase : () => string | ||
} | ||
const key3 = key + key; | ||
>key3 : string | ||
>key + key : string | ||
>key : string | ||
>key : string | ||
|
||
if (obj[key3] !== undefined) { | ||
>obj[key3] !== undefined : boolean | ||
>obj[key3] : string | undefined | ||
>obj : Record<string, string | undefined> | ||
>key3 : string | ||
>undefined : undefined | ||
|
||
obj[key3].toUpperCase(); | ||
>obj[key3].toUpperCase() : string | ||
>obj[key3].toUpperCase : () => string | ||
>obj[key3] : string | ||
>obj : Record<string, string | undefined> | ||
>key3 : string | ||
>toUpperCase : () => string | ||
} | ||
} | ||
|
||
type Thing = { a?: string, b?: number, c?: number }; | ||
>Thing : { a?: string | undefined; b?: number | undefined; c?: number | undefined; } | ||
>a : string | undefined | ||
>b : number | undefined | ||
>c : number | undefined | ||
|
||
function f3(obj: Thing, key: keyof Thing) { | ||
>f3 : (obj: Thing, key: keyof Thing) => void | ||
>obj : Thing | ||
>key : keyof Thing | ||
|
||
if (obj[key] !== undefined) { | ||
>obj[key] !== undefined : boolean | ||
>obj[key] : string | number | undefined | ||
>obj : Thing | ||
>key : keyof Thing | ||
>undefined : undefined | ||
|
||
if (typeof obj[key] === "string") { | ||
>typeof obj[key] === "string" : boolean | ||
>typeof obj[key] : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>obj[key] : string | number | ||
>obj : Thing | ||
>key : keyof Thing | ||
>"string" : "string" | ||
|
||
obj[key].toUpperCase(); | ||
>obj[key].toUpperCase() : string | ||
>obj[key].toUpperCase : () => string | ||
>obj[key] : string | ||
>obj : Thing | ||
>key : keyof Thing | ||
>toUpperCase : () => string | ||
} | ||
if (typeof obj[key] === "number") { | ||
>typeof obj[key] === "number" : boolean | ||
>typeof obj[key] : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>obj[key] : string | number | ||
>obj : Thing | ||
>key : keyof Thing | ||
>"number" : "number" | ||
|
||
obj[key].toFixed(); | ||
>obj[key].toFixed() : string | ||
>obj[key].toFixed : (fractionDigits?: number | undefined) => string | ||
>obj[key] : number | ||
>obj : Thing | ||
>key : keyof Thing | ||
>toFixed : (fractionDigits?: number | undefined) => string | ||
} | ||
} | ||
} | ||
|
||
function f4<K extends string>(obj: Record<K, string | undefined>, key: K) { | ||
>f4 : <K extends string>(obj: Record<K, string | undefined>, key: K) => void | ||
>obj : Record<K, string | undefined> | ||
>key : K | ||
|
||
if (obj[key]) { | ||
>obj[key] : Record<K, string | undefined>[K] | ||
>obj : Record<K, string | undefined> | ||
>key : K | ||
|
||
obj[key].toUpperCase(); | ||
>obj[key].toUpperCase() : string | ||
>obj[key].toUpperCase : () => string | ||
>obj[key] : string | ||
>obj : Record<K, string | undefined> | ||
>key : K | ||
>toUpperCase : () => string | ||
} | ||
} | ||
|
This file contains 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 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 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
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.
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.
Does it make more sense to do
isPastLastAssignment
using a more specific location? How does this PR interact with #56908?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.
Unfortunately not that simple. We would need to know that all narrowing operations for
obj[key]
occur past the last assignment tokey
which would require an additional CFA graph walk. We don't have that issue in #56908 because the only location we care about is the location where the arrow function is created.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.
Makes sense; I really need to prototype the idea I had to make that efficient enough for us to do, but I've lost that brainwave☹️
Can you add a test in the vein of #56908 in terms of what closures do? Something like: Playground Link (which does behave properly I think)
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.
Such a test wouldn't show anything. The new analysis in #56908 only affects references to local variables, not references to properties, so there are no changes across closure boundaries with this PR.