mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
[red-knot] Track reachability of scopes (#17332)
## Summary Track the reachability of nested scopes within their parent scopes. We use this as an additional requirement for emitting `unresolved-reference` diagnostics (and in the future, `unresolved-attribute` and `unresolved-import`). This means that we only emit `unresolved-reference` for a given use of a symbol if the use itself is reachable (within its own scope), *and if the scope itself is reachable*. For example, no diagnostic should be emitted for the use of `x` here: ```py if False: x = 1 def f(): print(x) # this use of `x` is reachable inside the `f` scope, # but the whole `f` scope is not reachable. ``` There are probably more fine-grained ways of solving this problem, but they require a more sophisticated understanding of nested scopes (see #15777, in particular https://github.com/astral-sh/ruff/issues/15777#issuecomment-2788950267). But it doesn't seem completely unreasonable to silence *this specific kind of error* in unreachable scopes. ## Test Plan Observed changes in reachability tests and ecosystem.
This commit is contained in:
parent
06ffeb2e09
commit
4d50ee6f52
6 changed files with 79 additions and 24 deletions
|
@ -11,7 +11,7 @@ use salsa::Update;
|
|||
|
||||
use crate::module_name::ModuleName;
|
||||
use crate::semantic_index::ast_ids::node_key::ExpressionNodeKey;
|
||||
use crate::semantic_index::ast_ids::AstIds;
|
||||
use crate::semantic_index::ast_ids::{AstIds, ScopedUseId};
|
||||
use crate::semantic_index::attribute_assignment::AttributeAssignments;
|
||||
use crate::semantic_index::builder::SemanticIndexBuilder;
|
||||
use crate::semantic_index::definition::{Definition, DefinitionNodeKey, Definitions};
|
||||
|
@ -240,6 +240,43 @@ impl<'db> SemanticIndex<'db> {
|
|||
Some(&self.scopes[self.parent_scope_id(scope_id)?])
|
||||
}
|
||||
|
||||
fn is_scope_reachable(&self, db: &'db dyn Db, scope_id: FileScopeId) -> bool {
|
||||
self.parent_scope_id(scope_id)
|
||||
.is_none_or(|parent_scope_id| {
|
||||
if !self.is_scope_reachable(db, parent_scope_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let parent_use_def = self.use_def_map(parent_scope_id);
|
||||
let reachability = self.scope(scope_id).reachability();
|
||||
|
||||
parent_use_def.is_reachable(db, reachability)
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns true if a given 'use' of a symbol is reachable from the start of the scope.
|
||||
/// For example, in the following code, use `2` is reachable, but `1` and `3` are not:
|
||||
/// ```py
|
||||
/// def f():
|
||||
/// x = 1
|
||||
/// if False:
|
||||
/// x # 1
|
||||
/// x # 2
|
||||
/// return
|
||||
/// x # 3
|
||||
/// ```
|
||||
pub(crate) fn is_symbol_use_reachable(
|
||||
&self,
|
||||
db: &'db dyn crate::Db,
|
||||
scope_id: FileScopeId,
|
||||
use_id: ScopedUseId,
|
||||
) -> bool {
|
||||
self.is_scope_reachable(db, scope_id)
|
||||
&& self
|
||||
.use_def_map(scope_id)
|
||||
.is_symbol_use_reachable(db, use_id)
|
||||
}
|
||||
|
||||
/// Returns an iterator over the descendent scopes of `scope`.
|
||||
#[allow(unused)]
|
||||
pub(crate) fn descendent_scopes(&self, scope: FileScopeId) -> DescendantsIter {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue