From 2abfab0f9b58dabea923cc9c662fe83b7d997466 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 9 Aug 2024 11:50:45 +0200 Subject: [PATCH] Move Program and related structs to `red_knot_python_semantic` (#12777) --- Cargo.lock | 3 +++ crates/red_knot/src/main.rs | 2 +- crates/red_knot/src/target_version.rs | 4 ++-- crates/red_knot/tests/file_watching.rs | 5 +++-- crates/red_knot_python_semantic/src/lib.rs | 2 ++ .../src/module_resolver/path.rs | 5 ++--- .../src/module_resolver/resolver.rs | 12 ++++++------ .../src/module_resolver/state.rs | 2 +- .../src/module_resolver/testing.rs | 2 +- .../src/module_resolver/typeshed/versions.rs | 6 ++---- .../src/program.rs | 3 ++- .../red_knot_python_semantic/src/semantic_model.rs | 2 +- crates/red_knot_python_semantic/src/types/infer.rs | 2 +- crates/red_knot_server/Cargo.toml | 1 + crates/red_knot_server/src/session.rs | 2 +- crates/red_knot_wasm/Cargo.toml | 1 + crates/red_knot_wasm/src/lib.rs | 4 ++-- crates/red_knot_workspace/src/db.rs | 5 +++-- crates/red_knot_workspace/src/lint.rs | 5 +++-- crates/red_knot_workspace/tests/check.rs | 2 +- crates/ruff_benchmark/Cargo.toml | 1 + crates/ruff_benchmark/benches/red_knot.rs | 2 +- crates/ruff_db/src/lib.rs | 1 - 23 files changed, 41 insertions(+), 33 deletions(-) rename crates/{ruff_db => red_knot_python_semantic}/src/program.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index abfd8003060e0..970f8db7f5b24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1918,6 +1918,7 @@ dependencies = [ "libc", "lsp-server", "lsp-types", + "red_knot_python_semantic", "red_knot_workspace", "ruff_db", "ruff_linter", @@ -1941,6 +1942,7 @@ dependencies = [ "console_log", "js-sys", "log", + "red_knot_python_semantic", "red_knot_workspace", "ruff_db", "ruff_notebook", @@ -2104,6 +2106,7 @@ dependencies = [ "criterion", "mimalloc", "once_cell", + "red_knot_python_semantic", "red_knot_workspace", "ruff_db", "ruff_linter", diff --git a/crates/red_knot/src/main.rs b/crates/red_knot/src/main.rs index e6366db3a9fdf..84a621009a858 100644 --- a/crates/red_knot/src/main.rs +++ b/crates/red_knot/src/main.rs @@ -7,13 +7,13 @@ use colored::Colorize; use crossbeam::channel as crossbeam_channel; use salsa::plumbing::ZalsaDatabase; +use red_knot_python_semantic::{ProgramSettings, SearchPathSettings}; use red_knot_server::run_server; use red_knot_workspace::db::RootDatabase; use red_knot_workspace::site_packages::site_packages_dirs_of_venv; use red_knot_workspace::watch; use red_knot_workspace::watch::WorkspaceWatcher; use red_knot_workspace::workspace::WorkspaceMetadata; -use ruff_db::program::{ProgramSettings, SearchPathSettings}; use ruff_db::system::{OsSystem, System, SystemPath, SystemPathBuf}; use target_version::TargetVersion; diff --git a/crates/red_knot/src/target_version.rs b/crates/red_knot/src/target_version.rs index b636227271e37..43e249a6c57e0 100644 --- a/crates/red_knot/src/target_version.rs +++ b/crates/red_knot/src/target_version.rs @@ -15,11 +15,11 @@ pub enum TargetVersion { impl std::fmt::Display for TargetVersion { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ruff_db::program::TargetVersion::from(*self).fmt(f) + red_knot_python_semantic::TargetVersion::from(*self).fmt(f) } } -impl From for ruff_db::program::TargetVersion { +impl From for red_knot_python_semantic::TargetVersion { fn from(value: TargetVersion) -> Self { match value { TargetVersion::Py37 => Self::Py37, diff --git a/crates/red_knot/tests/file_watching.rs b/crates/red_knot/tests/file_watching.rs index 42d8cca9adb86..7e45ec6027dbc 100644 --- a/crates/red_knot/tests/file_watching.rs +++ b/crates/red_knot/tests/file_watching.rs @@ -6,13 +6,14 @@ use std::time::Duration; use anyhow::{anyhow, Context}; use salsa::Setter; -use red_knot_python_semantic::{resolve_module, ModuleName}; +use red_knot_python_semantic::{ + resolve_module, ModuleName, Program, ProgramSettings, SearchPathSettings, TargetVersion, +}; use red_knot_workspace::db::RootDatabase; use red_knot_workspace::watch; use red_knot_workspace::watch::{directory_watcher, WorkspaceWatcher}; use red_knot_workspace::workspace::WorkspaceMetadata; use ruff_db::files::{system_path_to_file, File, FileError}; -use ruff_db::program::{Program, ProgramSettings, SearchPathSettings, TargetVersion}; use ruff_db::source::source_text; use ruff_db::system::{OsSystem, SystemPath, SystemPathBuf}; use ruff_db::Upcast; diff --git a/crates/red_knot_python_semantic/src/lib.rs b/crates/red_knot_python_semantic/src/lib.rs index 998d85bf2f822..bae2308900dd8 100644 --- a/crates/red_knot_python_semantic/src/lib.rs +++ b/crates/red_knot_python_semantic/src/lib.rs @@ -5,6 +5,7 @@ use rustc_hash::FxHasher; pub use db::Db; pub use module_name::ModuleName; pub use module_resolver::{resolve_module, system_module_search_paths, vendored_typeshed_stubs}; +pub use program::{Program, ProgramSettings, SearchPathSettings, TargetVersion}; pub use semantic_model::{HasTy, SemanticModel}; pub mod ast_node_ref; @@ -13,6 +14,7 @@ mod db; mod module_name; mod module_resolver; mod node_key; +mod program; pub mod semantic_index; mod semantic_model; pub mod types; diff --git a/crates/red_knot_python_semantic/src/module_resolver/path.rs b/crates/red_knot_python_semantic/src/module_resolver/path.rs index c2ccf2e439f89..b91831de46342 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/path.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/path.rs @@ -620,14 +620,13 @@ impl PartialEq for VendoredPathBuf { #[cfg(test)] mod tests { - use ruff_db::program::TargetVersion; use ruff_db::Db; use crate::db::tests::TestDb; - use crate::module_resolver::testing::{FileSpec, MockedTypeshed, TestCase, TestCaseBuilder}; - use super::*; + use crate::module_resolver::testing::{FileSpec, MockedTypeshed, TestCase, TestCaseBuilder}; + use crate::TargetVersion; impl ModulePath { #[must_use] diff --git a/crates/red_knot_python_semantic/src/module_resolver/resolver.rs b/crates/red_knot_python_semantic/src/module_resolver/resolver.rs index 170a967ae38f1..b8d37e0584bc1 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/resolver.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/resolver.rs @@ -1,18 +1,18 @@ use std::borrow::Cow; use std::iter::FusedIterator; +use rustc_hash::{FxBuildHasher, FxHashSet}; + use ruff_db::files::{File, FilePath, FileRootKind}; -use ruff_db::program::{Program, SearchPathSettings, TargetVersion}; use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf}; use ruff_db::vendored::VendoredPath; -use rustc_hash::{FxBuildHasher, FxHashSet}; - -use crate::db::Db; -use crate::module_name::ModuleName; use super::module::{Module, ModuleKind}; use super::path::{ModulePath, SearchPath, SearchPathValidationError}; use super::state::ResolverState; +use crate::db::Db; +use crate::module_name::ModuleName; +use crate::{Program, SearchPathSettings, TargetVersion}; /// Resolves a module name to a module. pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option { @@ -1143,7 +1143,7 @@ mod tests { fn symlink() -> anyhow::Result<()> { use anyhow::Context; - use ruff_db::program::Program; + use crate::program::Program; use ruff_db::system::{OsSystem, SystemPath}; use crate::db::tests::TestDb; diff --git a/crates/red_knot_python_semantic/src/module_resolver/state.rs b/crates/red_knot_python_semantic/src/module_resolver/state.rs index 1f5c244fdd6c1..1b16e13e40091 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/state.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/state.rs @@ -1,8 +1,8 @@ -use ruff_db::program::TargetVersion; use ruff_db::vendored::VendoredFileSystem; use super::typeshed::LazyTypeshedVersions; use crate::db::Db; +use crate::TargetVersion; pub(crate) struct ResolverState<'db> { pub(crate) db: &'db dyn Db, diff --git a/crates/red_knot_python_semantic/src/module_resolver/testing.rs b/crates/red_knot_python_semantic/src/module_resolver/testing.rs index 8d30156521bbe..218761c16f7db 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/testing.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/testing.rs @@ -1,8 +1,8 @@ -use ruff_db::program::{Program, SearchPathSettings, TargetVersion}; use ruff_db::system::{DbWithTestSystem, SystemPath, SystemPathBuf}; use ruff_db::vendored::VendoredPathBuf; use crate::db::tests::TestDb; +use crate::program::{Program, SearchPathSettings, TargetVersion}; /// A test case for the module resolver. /// diff --git a/crates/red_knot_python_semantic/src/module_resolver/typeshed/versions.rs b/crates/red_knot_python_semantic/src/module_resolver/typeshed/versions.rs index 03cc83f66e156..778dedf7d6dbe 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/typeshed/versions.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/typeshed/versions.rs @@ -6,16 +6,15 @@ use std::ops::{RangeFrom, RangeInclusive}; use std::str::FromStr; use once_cell::sync::Lazy; -use ruff_db::program::TargetVersion; use ruff_db::system::SystemPath; use rustc_hash::FxHashMap; use ruff_db::files::{system_path_to_file, File}; +use super::vendored::vendored_typeshed_stubs; use crate::db::Db; use crate::module_name::ModuleName; - -use super::vendored::vendored_typeshed_stubs; +use crate::TargetVersion; #[derive(Debug)] pub(crate) struct LazyTypeshedVersions<'db>(OnceCell<&'db TypeshedVersions>); @@ -440,7 +439,6 @@ mod tests { use std::path::Path; use insta::assert_snapshot; - use ruff_db::program::TargetVersion; use super::*; diff --git a/crates/ruff_db/src/program.rs b/crates/red_knot_python_semantic/src/program.rs similarity index 98% rename from crates/ruff_db/src/program.rs rename to crates/red_knot_python_semantic/src/program.rs index fbdf198824e9f..22b3dfa68c9e8 100644 --- a/crates/ruff_db/src/program.rs +++ b/crates/red_knot_python_semantic/src/program.rs @@ -1,4 +1,5 @@ -use crate::{system::SystemPathBuf, Db}; +use crate::Db; +use ruff_db::system::SystemPathBuf; use salsa::Durability; #[salsa::input(singleton)] diff --git a/crates/red_knot_python_semantic/src/semantic_model.rs b/crates/red_knot_python_semantic/src/semantic_model.rs index 159cd62c077f5..602777e102428 100644 --- a/crates/red_knot_python_semantic/src/semantic_model.rs +++ b/crates/red_knot_python_semantic/src/semantic_model.rs @@ -165,10 +165,10 @@ impl HasTy for ast::Alias { mod tests { use ruff_db::files::system_path_to_file; use ruff_db::parsed::parsed_module; - use ruff_db::program::{Program, SearchPathSettings, TargetVersion}; use ruff_db::system::{DbWithTestSystem, SystemPathBuf}; use crate::db::tests::TestDb; + use crate::program::{Program, SearchPathSettings, TargetVersion}; use crate::types::Type; use crate::{HasTy, SemanticModel}; diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index f8ad51e8362f6..29b9c7ce16bb7 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -1496,13 +1496,13 @@ impl<'db> TypeInferenceBuilder<'db> { mod tests { use ruff_db::files::{system_path_to_file, File}; use ruff_db::parsed::parsed_module; - use ruff_db::program::{Program, SearchPathSettings, TargetVersion}; use ruff_db::system::{DbWithTestSystem, SystemPathBuf}; use ruff_db::testing::assert_function_query_was_not_run; use ruff_python_ast::name::Name; use crate::builtins::builtins_scope; use crate::db::tests::TestDb; + use crate::program::{Program, SearchPathSettings, TargetVersion}; 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}; diff --git a/crates/red_knot_server/Cargo.toml b/crates/red_knot_server/Cargo.toml index 01be751854c7d..81c2302bdb6ed 100644 --- a/crates/red_knot_server/Cargo.toml +++ b/crates/red_knot_server/Cargo.toml @@ -11,6 +11,7 @@ repository = { workspace = true } license = { workspace = true } [dependencies] +red_knot_python_semantic = { workspace = true } red_knot_workspace = { workspace = true } ruff_db = { workspace = true } ruff_linter = { workspace = true } diff --git a/crates/red_knot_server/src/session.rs b/crates/red_knot_server/src/session.rs index 2c8e1209e5859..1236f51b71a8f 100644 --- a/crates/red_knot_server/src/session.rs +++ b/crates/red_knot_server/src/session.rs @@ -8,10 +8,10 @@ use std::sync::Arc; use anyhow::anyhow; use lsp_types::{ClientCapabilities, Url}; +use red_knot_python_semantic::{ProgramSettings, SearchPathSettings, TargetVersion}; use red_knot_workspace::db::RootDatabase; use red_knot_workspace::workspace::WorkspaceMetadata; use ruff_db::files::{system_path_to_file, File}; -use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion}; use ruff_db::system::SystemPath; use crate::edit::{DocumentKey, NotebookDocument}; diff --git a/crates/red_knot_wasm/Cargo.toml b/crates/red_knot_wasm/Cargo.toml index 21bafe469aa61..df70eaa9b39db 100644 --- a/crates/red_knot_wasm/Cargo.toml +++ b/crates/red_knot_wasm/Cargo.toml @@ -19,6 +19,7 @@ doctest = false default = ["console_error_panic_hook"] [dependencies] +red_knot_python_semantic = { workspace = true } red_knot_workspace = { workspace = true } ruff_db = { workspace = true } diff --git a/crates/red_knot_wasm/src/lib.rs b/crates/red_knot_wasm/src/lib.rs index 0576a61992215..70d2020798d64 100644 --- a/crates/red_knot_wasm/src/lib.rs +++ b/crates/red_knot_wasm/src/lib.rs @@ -3,10 +3,10 @@ use std::any::Any; use js_sys::Error; use wasm_bindgen::prelude::*; +use red_knot_python_semantic::{ProgramSettings, SearchPathSettings}; use red_knot_workspace::db::RootDatabase; use red_knot_workspace::workspace::WorkspaceMetadata; use ruff_db::files::{system_path_to_file, File}; -use ruff_db::program::{ProgramSettings, SearchPathSettings}; use ruff_db::system::walk_directory::WalkDirectoryBuilder; use ruff_db::system::{ DirectoryEntry, MemoryFileSystem, Metadata, System, SystemPath, SystemPathBuf, @@ -184,7 +184,7 @@ pub enum TargetVersion { Py313, } -impl From for ruff_db::program::TargetVersion { +impl From for red_knot_python_semantic::TargetVersion { fn from(value: TargetVersion) -> Self { match value { TargetVersion::Py37 => Self::Py37, diff --git a/crates/red_knot_workspace/src/db.rs b/crates/red_knot_workspace/src/db.rs index c03b5c6aed61c..a3abfec07e126 100644 --- a/crates/red_knot_workspace/src/db.rs +++ b/crates/red_knot_workspace/src/db.rs @@ -1,9 +1,10 @@ use std::panic::RefUnwindSafe; use std::sync::Arc; -use red_knot_python_semantic::{vendored_typeshed_stubs, Db as SemanticDb}; +use red_knot_python_semantic::{ + vendored_typeshed_stubs, Db as SemanticDb, Program, ProgramSettings, +}; use ruff_db::files::{File, Files}; -use ruff_db::program::{Program, ProgramSettings}; use ruff_db::system::System; use ruff_db::vendored::VendoredFileSystem; use ruff_db::{Db as SourceDb, Upcast}; diff --git a/crates/red_knot_workspace/src/lint.rs b/crates/red_knot_workspace/src/lint.rs index f331641dedd6d..0165c4bea6839 100644 --- a/crates/red_knot_workspace/src/lint.rs +++ b/crates/red_knot_workspace/src/lint.rs @@ -305,13 +305,14 @@ enum AnyImportRef<'a> { #[cfg(test)] mod tests { + use red_knot_python_semantic::{Program, SearchPathSettings, TargetVersion}; use ruff_db::files::system_path_to_file; - use ruff_db::program::{Program, SearchPathSettings, TargetVersion}; use ruff_db::system::{DbWithTestSystem, SystemPathBuf}; - use super::{lint_semantic, Diagnostics}; use crate::db::tests::TestDb; + use super::{lint_semantic, Diagnostics}; + fn setup_db() -> TestDb { setup_db_with_root(SystemPathBuf::from("/src")) } diff --git a/crates/red_knot_workspace/tests/check.rs b/crates/red_knot_workspace/tests/check.rs index ffc6f2721c636..dfb4a6e540dd5 100644 --- a/crates/red_knot_workspace/tests/check.rs +++ b/crates/red_knot_workspace/tests/check.rs @@ -1,8 +1,8 @@ +use red_knot_python_semantic::{ProgramSettings, SearchPathSettings, TargetVersion}; use red_knot_workspace::db::RootDatabase; use red_knot_workspace::lint::lint_semantic; use red_knot_workspace::workspace::WorkspaceMetadata; use ruff_db::files::system_path_to_file; -use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion}; use ruff_db::system::{OsSystem, SystemPathBuf}; use std::fs; use std::path::PathBuf; diff --git a/crates/ruff_benchmark/Cargo.toml b/crates/ruff_benchmark/Cargo.toml index cf4e5dbaec2ed..3efe932a143f0 100644 --- a/crates/ruff_benchmark/Cargo.toml +++ b/crates/ruff_benchmark/Cargo.toml @@ -52,6 +52,7 @@ ruff_python_ast = { workspace = true } ruff_python_formatter = { workspace = true } ruff_python_parser = { workspace = true } ruff_python_trivia = { workspace = true } +red_knot_python_semantic = { workspace = true } red_knot_workspace = { workspace = true } [lints] diff --git a/crates/ruff_benchmark/benches/red_knot.rs b/crates/ruff_benchmark/benches/red_knot.rs index 5dc752b54cbc3..95bf0b7d1e057 100644 --- a/crates/ruff_benchmark/benches/red_knot.rs +++ b/crates/ruff_benchmark/benches/red_knot.rs @@ -1,11 +1,11 @@ #![allow(clippy::disallowed_names)] +use red_knot_python_semantic::{ProgramSettings, SearchPathSettings, TargetVersion}; use red_knot_workspace::db::RootDatabase; use red_knot_workspace::workspace::WorkspaceMetadata; use ruff_benchmark::criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use ruff_benchmark::TestFile; use ruff_db::files::{system_path_to_file, File}; -use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion}; use ruff_db::source::source_text; use ruff_db::system::{MemoryFileSystem, SystemPath, TestSystem}; diff --git a/crates/ruff_db/src/lib.rs b/crates/ruff_db/src/lib.rs index f7582bfa9d33f..df3fb4784d7a8 100644 --- a/crates/ruff_db/src/lib.rs +++ b/crates/ruff_db/src/lib.rs @@ -9,7 +9,6 @@ use crate::vendored::VendoredFileSystem; pub mod file_revision; pub mod files; pub mod parsed; -pub mod program; pub mod source; pub mod system; #[cfg(feature = "testing")]