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

Red Knot - Add symbol flags #11134

Merged
merged 18 commits into from Apr 30, 2024
Merged

Red Knot - Add symbol flags #11134

merged 18 commits into from Apr 30, 2024

Conversation

plredmond
Copy link
Contributor

@plredmond plredmond commented Apr 24, 2024

  • Adds Symbol.flag bitfield. Populates it from (the three renamed) add_or_update_symbol* methods.
  • Currently there are these flags supported:
    • IS_DEFINED is set in a scope where a variable is defined.
    • IS_USED is set in a scope where a variable is referenced. (To have both this and IS_DEFINED would require two separate appearances of a variable in the same scope-- one def and one use.)
    • MARKED_GLOBAL and MARKED_NONLOCAL are not yet implemented. (TODO: While traversing, if you find these declarations, add these flags to the variable.)
  • Adds Symbol.kind field (commented) and the data structure which will populate it: Kind which is an enum of freevar, cellvar, implicit_global, and implicit_local. Not yet populated. (TODO: a second pass over the scope (or the ast?) will observe the MARKED_GLOBAL and MARKED_NONLOCAL flags to populate this field. When that's added, we'll uncomment the field.)
  • Adds a few tests that the IS_DEFINED and IS_USED fields are correctly set and/or merged:
    • Unit test that subsequent calls to add_or_update_symbol will merge the flag arguments.
    • Unit test that in the statement x = foo, the variable foo is considered used but not defined.
    • Unit test that in the statement from bar import foo, the variable foo is considered defined but not used.

@plredmond plredmond changed the base branch from main to red-knot April 24, 2024 19:02
@plredmond plredmond requested a review from carljm April 24, 2024 19:04
crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
Copy link

github-actions bot commented Apr 25, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@plredmond plredmond marked this pull request as ready for review April 29, 2024 22:51
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Looks good! Couple comments.

crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
crates/red_knot/src/symbols.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Awesome!

Couple comments on the PR description:

  1. It says three flags are supported, but it seems like just two?
  2. This is more a forward-looking comment for the TODOs, but I realized from some conversation this morning that we will probably want to track both CellVar and CellVarAssigned as separate kinds, where the former is "defined in current scope, used in at least one nested scope" and the latter is "defined in current scope, defined and marked_nonlocal in at least one nested scope", because the latter will have different implications for type checking.

@plredmond
Copy link
Contributor Author

It says three flags are supported, but it seems like just two?

That was a typo of "these". Sorry.

I've added a commit for the other variant of Kind.

@plredmond plredmond merged commit c391c8b into main Apr 30, 2024
19 checks passed
@plredmond plredmond deleted the red-knot_symbol-flags branch April 30, 2024 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants