Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions crates/ty_python_semantic/src/semantic_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,6 @@ impl<'db> SemanticIndex<'db> {
.map(|node_ref| self.expect_single_definition(node_ref))
}

pub(crate) 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)
})
}

/// Check whether a diagnostic emitted at `range` is in reachable code, considering both
/// scope reachability and statement-level reachability within the scope.
pub(crate) fn is_range_reachable(
Expand All @@ -496,8 +482,8 @@ impl<'db> SemanticIndex<'db> {
scope_id: FileScopeId,
range: TextRange,
) -> bool {
self.is_scope_reachable(db, scope_id)
&& self.use_def_map(scope_id).is_range_reachable(db, range)
self.ancestor_scopes(scope_id)
.all(|(scope_id, _)| self.use_def_map(scope_id).is_range_reachable(db, range))
}

pub(crate) fn is_in_type_checking_block(
Expand Down
33 changes: 4 additions & 29 deletions crates/ty_python_semantic/src/semantic_index/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,7 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
semantic_syntax_errors: RefCell::default(),
};

builder.push_scope_with_parent(
NodeWithScopeRef::Module,
None,
ScopedReachabilityConstraintId::ALWAYS_TRUE,
);

builder.push_scope_with_parent(NodeWithScopeRef::Module, None);
builder
}

Expand Down Expand Up @@ -303,28 +298,16 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
}

fn push_scope(&mut self, node: NodeWithScopeRef) {
let parent = self.current_scope();
let reachability = self.current_use_def_map().reachability;
self.push_scope_with_parent(node, Some(parent), reachability);
self.push_scope_with_parent(node, Some(self.current_scope()));
}

fn push_scope_with_parent(
&mut self,
node: NodeWithScopeRef,
parent: Option<FileScopeId>,
reachability: ScopedReachabilityConstraintId,
) {
fn push_scope_with_parent(&mut self, node: NodeWithScopeRef, parent: Option<FileScopeId>) {
let children_start = self.scopes.next_index() + 1;

// Note `node` is guaranteed to be a child of `self.module`
let node_with_kind = node.to_kind(self.module);

let scope = Scope::new(
parent,
node_with_kind,
children_start..children_start,
reachability,
);
let scope = Scope::new(parent, node_with_kind, children_start..children_start);
let is_class_scope = scope.kind().is_class();
self.try_node_context_stack_manager.enter_nested_scope();

Expand Down Expand Up @@ -1742,14 +1725,6 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {

assert_eq!(&self.current_assignments, &[]);

for scope in &self.scopes {
if let Some(parent) = scope.parent() {
self.use_def_maps[parent]
.reachability_constraints
.mark_used(scope.reachability());
}
}

let mut place_tables: IndexVec<_, _> = self
.place_tables
.into_iter()
Expand Down
13 changes: 1 addition & 12 deletions crates/ty_python_semantic/src/semantic_index/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use crate::{
Db,
ast_node_ref::AstNodeRef,
node_key::NodeKey,
semantic_index::{
SemanticIndex, reachability_constraints::ScopedReachabilityConstraintId, semantic_index,
},
semantic_index::{SemanticIndex, semantic_index},
types::{GenericContext, binding_type, infer_definition_types},
};

Expand Down Expand Up @@ -111,23 +109,18 @@ pub(crate) struct Scope {

/// The range of [`FileScopeId`]s that are descendants of this scope.
descendants: Range<FileScopeId>,

/// The constraint that determines the reachability of this scope.
reachability: ScopedReachabilityConstraintId,
}

impl Scope {
pub(super) fn new(
parent: Option<FileScopeId>,
node: NodeWithScopeKind,
descendants: Range<FileScopeId>,
reachability: ScopedReachabilityConstraintId,
) -> Self {
Scope {
parent,
node,
descendants,
reachability,
}
}

Expand Down Expand Up @@ -158,10 +151,6 @@ impl Scope {
pub(crate) fn is_eager(&self) -> bool {
self.kind().is_eager()
}

pub(crate) fn reachability(&self) -> ScopedReachabilityConstraintId {
self.reachability
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, get_size2::GetSize)]
Expand Down
3 changes: 2 additions & 1 deletion crates/ty_python_semantic/src/types/ide_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,8 @@ pub fn type_hierarchy_subtypes(db: &dyn Db, ty: Type<'_>) -> Vec<TypeHierarchyCl
}

let file_scope_id = scope_id.file_scope_id(db);
if !index.is_scope_reachable(db, file_scope_id) {
let parsed = parsed_module(db, file).load(db);
if !index.is_range_reachable(db, file_scope_id, class_node.node(&parsed).range()) {
continue;
}

Expand Down
Loading