From a0a8cb4ed3cafac4be63f60733d8db538b3c1101 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 1 May 2025 08:31:24 +0200 Subject: [PATCH 1/2] [red-knot] Cache source type during semanic index building --- .../red_knot_python_semantic/src/semantic_index/builder.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/red_knot_python_semantic/src/semantic_index/builder.rs b/crates/red_knot_python_semantic/src/semantic_index/builder.rs index a633c2c5cfc5d..2e8247b8087d6 100644 --- a/crates/red_knot_python_semantic/src/semantic_index/builder.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/builder.rs @@ -10,7 +10,7 @@ use ruff_db::source::{source_text, SourceText}; use ruff_index::IndexVec; use ruff_python_ast::name::Name; use ruff_python_ast::visitor::{walk_expr, walk_pattern, walk_stmt, Visitor}; -use ruff_python_ast::{self as ast, PythonVersion}; +use ruff_python_ast::{self as ast, PySourceType, PythonVersion}; use ruff_python_parser::semantic_errors::{ SemanticSyntaxChecker, SemanticSyntaxContext, SemanticSyntaxError, }; @@ -75,6 +75,7 @@ pub(super) struct SemanticIndexBuilder<'db> { // Builder state db: &'db dyn Db, file: File, + source_type: PySourceType, module: &'db ParsedModule, scope_stack: Vec, /// The assignments we're currently visiting, with @@ -118,6 +119,7 @@ impl<'db> SemanticIndexBuilder<'db> { let mut builder = Self { db, file, + source_type: file.source_type(db), module: parsed, scope_stack: Vec::new(), current_assignments: vec![], @@ -445,7 +447,7 @@ impl<'db> SemanticIndexBuilder<'db> { #[allow(unsafe_code)] // SAFETY: `definition_node` is guaranteed to be a child of `self.module` let kind = unsafe { definition_node.into_owned(self.module.clone()) }; - let category = kind.category(self.file.is_stub(self.db.upcast())); + let category = kind.category(self.source_type.is_stub()); let is_reexported = kind.is_reexported(); let definition = Definition::new( From f9cde10df384a0ee34c2a5c517e3b35daf44ea35 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 1 May 2025 08:41:55 +0200 Subject: [PATCH 2/2] Update crates/red_knot_python_semantic/src/semantic_index/builder.rs Co-authored-by: Alex Waygood --- crates/red_knot_python_semantic/src/semantic_index/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/red_knot_python_semantic/src/semantic_index/builder.rs b/crates/red_knot_python_semantic/src/semantic_index/builder.rs index 2e8247b8087d6..75c7494b4327f 100644 --- a/crates/red_knot_python_semantic/src/semantic_index/builder.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/builder.rs @@ -119,7 +119,7 @@ impl<'db> SemanticIndexBuilder<'db> { let mut builder = Self { db, file, - source_type: file.source_type(db), + source_type: file.source_type(db.upcast()), module: parsed, scope_stack: Vec::new(), current_assignments: vec![],