-
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
Conversation
@typescript-bot test top200 |
Hey @ahejlsberg, the results of running the DT tests are ready. |
@ahejlsberg Here are the results of running the user test suite comparing Something interesting changed - please have a look. Details |
@ahejlsberg Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@ahejlsberg Here are the results of running the top-repos suite comparing Something interesting changed - please have a look. Details
|
Weird; reading some of the breaks, there seems to be a bug in this iteration of the PR where unrelated accesses are getting narrowed too. declare const key: string;
declare const obj1: Record<string, number | undefined>;
declare const obj2: Record<string, number | undefined>;
if (obj1[key]) {
const x = obj1[key];
// ^?
const y = obj2[key];
// ^?
} |
Argh, I see what the issue is. Will fix. |
@typescript-bot test top200 |
Hey @ahejlsberg, the results of running the DT tests are ready. |
@ahejlsberg Here are the results of running the user test suite comparing Everything looks good! |
@ahejlsberg Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@typescript-bot perf test this faster |
@ahejlsberg Here are the results of running the top-repos suite comparing Something interesting changed - please have a look. Details
|
@typescript-bot pack this |
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
@typescript-bot test top200 |
@typescript-bot pack this |
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
@ahejlsberg Here are the results of running the top-repos suite comparing Everything looks good! |
Tests and performance look good. This is ready for a review. |
} | ||
if (isElementAccessExpression(source) && isElementAccessExpression(target) && isIdentifier(source.argumentExpression) && isIdentifier(target.argumentExpression)) { | ||
const symbol = getResolvedSymbol(source.argumentExpression); | ||
if (symbol === getResolvedSymbol(target.argumentExpression) && (isConstantVariable(symbol) || isParameterOrMutableLocalVariable(symbol) && !isSymbolAssigned(symbol))) { |
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 to key
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.
Can you add a test in the vein of...
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.
@typescript-bot perf test this faster |
@ahejlsberg Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
Hi. I'm not sure if it's okay to ask here or I should rather open new discussion, so sorry in advance. Do you think the following type narrowing using I think this might be related to this feature. |
That error is correct and desirable: the index signature only specifies that properties in |
I see. Big thank you for an explanation |
Hi and thank you for your work! Trying to clarify for myself this statement "The intuition here is that even if the exact name of the property selected by key isn't known, any control flow analysis proof that involves obj[key] should hold in subsequent statements as long as obj and key aren't mutated" from the PR description. Here I mutate
Is this "expected"? |
With this PR we perform control flow analysis for element access expressions
obj[key]
wherekey
is aconst
variable, or alet
variable or parameter that is never targeted in an assignment. The intuition here is that even if the exact name of the property selected bykey
isn't known, any control flow analysis proof that involvesobj[key]
should hold in subsequent statements as long asobj
andkey
aren't mutated,Some examples:
Fixes #56389.