From 52941b980110cd5ba3a430d7418031d98ee3d090 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 2 Sep 2024 14:17:59 +0100 Subject: [PATCH] Move things around --- crates/red_knot_python_semantic/src/lib.rs | 2 +- crates/red_knot_python_semantic/src/stdlib.rs | 39 ++++++++++++++++++ crates/red_knot_python_semantic/src/types.rs | 40 +------------------ .../src/types/infer.rs | 4 +- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/crates/red_knot_python_semantic/src/lib.rs b/crates/red_knot_python_semantic/src/lib.rs index ac512fad3bb9b8..f159bbf9047ff0 100644 --- a/crates/red_knot_python_semantic/src/lib.rs +++ b/crates/red_knot_python_semantic/src/lib.rs @@ -10,7 +10,6 @@ pub use python_version::PythonVersion; pub use semantic_model::{HasTy, SemanticModel}; pub mod ast_node_ref; -mod stdlib; mod db; mod module_name; mod module_resolver; @@ -20,6 +19,7 @@ mod python_version; pub mod semantic_index; mod semantic_model; pub(crate) mod site_packages; +mod stdlib; pub mod types; type FxOrderSet = ordermap::set::OrderSet>; diff --git a/crates/red_knot_python_semantic/src/stdlib.rs b/crates/red_knot_python_semantic/src/stdlib.rs index 0886456fa3ae93..85dd3590419143 100644 --- a/crates/red_knot_python_semantic/src/stdlib.rs +++ b/crates/red_knot_python_semantic/src/stdlib.rs @@ -2,8 +2,47 @@ use crate::module_name::ModuleName; use crate::module_resolver::resolve_module; use crate::semantic_index::global_scope; use crate::semantic_index::symbol::ScopeId; +use crate::types::{symbol_ty_by_name, Type}; use crate::Db; +/// Enumeration of various core stdlib modules, for which we have dedicated Salsa queries. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum CoreStdlibModule { + Builtins, + Types, + Typeshed, +} + +impl CoreStdlibModule { + /// Retrieve the global scope of the given module. + /// + /// Returns `None` if the given module isn't available for some reason. + pub(crate) fn global_scope(self, db: &dyn Db) -> Option> { + match self { + Self::Builtins => builtins_scope(db), + Self::Types => types_scope(db), + Self::Typeshed => typeshed_scope(db), + } + } + + /// Shorthand for `symbol_ty` that looks up a symbol in the scope of a given core module. + /// + /// Returns `Unbound` if the given module isn't available for some reason. + pub(crate) fn symbol_ty_by_name<'db>(self, db: &'db dyn Db, name: &str) -> Type<'db> { + self.global_scope(db) + .map(|globals| symbol_ty_by_name(db, globals, name)) + .unwrap_or(Type::Unbound) + } +} + +/// Shorthand for `symbol_ty` that looks up a symbol in the `builtins` scope. +/// +/// Returns `Unbound` if the `builtins` module isn't available for some reason. +#[inline] +pub(crate) fn builtins_symbol_ty_by_name<'db>(db: &'db dyn Db, name: &str) -> Type<'db> { + CoreStdlibModule::Builtins.symbol_ty_by_name(db, name) +} + /// Salsa query to get the builtins scope. /// /// Can return None if a custom typeshed is used that is missing `builtins.pyi`. diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index aa71f163ca55ec..6daae8b5247177 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -8,7 +8,7 @@ use crate::semantic_index::{ global_scope, semantic_index, symbol_table, use_def_map, DefinitionWithConstraints, DefinitionWithConstraintsIterator, }; -use crate::stdlib::{builtins_scope, types_scope, typeshed_scope}; +use crate::stdlib::{builtins_symbol_ty_by_name, CoreStdlibModule}; use crate::types::narrow::narrowing_constraint; use crate::{Db, FxOrderSet}; @@ -75,44 +75,6 @@ pub(crate) fn global_symbol_ty_by_name<'db>(db: &'db dyn Db, file: File, name: & symbol_ty_by_name(db, global_scope(db, file), name) } -/// Enumeration of various core stdlib modules, for which we have dedicated Salsa queries. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum CoreStdlibModule { - Builtins, - Types, - Typeshed, -} - -impl CoreStdlibModule { - /// Retrieve the global scope of the given module. - /// - /// Returns `None` if the given module isn't available for some reason. - pub(crate) fn global_scope(self, db: &dyn Db) -> Option> { - match self { - Self::Builtins => builtins_scope(db), - Self::Types => types_scope(db), - Self::Typeshed => typeshed_scope(db), - } - } - - /// Shorthand for `symbol_ty` that looks up a symbol in the scope of a given core module. - /// - /// Returns `Unbound` if the given module isn't available for some reason. - pub(crate) fn symbol_ty_by_name<'db>(self, db: &'db dyn Db, name: &str) -> Type<'db> { - self.global_scope(db) - .map(|globals| symbol_ty_by_name(db, globals, name)) - .unwrap_or(Type::Unbound) - } -} - -/// Shorthand for `symbol_ty` that looks up a symbol in the `builtins` scope. -/// -/// Returns `Unbound` if the `builtins` module isn't available for some reason. -#[inline] -pub(crate) fn builtins_symbol_ty_by_name<'db>(db: &'db dyn Db, name: &str) -> Type<'db> { - CoreStdlibModule::Builtins.symbol_ty_by_name(db, name) -} - /// Infer the type of a [`Definition`]. pub(crate) fn definition_ty<'db>(db: &'db dyn Db, definition: Definition<'db>) -> Type<'db> { let inference = infer_definition_types(db, definition); diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 4df369f1415ba8..2fe02bf6f6de01 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -37,7 +37,6 @@ use ruff_db::parsed::parsed_module; use ruff_python_ast::{self as ast, AnyNodeRef, ExprContext, UnaryOp}; use ruff_text_size::Ranged; -use crate::stdlib::builtins_scope; use crate::module_name::ModuleName; use crate::module_resolver::{file_to_module, resolve_module}; use crate::semantic_index::ast_ids::{HasScopedAstId, HasScopedUseId, ScopedExpressionId}; @@ -46,6 +45,7 @@ use crate::semantic_index::expression::Expression; use crate::semantic_index::semantic_index; use crate::semantic_index::symbol::{FileScopeId, NodeWithScopeKind, NodeWithScopeRef, ScopeId}; use crate::semantic_index::SemanticIndex; +use crate::stdlib::builtins_scope; use crate::types::diagnostic::{TypeCheckDiagnostic, TypeCheckDiagnostics}; use crate::types::{ builtins_symbol_ty_by_name, definitions_ty, global_symbol_ty_by_name, symbol_ty, @@ -2319,13 +2319,13 @@ mod tests { use ruff_db::testing::assert_function_query_was_not_run; use ruff_python_ast::name::Name; - use crate::stdlib::builtins_scope; use crate::db::tests::TestDb; use crate::program::{Program, SearchPathSettings}; use crate::python_version::PythonVersion; use crate::semantic_index::definition::Definition; use crate::semantic_index::symbol::FileScopeId; use crate::semantic_index::{global_scope, semantic_index, symbol_table, use_def_map}; + use crate::stdlib::builtins_scope; use crate::types::{global_symbol_ty_by_name, infer_definition_types, symbol_ty_by_name, Type}; use crate::{HasTy, ProgramSettings, SemanticModel};