Skip to content

Commit

Permalink
[red-knot] Extract red_knot_python_semantic crate
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 20, 2024
1 parent 22733cb commit 9fbb4f7
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 72 deletions.
30 changes: 24 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 3 additions & 0 deletions crates/red_knot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
36 changes: 36 additions & 0 deletions crates/red_knot_python_semantic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ unsafe impl<T> Sync for AstNodeRef<T> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use crate::module::resolver::{
file_to_module, internal::ModuleNameIngredient, internal::ModuleResolverSearchPaths,
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(
Expand Down
13 changes: 13 additions & 0 deletions crates/red_knot_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<V> = indexmap::set::IndexSet<V, BuildHasherDefault<FxHasher>>;

pub use db::{Db, Jar};
use rustc_hash::FxHasher;
use std::hash::BuildHasherDefault;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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, ScopedSymbolId, SymbolTable,
};
use crate::Db;
Expand Down Expand Up @@ -272,8 +272,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Self::ScopeId>;

/// Resolves the AST node for `id`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, ScopedSymbolId, SymbolFlags, SymbolTableBuilder,
};
use crate::red_knot::semantic_index::{NodeWithScopeId, SemanticIndex};
use crate::semantic_index::{NodeWithScopeId, SemanticIndex};

pub(super) struct SemanticIndexBuilder<'a> {
// Builder state
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,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;
Expand Down Expand Up @@ -500,10 +500,8 @@ mod tests {
assert_will_not_run_function_query, assert_will_run_function_query, 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, ScopedSymbolId, 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, ScopedSymbolId, SymbolTable};
use crate::semantic_index::{symbol_table, ChildrenIter, SemanticIndex};
use crate::types::{
ClassType, FunctionType, IntersectionType, ModuleType, ScopedClassTypeId, ScopedFunctionTypeId,
ScopedIntersectionTypeId, ScopedUnionTypeId, Type, TypeId, TypingContext, UnionType,
UnionTypeBuilder,
Expand Down Expand Up @@ -700,7 +696,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();
Expand Down
8 changes: 0 additions & 8 deletions crates/ruff_python_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand All @@ -36,5 +30,3 @@ tempfile = { workspace = true }
[lints]
workspace = true

[features]
red_knot = ["dep:salsa", "dep:tracing", "dep:hashbrown", "dep:smallvec", "dep:indexmap"]
10 changes: 0 additions & 10 deletions crates/ruff_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};

0 comments on commit 9fbb4f7

Please sign in to comment.