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

Move binding and scope tracking into a separate ast::Context struct #3298

Merged
merged 3 commits into from
Mar 4, 2023

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Mar 2, 2023

This PR is an attempt to split up Checker in such a way so as to allow us to break the Ruff linter itself into multiple crates.

The first step (seen here) is to remove all AST-visitation state into a new Context struct, which tracks the current scope stack, available bindings, etc. By moving these fields and methods off of Checker, rules can instead take Context as an argument, rather than Checker, which means they can be decoupled from the actual "driver" / "visitor" and thereby the rest of the lint rules.

@charliermarsh charliermarsh force-pushed the charlie/split-ast branch 3 times, most recently from 8eb23f6 to f666edb Compare March 2, 2023 04:37
@charliermarsh
Copy link
Member Author

charliermarsh commented Mar 2, 2023

Diagnostic is another tough cycle to break. Depends on DiagnosticKind which depends on all rule violations. Need to play around with this and reconsider the struct hierarchy.

@charliermarsh
Copy link
Member Author

Settings is another. Perhaps we can change rules to rely on the settings they care about, rather than the Settings struct, and rely on Checker to inject those settings at the call-site.

crates/ruff/src/checkers/ctx.rs Outdated Show resolved Hide resolved
crates/ruff/src/checkers/ctx.rs Outdated Show resolved Hide resolved
crates/ruff/src/checkers/ast.rs Outdated Show resolved Hide resolved
@charliermarsh charliermarsh force-pushed the charlie/split-ast branch 8 times, most recently from 2fe4d67 to cb54ddc Compare March 4, 2023 00:42
@charliermarsh charliermarsh marked this pull request as ready for review March 4, 2023 00:42
@charliermarsh charliermarsh changed the title Split up Checker into independent structs Move binding and scope tracking into a separate ast::Context struct Mar 4, 2023
@charliermarsh
Copy link
Member Author

Okay, this can be merged as-is (in that, it doesn't depend on any additional refactors to be useful).

@JonathanPlasse
Copy link
Contributor

JonathanPlasse commented Mar 4, 2023

How much did the compilation time improved?
Or is it preparation for future improvements?

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

LGTM: Something that's unclear to me but isn't something that we need to solve now is how do I decide whether to add some logic to Checker or Context? It is my impression that there are still many methods on Checker that only use state stored on the Context. Should these be moved to Context at some point?

type Context<'a> = (Vec<usize>, Vec<RefEquality<'a, Stmt>>);

#[derive(Default)]
pub struct Deferred<'a> {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Can you add some documentation explaining what a Deferred is? I'm unable to guess its usage from just its name.

crates/ruff/src/checkers/ast/mod.rs Show resolved Hide resolved
@charliermarsh
Copy link
Member Author

It is my impression that there are still many methods on Checker that only use state stored on the Context. Should these be moved to Context at some point?

The intent is that these should all be moved over to Context. Lemme see if I missed any...

@charliermarsh
Copy link
Member Author

How much did the compilation time improved? Or is it preparation for future improvements?

Yeah just in preparation for future improvements. I don't expect this alone to do much, it's more that it sets us up to be able to split the crate later.

@charliermarsh charliermarsh merged commit 40d3b40 into main Mar 4, 2023
@charliermarsh charliermarsh deleted the charlie/split-ast branch March 4, 2023 19:01
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.

3 participants