mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:15:33 +00:00
Move dead_scopes
to deferred.scopes
(#5171)
## Summary This is more consistent with the rest of the `deferred` patterns.
This commit is contained in:
parent
524a2045ba
commit
a6cf31cc89
3 changed files with 9 additions and 8 deletions
|
@ -1,13 +1,14 @@
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::Expr;
|
use rustpython_parser::ast::Expr;
|
||||||
|
|
||||||
use ruff_python_semantic::Snapshot;
|
use ruff_python_semantic::{ScopeId, Snapshot};
|
||||||
|
|
||||||
/// A collection of AST nodes that are deferred for later analysis.
|
/// A collection of AST nodes that are deferred for later analysis.
|
||||||
/// Used to, e.g., store functions, whose bodies shouldn't be analyzed until all
|
/// Used to, e.g., store functions, whose bodies shouldn't be analyzed until all
|
||||||
/// module-level definitions have been analyzed.
|
/// module-level definitions have been analyzed.
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub(crate) struct Deferred<'a> {
|
pub(crate) struct Deferred<'a> {
|
||||||
|
pub(crate) scopes: Vec<ScopeId>,
|
||||||
pub(crate) string_type_definitions: Vec<(TextRange, &'a str, Snapshot)>,
|
pub(crate) string_type_definitions: Vec<(TextRange, &'a str, Snapshot)>,
|
||||||
pub(crate) future_type_definitions: Vec<(&'a Expr, Snapshot)>,
|
pub(crate) future_type_definitions: Vec<(&'a Expr, Snapshot)>,
|
||||||
pub(crate) functions: Vec<Snapshot>,
|
pub(crate) functions: Vec<Snapshot>,
|
||||||
|
|
|
@ -2038,10 +2038,12 @@ where
|
||||||
// Post-visit.
|
// Post-visit.
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => {
|
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => {
|
||||||
|
self.deferred.scopes.push(self.semantic.scope_id);
|
||||||
self.semantic.pop_scope();
|
self.semantic.pop_scope();
|
||||||
self.semantic.pop_definition();
|
self.semantic.pop_definition();
|
||||||
}
|
}
|
||||||
Stmt::ClassDef(ast::StmtClassDef { name, .. }) => {
|
Stmt::ClassDef(ast::StmtClassDef { name, .. }) => {
|
||||||
|
self.deferred.scopes.push(self.semantic.scope_id);
|
||||||
self.semantic.pop_scope();
|
self.semantic.pop_scope();
|
||||||
self.semantic.pop_definition();
|
self.semantic.pop_definition();
|
||||||
self.add_binding(
|
self.add_binding(
|
||||||
|
@ -3785,6 +3787,7 @@ where
|
||||||
| Expr::ListComp(_)
|
| Expr::ListComp(_)
|
||||||
| Expr::DictComp(_)
|
| Expr::DictComp(_)
|
||||||
| Expr::SetComp(_) => {
|
| Expr::SetComp(_) => {
|
||||||
|
self.deferred.scopes.push(self.semantic.scope_id);
|
||||||
self.semantic.pop_scope();
|
self.semantic.pop_scope();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -4692,7 +4695,7 @@ impl<'a> Checker<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_dead_scopes(&mut self) {
|
fn check_deferred_scopes(&mut self) {
|
||||||
if !self.any_enabled(&[
|
if !self.any_enabled(&[
|
||||||
Rule::UnusedImport,
|
Rule::UnusedImport,
|
||||||
Rule::GlobalVariableNotAssigned,
|
Rule::GlobalVariableNotAssigned,
|
||||||
|
@ -4771,7 +4774,7 @@ impl<'a> Checker<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut diagnostics: Vec<Diagnostic> = vec![];
|
let mut diagnostics: Vec<Diagnostic> = vec![];
|
||||||
for scope_id in self.semantic.dead_scopes.iter().rev() {
|
for scope_id in self.deferred.scopes.iter().rev() {
|
||||||
let scope = &self.semantic.scopes[*scope_id];
|
let scope = &self.semantic.scopes[*scope_id];
|
||||||
|
|
||||||
if scope.kind.is_module() {
|
if scope.kind.is_module() {
|
||||||
|
@ -5256,8 +5259,8 @@ pub(crate) fn check_ast(
|
||||||
|
|
||||||
// Reset the scope to module-level, and check all consumed scopes.
|
// Reset the scope to module-level, and check all consumed scopes.
|
||||||
checker.semantic.scope_id = ScopeId::global();
|
checker.semantic.scope_id = ScopeId::global();
|
||||||
checker.semantic.dead_scopes.push(ScopeId::global());
|
checker.deferred.scopes.push(ScopeId::global());
|
||||||
checker.check_dead_scopes();
|
checker.check_deferred_scopes();
|
||||||
|
|
||||||
checker.diagnostics
|
checker.diagnostics
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,6 @@ pub struct SemanticModel<'a> {
|
||||||
/// Stack of all scopes, along with the identifier of the current scope.
|
/// Stack of all scopes, along with the identifier of the current scope.
|
||||||
pub scopes: Scopes<'a>,
|
pub scopes: Scopes<'a>,
|
||||||
pub scope_id: ScopeId,
|
pub scope_id: ScopeId,
|
||||||
pub dead_scopes: Vec<ScopeId>,
|
|
||||||
|
|
||||||
/// Stack of all definitions created in any scope, at any point in execution.
|
/// Stack of all definitions created in any scope, at any point in execution.
|
||||||
pub definitions: Definitions<'a>,
|
pub definitions: Definitions<'a>,
|
||||||
|
@ -130,7 +129,6 @@ impl<'a> SemanticModel<'a> {
|
||||||
exprs: Vec::default(),
|
exprs: Vec::default(),
|
||||||
scopes: Scopes::default(),
|
scopes: Scopes::default(),
|
||||||
scope_id: ScopeId::global(),
|
scope_id: ScopeId::global(),
|
||||||
dead_scopes: Vec::default(),
|
|
||||||
definitions: Definitions::for_module(module),
|
definitions: Definitions::for_module(module),
|
||||||
definition_id: DefinitionId::module(),
|
definition_id: DefinitionId::module(),
|
||||||
bindings: Bindings::default(),
|
bindings: Bindings::default(),
|
||||||
|
@ -596,7 +594,6 @@ impl<'a> SemanticModel<'a> {
|
||||||
|
|
||||||
/// Pop the current [`Scope`] off the stack.
|
/// Pop the current [`Scope`] off the stack.
|
||||||
pub fn pop_scope(&mut self) {
|
pub fn pop_scope(&mut self) {
|
||||||
self.dead_scopes.push(self.scope_id);
|
|
||||||
self.scope_id = self.scopes[self.scope_id]
|
self.scope_id = self.scopes[self.scope_id]
|
||||||
.parent
|
.parent
|
||||||
.expect("Attempted to pop without scope");
|
.expect("Attempted to pop without scope");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue