diff --git a/Cargo.lock b/Cargo.lock index 30739daba811f..56506e2e85944 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1978,6 +1978,8 @@ dependencies = [ "notify", "parking_lot", "rayon", + "red_knot_python_semantic", + "ruff_db", "ruff_index", "ruff_notebook", "ruff_python_ast", @@ -1994,6 +1996,28 @@ dependencies = [ "zip", ] +[[package]] +name = "red_knot_python_semantic" +version = "0.0.0" +dependencies = [ + "anyhow", + "bitflags 2.5.0", + "hashbrown 0.14.5", + "indexmap", + "ruff_db", + "ruff_index", + "ruff_python_ast", + "ruff_python_parser", + "ruff_python_stdlib", + "ruff_text_size", + "rustc-hash", + "salsa-2022", + "smallvec", + "smol_str", + "tempfile", + "tracing", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -2470,10 +2494,7 @@ version = "0.0.0" dependencies = [ "anyhow", "bitflags 2.5.0", - "hashbrown 0.14.5", - "indexmap", "is-macro", - "ruff_db", "ruff_index", "ruff_python_ast", "ruff_python_parser", @@ -2481,11 +2502,8 @@ dependencies = [ "ruff_source_file", "ruff_text_size", "rustc-hash", - "salsa-2022", - "smallvec", "smol_str", "tempfile", - "tracing", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c0133b6cc8825..fd1ab491e0f0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,8 @@ ruff_source_file = { path = "crates/ruff_source_file" } ruff_text_size = { path = "crates/ruff_text_size" } ruff_workspace = { path = "crates/ruff_workspace" } +red_knot_python_semantic = { path = "crates/red_knot_python_semantic" } + aho-corasick = { version = "1.1.3" } annotate-snippets = { version = "0.9.2", features = ["color"] } anyhow = { version = "1.0.80" } diff --git a/crates/red_knot/Cargo.toml b/crates/red_knot/Cargo.toml index 26f0e7dde3a9d..77b60e0749e23 100644 --- a/crates/red_knot/Cargo.toml +++ b/crates/red_knot/Cargo.toml @@ -12,6 +12,9 @@ license.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +red_knot_python_semantic = { workspace = true } + +ruff_db = { workspace = true } ruff_python_parser = { workspace = true } ruff_python_ast = { workspace = true } ruff_python_stdlib = { workspace = true } diff --git a/crates/red_knot_python_semantic/Cargo.toml b/crates/red_knot_python_semantic/Cargo.toml new file mode 100644 index 0000000000000..35e5f5297c236 --- /dev/null +++ b/crates/red_knot_python_semantic/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "red_knot_python_semantic" +version = "0.0.0" +publish = false +authors = { workspace = true } +edition = { workspace = true } +rust-version = { workspace = true } +homepage = { workspace = true } +documentation = { workspace = true } +repository = { workspace = true } +license = { workspace = true } + +[dependencies] +ruff_db = { workspace = true } +ruff_index = { workspace = true } +ruff_python_ast = { workspace = true } +ruff_python_stdlib = { workspace = true } +ruff_text_size = { workspace = true } + +bitflags = { workspace = true } +indexmap = { workspace = true } +salsa = { workspace = true } +smallvec = { workspace = true } +smol_str = { workspace = true } +tracing = { workspace = true } +rustc-hash = { workspace = true } +hashbrown = { workspace = true } + +[dev-dependencies] +anyhow = { workspace = true } +ruff_python_parser = { workspace = true } +tempfile = { workspace = true } + +[lints] +workspace = true + diff --git a/crates/ruff_python_semantic/src/red_knot/ast_node_ref.rs b/crates/red_knot_python_semantic/src/ast_node_ref.rs similarity index 98% rename from crates/ruff_python_semantic/src/red_knot/ast_node_ref.rs rename to crates/red_knot_python_semantic/src/ast_node_ref.rs index b3e58e2237c94..118a1918a3634 100644 --- a/crates/ruff_python_semantic/src/red_knot/ast_node_ref.rs +++ b/crates/red_knot_python_semantic/src/ast_node_ref.rs @@ -93,7 +93,7 @@ unsafe impl Sync for AstNodeRef where T: Sync {} #[cfg(test)] mod tests { - use crate::red_knot::ast_node_ref::AstNodeRef; + use crate::ast_node_ref::AstNodeRef; use ruff_db::parsed::ParsedModule; use ruff_python_ast::PySourceType; use ruff_python_parser::parse_unchecked_source; diff --git a/crates/ruff_python_semantic/src/db.rs b/crates/red_knot_python_semantic/src/db.rs similarity index 94% rename from crates/ruff_python_semantic/src/db.rs rename to crates/red_knot_python_semantic/src/db.rs index 5d8de7f916d04..a27a38d9c9106 100644 --- a/crates/ruff_python_semantic/src/db.rs +++ b/crates/red_knot_python_semantic/src/db.rs @@ -7,11 +7,9 @@ use crate::module::resolver::{ resolve_module_query, }; -use crate::red_knot::semantic_index::symbol::{ - public_symbols_map, scopes_map, PublicSymbolId, ScopeId, -}; -use crate::red_knot::semantic_index::{root_scope, semantic_index, symbol_table}; -use crate::red_knot::types::{infer_types, public_symbol_ty}; +use crate::semantic_index::symbol::{public_symbols_map, scopes_map, PublicSymbolId, ScopeId}; +use crate::semantic_index::{root_scope, semantic_index, symbol_table}; +use crate::types::{infer_types, public_symbol_ty}; #[salsa::jar(db=Db)] pub struct Jar( diff --git a/crates/red_knot_python_semantic/src/lib.rs b/crates/red_knot_python_semantic/src/lib.rs new file mode 100644 index 0000000000000..a37b0d9ec18d0 --- /dev/null +++ b/crates/red_knot_python_semantic/src/lib.rs @@ -0,0 +1,13 @@ +pub mod ast_node_ref; +mod db; +pub mod module; +pub mod name; +mod node_key; +pub mod semantic_index; +pub mod types; + +type FxIndexSet = indexmap::set::IndexSet>; + +pub use db::{Db, Jar}; +use rustc_hash::FxHasher; +use std::hash::BuildHasherDefault; diff --git a/crates/ruff_python_semantic/src/red_knot/mod.rs b/crates/red_knot_python_semantic/src/mod.rs similarity index 100% rename from crates/ruff_python_semantic/src/red_knot/mod.rs rename to crates/red_knot_python_semantic/src/mod.rs diff --git a/crates/ruff_python_semantic/src/module.rs b/crates/red_knot_python_semantic/src/module.rs similarity index 99% rename from crates/ruff_python_semantic/src/module.rs rename to crates/red_knot_python_semantic/src/module.rs index 26c7aef90441d..692a071ee196a 100644 --- a/crates/ruff_python_semantic/src/module.rs +++ b/crates/red_knot_python_semantic/src/module.rs @@ -312,7 +312,7 @@ struct ModuleSearchPathInner { /// for the standard library are moved higher up to match Python's semantics at runtime. /// /// [the order given in the typing spec]: https://typing.readthedocs.io/en/latest/spec/distributing.html#import-resolution-ordering -#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, is_macro::Is)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum ModuleSearchPathKind { /// "Extra" paths provided by the user in a config file, env var or CLI flag. /// E.g. mypy's `MYPYPATH` env var, or pyright's `stubPath` configuration setting diff --git a/crates/ruff_python_semantic/src/module/resolver.rs b/crates/red_knot_python_semantic/src/module/resolver.rs similarity index 100% rename from crates/ruff_python_semantic/src/module/resolver.rs rename to crates/red_knot_python_semantic/src/module/resolver.rs diff --git a/crates/ruff_python_semantic/src/name.rs b/crates/red_knot_python_semantic/src/name.rs similarity index 100% rename from crates/ruff_python_semantic/src/name.rs rename to crates/red_knot_python_semantic/src/name.rs diff --git a/crates/ruff_python_semantic/src/red_knot/node_key.rs b/crates/red_knot_python_semantic/src/node_key.rs similarity index 100% rename from crates/ruff_python_semantic/src/red_knot/node_key.rs rename to crates/red_knot_python_semantic/src/node_key.rs diff --git a/crates/ruff_python_semantic/src/red_knot/semantic_index.rs b/crates/red_knot_python_semantic/src/semantic_index.rs similarity index 97% rename from crates/ruff_python_semantic/src/red_knot/semantic_index.rs rename to crates/red_knot_python_semantic/src/semantic_index.rs index 9e17727c49d9b..f8696f8ca390a 100644 --- a/crates/ruff_python_semantic/src/red_knot/semantic_index.rs +++ b/crates/red_knot_python_semantic/src/semantic_index.rs @@ -8,10 +8,10 @@ use ruff_db::vfs::VfsFile; use ruff_index::{IndexSlice, IndexVec}; use ruff_python_ast as ast; -use crate::red_knot::node_key::NodeKey; -use crate::red_knot::semantic_index::ast_ids::{AstId, AstIds, ScopeClassId, ScopeFunctionId}; -use crate::red_knot::semantic_index::builder::SemanticIndexBuilder; -use crate::red_knot::semantic_index::symbol::{ +use crate::node_key::NodeKey; +use crate::semantic_index::ast_ids::{AstId, AstIds, ScopeClassId, ScopeFunctionId}; +use crate::semantic_index::builder::SemanticIndexBuilder; +use crate::semantic_index::symbol::{ FileScopeId, PublicSymbolId, Scope, ScopeId, ScopeKind, ScopeSymbolId, SymbolTable, }; use crate::Db; @@ -271,8 +271,8 @@ mod tests { use ruff_db::vfs::{system_path_to_file, VfsFile}; use crate::db::tests::TestDb; - use crate::red_knot::semantic_index::symbol::{FileScopeId, ScopeKind, SymbolTable}; - use crate::red_knot::semantic_index::{root_scope, semantic_index, symbol_table}; + use crate::semantic_index::symbol::{FileScopeId, ScopeKind, SymbolTable}; + use crate::semantic_index::{root_scope, semantic_index, symbol_table}; struct TestCase { db: TestDb, diff --git a/crates/ruff_python_semantic/src/red_knot/semantic_index/ast_ids.rs b/crates/red_knot_python_semantic/src/semantic_index/ast_ids.rs similarity index 98% rename from crates/ruff_python_semantic/src/red_knot/semantic_index/ast_ids.rs rename to crates/red_knot_python_semantic/src/semantic_index/ast_ids.rs index f142ecec6fa11..24a6d3594c4fb 100644 --- a/crates/ruff_python_semantic/src/red_knot/semantic_index/ast_ids.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/ast_ids.rs @@ -6,10 +6,10 @@ use ruff_index::{newtype_index, IndexVec}; use ruff_python_ast as ast; use ruff_python_ast::AnyNodeRef; -use crate::red_knot::ast_node_ref::AstNodeRef; -use crate::red_knot::node_key::NodeKey; -use crate::red_knot::semantic_index::semantic_index; -use crate::red_knot::semantic_index::symbol::{FileScopeId, ScopeId}; +use crate::ast_node_ref::AstNodeRef; +use crate::node_key::NodeKey; +use crate::semantic_index::semantic_index; +use crate::semantic_index::symbol::{FileScopeId, ScopeId}; use crate::Db; /// AST ids for a single scope. @@ -98,7 +98,6 @@ pub trait AstIdNode { /// ## Panics /// May panic if the node does not belongs to `file`'s AST or is outside of `scope`. It may also /// return an incorrect node if that's the case. - fn ast_id(&self, db: &dyn Db, file: VfsFile, scope: FileScopeId) -> AstId; /// Resolves the AST node for `id`. diff --git a/crates/ruff_python_semantic/src/red_knot/semantic_index/builder.rs b/crates/red_knot_python_semantic/src/semantic_index/builder.rs similarity index 98% rename from crates/ruff_python_semantic/src/red_knot/semantic_index/builder.rs rename to crates/red_knot_python_semantic/src/semantic_index/builder.rs index 7e549f9b30399..020752484a073 100644 --- a/crates/ruff_python_semantic/src/red_knot/semantic_index/builder.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/builder.rs @@ -8,18 +8,16 @@ use ruff_python_ast as ast; use ruff_python_ast::visitor::{walk_expr, walk_stmt, Visitor}; use crate::name::Name; -use crate::red_knot::node_key::NodeKey; -use crate::red_knot::semantic_index::ast_ids::{ +use crate::node_key::NodeKey; +use crate::semantic_index::ast_ids::{ AstId, AstIdsBuilder, ScopeAssignmentId, ScopeClassId, ScopeFunctionId, ScopeImportFromId, ScopeImportId, ScopeNamedExprId, }; -use crate::red_knot::semantic_index::definition::{ - Definition, ImportDefinition, ImportFromDefinition, -}; -use crate::red_knot::semantic_index::symbol::{ +use crate::semantic_index::definition::{Definition, ImportDefinition, ImportFromDefinition}; +use crate::semantic_index::symbol::{ FileScopeId, FileSymbolId, Scope, ScopeSymbolId, SymbolFlags, SymbolTableBuilder, }; -use crate::red_knot::semantic_index::{NodeWithScopeId, SemanticIndex}; +use crate::semantic_index::{NodeWithScopeId, SemanticIndex}; pub(super) struct SemanticIndexBuilder<'a> { // Builder state diff --git a/crates/ruff_python_semantic/src/red_knot/semantic_index/definition.rs b/crates/red_knot_python_semantic/src/semantic_index/definition.rs similarity index 97% rename from crates/ruff_python_semantic/src/red_knot/semantic_index/definition.rs rename to crates/red_knot_python_semantic/src/semantic_index/definition.rs index 9c91e99aa969b..3eb8f40c18c62 100644 --- a/crates/ruff_python_semantic/src/red_knot/semantic_index/definition.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/definition.rs @@ -1,4 +1,4 @@ -use crate::red_knot::semantic_index::ast_ids::{ +use crate::semantic_index::ast_ids::{ ScopeAnnotatedAssignmentId, ScopeAssignmentId, ScopeClassId, ScopeFunctionId, ScopeImportFromId, ScopeImportId, ScopeNamedExprId, }; diff --git a/crates/ruff_python_semantic/src/red_knot/semantic_index/symbol.rs b/crates/red_knot_python_semantic/src/semantic_index/symbol.rs similarity index 98% rename from crates/ruff_python_semantic/src/red_knot/semantic_index/symbol.rs rename to crates/red_knot_python_semantic/src/semantic_index/symbol.rs index 9cfb1a010f562..55b03de78892b 100644 --- a/crates/ruff_python_semantic/src/red_knot/semantic_index/symbol.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/symbol.rs @@ -14,8 +14,8 @@ use ruff_db::vfs::VfsFile; use ruff_index::{newtype_index, IndexVec}; use crate::name::Name; -use crate::red_knot::semantic_index::definition::Definition; -use crate::red_knot::semantic_index::{root_scope, semantic_index, symbol_table, SymbolMap}; +use crate::semantic_index::definition::Definition; +use crate::semantic_index::{root_scope, semantic_index, symbol_table, SymbolMap}; use crate::Db; #[derive(Eq, PartialEq, Debug)] diff --git a/crates/ruff_python_semantic/src/red_knot/types.rs b/crates/red_knot_python_semantic/src/types.rs similarity index 97% rename from crates/ruff_python_semantic/src/red_knot/types.rs rename to crates/red_knot_python_semantic/src/types.rs index 898a57b17ccda..dbd90dbcdbf15 100644 --- a/crates/ruff_python_semantic/src/red_knot/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -4,14 +4,14 @@ use ruff_index::newtype_index; use ruff_python_ast as ast; use crate::name::Name; -use crate::red_knot::semantic_index::ast_ids::{AstIdNode, ScopeAstIdNode}; -use crate::red_knot::semantic_index::symbol::{FileScopeId, PublicSymbolId, ScopeId}; -use crate::red_knot::semantic_index::{ +use crate::semantic_index::ast_ids::{AstIdNode, ScopeAstIdNode}; +use crate::semantic_index::symbol::{FileScopeId, PublicSymbolId, ScopeId}; +use crate::semantic_index::{ public_symbol, root_scope, semantic_index, symbol_table, NodeWithScopeId, }; -use crate::red_knot::types::infer::{TypeInference, TypeInferenceBuilder}; -use crate::red_knot::FxIndexSet; +use crate::types::infer::{TypeInference, TypeInferenceBuilder}; use crate::Db; +use crate::FxIndexSet; mod display; mod infer; @@ -500,10 +500,8 @@ mod tests { use crate::db::tests::TestDb; use crate::module::resolver::{set_module_resolution_settings, ModuleResolutionSettings}; - use crate::red_knot::semantic_index::root_scope; - use crate::red_knot::types::{ - expression_ty, infer_types, public_symbol_ty_by_name, TypingContext, - }; + use crate::semantic_index::root_scope; + use crate::types::{expression_ty, infer_types, public_symbol_ty_by_name, TypingContext}; fn setup_db() -> TestDb { let mut db = TestDb::new(); diff --git a/crates/ruff_python_semantic/src/red_knot/types/display.rs b/crates/red_knot_python_semantic/src/types/display.rs similarity index 98% rename from crates/ruff_python_semantic/src/red_knot/types/display.rs rename to crates/red_knot_python_semantic/src/types/display.rs index 804fbc2d6000f..3c8d3908c9acd 100644 --- a/crates/ruff_python_semantic/src/red_knot/types/display.rs +++ b/crates/red_knot_python_semantic/src/types/display.rs @@ -2,7 +2,7 @@ use std::fmt::{Display, Formatter}; -use crate::red_knot::types::{IntersectionType, Type, TypingContext, UnionType}; +use crate::types::{IntersectionType, Type, TypingContext, UnionType}; impl Type { pub fn display<'a>(&'a self, context: &'a TypingContext) -> DisplayType<'a> { diff --git a/crates/ruff_python_semantic/src/red_knot/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs similarity index 98% rename from crates/ruff_python_semantic/src/red_knot/types/infer.rs rename to crates/red_knot_python_semantic/src/types/infer.rs index 0cf41f34e9af1..7beef6ee93f24 100644 --- a/crates/ruff_python_semantic/src/red_knot/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -10,15 +10,11 @@ use ruff_python_ast::{ExprContext, TypeParams}; use crate::module::resolver::resolve_module; use crate::module::ModuleName; use crate::name::Name; -use crate::red_knot::semantic_index::ast_ids::{ScopeAstIdNode, ScopeExpressionId}; -use crate::red_knot::semantic_index::definition::{ - Definition, ImportDefinition, ImportFromDefinition, -}; -use crate::red_knot::semantic_index::symbol::{ - FileScopeId, ScopeId, ScopeKind, ScopeSymbolId, SymbolTable, -}; -use crate::red_knot::semantic_index::{symbol_table, ChildrenIter, SemanticIndex}; -use crate::red_knot::types::{ +use crate::semantic_index::ast_ids::{ScopeAstIdNode, ScopeExpressionId}; +use crate::semantic_index::definition::{Definition, ImportDefinition, ImportFromDefinition}; +use crate::semantic_index::symbol::{FileScopeId, ScopeId, ScopeKind, ScopeSymbolId, SymbolTable}; +use crate::semantic_index::{symbol_table, ChildrenIter, SemanticIndex}; +use crate::types::{ ClassType, FunctionType, IntersectionType, LocalClassTypeId, LocalFunctionTypeId, LocalIntersectionTypeId, LocalUnionTypeId, ModuleType, Type, TypeId, TypingContext, UnionType, UnionTypeBuilder, @@ -719,7 +715,7 @@ mod tests { use crate::db::tests::TestDb; use crate::module::resolver::{set_module_resolution_settings, ModuleResolutionSettings}; use crate::name::Name; - use crate::red_knot::types::{public_symbol_ty_by_name, Type, TypingContext}; + use crate::types::{public_symbol_ty_by_name, Type, TypingContext}; fn setup_db() -> TestDb { let mut db = TestDb::new(); diff --git a/crates/ruff_python_semantic/Cargo.toml b/crates/ruff_python_semantic/Cargo.toml index c823bc29fbe1d..119d1ffc5f53f 100644 --- a/crates/ruff_python_semantic/Cargo.toml +++ b/crates/ruff_python_semantic/Cargo.toml @@ -11,7 +11,6 @@ repository = { workspace = true } license = { workspace = true } [dependencies] -ruff_db = { workspace = true } ruff_index = { workspace = true } ruff_python_ast = { workspace = true } ruff_python_stdlib = { workspace = true } @@ -20,13 +19,8 @@ ruff_text_size = { workspace = true } bitflags = { workspace = true } is-macro = { workspace = true } -indexmap = { workspace = true, optional = true } -salsa = { workspace = true, optional = true } -smallvec = { workspace = true, optional = true } smol_str = { workspace = true } -tracing = { workspace = true, optional = true } rustc-hash = { workspace = true } -hashbrown = { workspace = true, optional = true } [dev-dependencies] anyhow = { workspace = true } @@ -36,5 +30,3 @@ tempfile = { workspace = true } [lints] workspace = true -[features] -red_knot = ["dep:salsa", "dep:tracing", "dep:hashbrown", "dep:smallvec", "dep:indexmap"] diff --git a/crates/ruff_python_semantic/src/lib.rs b/crates/ruff_python_semantic/src/lib.rs index 4f30103e392cb..ce45050239e47 100644 --- a/crates/ruff_python_semantic/src/lib.rs +++ b/crates/ruff_python_semantic/src/lib.rs @@ -2,17 +2,10 @@ pub mod analyze; mod binding; mod branches; mod context; -#[cfg(feature = "red_knot")] -mod db; mod definition; mod globals; mod model; -#[cfg(feature = "red_knot")] -pub mod module; -pub mod name; mod nodes; -#[cfg(feature = "red_knot")] -pub mod red_knot; mod reference; mod scope; mod star_import; @@ -27,6 +20,3 @@ pub use nodes::*; pub use reference::*; pub use scope::*; pub use star_import::*; - -#[cfg(feature = "red_knot")] -pub use db::{Db, Jar};