Skip to content
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

[scope] keep track of assignment/access ordering #413

Merged
merged 3 commits into from
Nov 17, 2020

Conversation

zsol
Copy link
Member

@zsol zsol commented Nov 4, 2020

Summary

This PR makes sure references are never attached to assignments that are declared later than the access (but still in the same scope). It does so by keeping track of the relative ordering of accesses to assignments, through the inclusion of an __index member in both. The idea is to increment the index every time an assignment "happens" in a scope. This is a bit tricky because for a = b, the right hand side cannot refer to variables on the left side even if the left side is visited first.

Fixes #326

Test Plan

Added extensive test case for a variable shadowing in a class/global scope scenario.

@zsol zsol requested a review from jimmylai November 4, 2020 16:46
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 4, 2020
@zsol zsol marked this pull request as ready for review November 4, 2020 17:00
@zsol zsol requested a review from jimmylai November 10, 2020 10:02
libcst/metadata/scope_provider.py Outdated Show resolved Hide resolved
Comment on lines 139 to 141
if earlier_accesses != set() and self.scope.parent != self.scope:
for shadowed_assignment in self.scope.parent[self.name]:
shadowed_assignment.record_accesses(earlier_accesses)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVM, I got it. It's to handle the accesses not part of later access. Add them to parent assignments make sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used to have a comment here, it might've got lost during rebases. I'll add it back

Copy link
Contributor

@jimmylai jimmylai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the comments, LGTM.

@zsol zsol merged commit 2ef7302 into Instagram:master Nov 17, 2020
@zsol zsol deleted the scope-ordering branch November 17, 2020 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[scope analysis] References attached to the wrong scope
4 participants