From 9c57862262558c08dae8b4e488444cac611f45cf Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 1 May 2025 08:51:53 +0200 Subject: [PATCH] [red-knot] Cache source type during semanic index building (#17756) Co-authored-by: Alex Waygood --- .../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 a633c2c5cf..75c7494b43 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.upcast()), 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(