Skip to content

Commit 2dfbf11

Browse files
authored
[red-knot] Extract red_knot_python_semantic crate (#11926)
1 parent ed948ea commit 2dfbf11

File tree

23 files changed

+125
-94
lines changed

23 files changed

+125
-94
lines changed

Cargo.lock

+23-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ ruff_source_file = { path = "crates/ruff_source_file" }
3535
ruff_text_size = { path = "crates/ruff_text_size" }
3636
ruff_workspace = { path = "crates/ruff_workspace" }
3737

38+
red_knot_python_semantic = { path = "crates/red_knot_python_semantic" }
39+
3840
aho-corasick = { version = "1.1.3" }
3941
annotate-snippets = { version = "0.9.2", features = ["color"] }
4042
anyhow = { version = "1.0.80" }

crates/red_knot/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ license.workspace = true
1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313

1414
[dependencies]
15+
red_knot_python_semantic = { workspace = true }
16+
1517
ruff_python_parser = { workspace = true }
1618
ruff_python_ast = { workspace = true }
1719
ruff_python_stdlib = { workspace = true }

crates/red_knot/src/module.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::sync::Arc;
77
use dashmap::mapref::entry::Entry;
88
use smol_str::SmolStr;
99

10+
use red_knot_python_semantic::module::ModuleKind;
11+
1012
use crate::db::{QueryResult, SemanticDb, SemanticJar};
1113
use crate::files::FileId;
1214
use crate::semantic::Dependency;
@@ -177,15 +179,6 @@ impl std::fmt::Display for ModuleName {
177179
}
178180
}
179181

180-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
181-
pub enum ModuleKind {
182-
/// A single-file module (e.g. `foo.py` or `foo.pyi`)
183-
Module,
184-
185-
/// A python package (`foo/__init__.py` or `foo/__init__.pyi`)
186-
Package,
187-
}
188-
189182
/// A search path in which to search modules.
190183
/// Corresponds to a path in [`sys.path`](https://docs.python.org/3/library/sys_path_init.html) at runtime.
191184
///
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[package]
2+
name = "red_knot_python_semantic"
3+
version = "0.0.0"
4+
publish = false
5+
authors = { workspace = true }
6+
edition = { workspace = true }
7+
rust-version = { workspace = true }
8+
homepage = { workspace = true }
9+
documentation = { workspace = true }
10+
repository = { workspace = true }
11+
license = { workspace = true }
12+
13+
[dependencies]
14+
ruff_db = { workspace = true }
15+
ruff_index = { workspace = true }
16+
ruff_python_ast = { workspace = true }
17+
ruff_python_stdlib = { workspace = true }
18+
ruff_text_size = { workspace = true }
19+
20+
bitflags = { workspace = true }
21+
indexmap = { workspace = true }
22+
salsa = { workspace = true }
23+
smallvec = { workspace = true }
24+
smol_str = { workspace = true }
25+
tracing = { workspace = true }
26+
rustc-hash = { workspace = true }
27+
hashbrown = { workspace = true }
28+
29+
[dev-dependencies]
30+
anyhow = { workspace = true }
31+
ruff_python_parser = { workspace = true }
32+
tempfile = { workspace = true }
33+
34+
[lints]
35+
workspace = true
36+

crates/ruff_python_semantic/src/red_knot/ast_node_ref.rs renamed to crates/red_knot_python_semantic/src/ast_node_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ unsafe impl<T> Sync for AstNodeRef<T> where T: Sync {}
9393

9494
#[cfg(test)]
9595
mod tests {
96-
use crate::red_knot::ast_node_ref::AstNodeRef;
96+
use crate::ast_node_ref::AstNodeRef;
9797
use ruff_db::parsed::ParsedModule;
9898
use ruff_python_ast::PySourceType;
9999
use ruff_python_parser::parse_unchecked_source;

crates/ruff_python_semantic/src/db.rs renamed to crates/red_knot_python_semantic/src/db.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ use crate::module::resolver::{
66
file_to_module, internal::ModuleNameIngredient, internal::ModuleResolverSearchPaths,
77
resolve_module_query,
88
};
9-
use crate::red_knot::semantic_index::symbol::{
10-
public_symbols_map, scopes_map, PublicSymbolId, ScopeId,
11-
};
12-
use crate::red_knot::semantic_index::{root_scope, semantic_index, symbol_table};
13-
use crate::red_knot::types::{infer_types, public_symbol_ty};
9+
10+
use crate::semantic_index::symbol::{public_symbols_map, scopes_map, PublicSymbolId, ScopeId};
11+
use crate::semantic_index::{root_scope, semantic_index, symbol_table};
12+
use crate::types::{infer_types, public_symbol_ty};
1413

1514
#[salsa::jar(db=Db)]
1615
pub struct Jar(
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pub mod ast_node_ref;
2+
mod db;
3+
pub mod module;
4+
pub mod name;
5+
mod node_key;
6+
pub mod semantic_index;
7+
pub mod types;
8+
9+
type FxIndexSet<V> = indexmap::set::IndexSet<V, BuildHasherDefault<FxHasher>>;
10+
11+
pub use db::{Db, Jar};
12+
use rustc_hash::FxHasher;
13+
use std::hash::BuildHasherDefault;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use rustc_hash::FxHasher;
21
use std::hash::BuildHasherDefault;
32

3+
use rustc_hash::FxHasher;
4+
45
pub mod ast_node_ref;
56
mod node_key;
67
pub mod semantic_index;
78
pub mod types;
9+
810
pub(crate) type FxIndexSet<V> = indexmap::set::IndexSet<V, BuildHasherDefault<FxHasher>>;

crates/ruff_python_semantic/src/module.rs renamed to crates/red_knot_python_semantic/src/module.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl ModuleName {
4646
/// ## Examples
4747
///
4848
/// ```
49-
/// use ruff_python_semantic::module::ModuleName;
49+
/// use red_knot_python_semantic::module::ModuleName;
5050
///
5151
/// assert_eq!(ModuleName::new_static("foo.bar").as_deref(), Some("foo.bar"));
5252
/// assert_eq!(ModuleName::new_static(""), None);
@@ -78,7 +78,7 @@ impl ModuleName {
7878
/// # Examples
7979
///
8080
/// ```
81-
/// use ruff_python_semantic::module::ModuleName;
81+
/// use red_knot_python_semantic::module::ModuleName;
8282
///
8383
/// assert_eq!(ModuleName::new_static("foo.bar.baz").unwrap().components().collect::<Vec<_>>(), vec!["foo", "bar", "baz"]);
8484
/// ```
@@ -91,7 +91,7 @@ impl ModuleName {
9191
/// # Examples
9292
///
9393
/// ```
94-
/// use ruff_python_semantic::module::ModuleName;
94+
/// use red_knot_python_semantic::module::ModuleName;
9595
///
9696
/// assert_eq!(ModuleName::new_static("foo.bar").unwrap().parent(), Some(ModuleName::new_static("foo").unwrap()));
9797
/// assert_eq!(ModuleName::new_static("foo.bar.baz").unwrap().parent(), Some(ModuleName::new_static("foo.bar").unwrap()));
@@ -110,7 +110,7 @@ impl ModuleName {
110110
/// # Examples
111111
///
112112
/// ```
113-
/// use ruff_python_semantic::module::ModuleName;
113+
/// use red_knot_python_semantic::module::ModuleName;
114114
///
115115
/// assert!(ModuleName::new_static("foo.bar").unwrap().starts_with(&ModuleName::new_static("foo").unwrap()));
116116
///
@@ -312,7 +312,7 @@ struct ModuleSearchPathInner {
312312
/// for the standard library are moved higher up to match Python's semantics at runtime.
313313
///
314314
/// [the order given in the typing spec]: https://typing.readthedocs.io/en/latest/spec/distributing.html#import-resolution-ordering
315-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, is_macro::Is)]
315+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
316316
pub enum ModuleSearchPathKind {
317317
/// "Extra" paths provided by the user in a config file, env var or CLI flag.
318318
/// E.g. mypy's `MYPYPATH` env var, or pyright's `stubPath` configuration setting

crates/ruff_python_semantic/src/red_knot/semantic_index.rs renamed to crates/red_knot_python_semantic/src/semantic_index.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use ruff_db::vfs::VfsFile;
88
use ruff_index::{IndexSlice, IndexVec};
99
use ruff_python_ast as ast;
1010

11-
use crate::red_knot::node_key::NodeKey;
12-
use crate::red_knot::semantic_index::ast_ids::{AstId, AstIds, ScopeClassId, ScopeFunctionId};
13-
use crate::red_knot::semantic_index::builder::SemanticIndexBuilder;
14-
use crate::red_knot::semantic_index::symbol::{
11+
use crate::node_key::NodeKey;
12+
use crate::semantic_index::ast_ids::{AstId, AstIds, ScopeClassId, ScopeFunctionId};
13+
use crate::semantic_index::builder::SemanticIndexBuilder;
14+
use crate::semantic_index::symbol::{
1515
FileScopeId, PublicSymbolId, Scope, ScopeId, ScopeKind, ScopedSymbolId, SymbolTable,
1616
};
1717
use crate::Db;
@@ -272,8 +272,8 @@ mod tests {
272272
use ruff_db::vfs::{system_path_to_file, VfsFile};
273273

274274
use crate::db::tests::TestDb;
275-
use crate::red_knot::semantic_index::symbol::{FileScopeId, ScopeKind, SymbolTable};
276-
use crate::red_knot::semantic_index::{root_scope, semantic_index, symbol_table};
275+
use crate::semantic_index::symbol::{FileScopeId, ScopeKind, SymbolTable};
276+
use crate::semantic_index::{root_scope, semantic_index, symbol_table};
277277

278278
struct TestCase {
279279
db: TestDb,

crates/ruff_python_semantic/src/red_knot/semantic_index/ast_ids.rs renamed to crates/red_knot_python_semantic/src/semantic_index/ast_ids.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use ruff_index::{newtype_index, IndexVec};
66
use ruff_python_ast as ast;
77
use ruff_python_ast::AnyNodeRef;
88

9-
use crate::red_knot::ast_node_ref::AstNodeRef;
10-
use crate::red_knot::node_key::NodeKey;
11-
use crate::red_knot::semantic_index::semantic_index;
12-
use crate::red_knot::semantic_index::symbol::{FileScopeId, ScopeId};
9+
use crate::ast_node_ref::AstNodeRef;
10+
use crate::node_key::NodeKey;
11+
use crate::semantic_index::semantic_index;
12+
use crate::semantic_index::symbol::{FileScopeId, ScopeId};
1313
use crate::Db;
1414

1515
/// AST ids for a single scope.
@@ -98,7 +98,6 @@ pub trait AstIdNode {
9898
/// ## Panics
9999
/// May panic if the node does not belongs to `file`'s AST or is outside of `scope`. It may also
100100
/// return an incorrect node if that's the case.
101-
102101
fn ast_id(&self, db: &dyn Db, file: VfsFile, scope: FileScopeId) -> AstId<Self::ScopeId>;
103102

104103
/// Resolves the AST node for `id`.

crates/ruff_python_semantic/src/red_knot/semantic_index/builder.rs renamed to crates/red_knot_python_semantic/src/semantic_index/builder.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ use ruff_python_ast as ast;
88
use ruff_python_ast::visitor::{walk_expr, walk_stmt, Visitor};
99

1010
use crate::name::Name;
11-
use crate::red_knot::node_key::NodeKey;
12-
use crate::red_knot::semantic_index::ast_ids::{
11+
use crate::node_key::NodeKey;
12+
use crate::semantic_index::ast_ids::{
1313
AstId, AstIdsBuilder, ScopeAssignmentId, ScopeClassId, ScopeFunctionId, ScopeImportFromId,
1414
ScopeImportId, ScopeNamedExprId,
1515
};
16-
use crate::red_knot::semantic_index::definition::{
17-
Definition, ImportDefinition, ImportFromDefinition,
18-
};
19-
use crate::red_knot::semantic_index::symbol::{
16+
use crate::semantic_index::definition::{Definition, ImportDefinition, ImportFromDefinition};
17+
use crate::semantic_index::symbol::{
2018
FileScopeId, FileSymbolId, Scope, ScopedSymbolId, SymbolFlags, SymbolTableBuilder,
2119
};
22-
use crate::red_knot::semantic_index::{NodeWithScopeId, SemanticIndex};
20+
use crate::semantic_index::{NodeWithScopeId, SemanticIndex};
2321

2422
pub(super) struct SemanticIndexBuilder<'a> {
2523
// Builder state

crates/ruff_python_semantic/src/red_knot/semantic_index/definition.rs renamed to crates/red_knot_python_semantic/src/semantic_index/definition.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::red_knot::semantic_index::ast_ids::{
1+
use crate::semantic_index::ast_ids::{
22
ScopeAnnotatedAssignmentId, ScopeAssignmentId, ScopeClassId, ScopeFunctionId,
33
ScopeImportFromId, ScopeImportId, ScopeNamedExprId,
44
};

crates/ruff_python_semantic/src/red_knot/semantic_index/symbol.rs renamed to crates/red_knot_python_semantic/src/semantic_index/symbol.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use ruff_db::vfs::VfsFile;
1414
use ruff_index::{newtype_index, IndexVec};
1515

1616
use crate::name::Name;
17-
use crate::red_knot::semantic_index::definition::Definition;
18-
use crate::red_knot::semantic_index::{root_scope, semantic_index, symbol_table, SymbolMap};
17+
use crate::semantic_index::definition::Definition;
18+
use crate::semantic_index::{root_scope, semantic_index, symbol_table, SymbolMap};
1919
use crate::Db;
2020

2121
#[derive(Eq, PartialEq, Debug)]

crates/ruff_python_semantic/src/red_knot/types.rs renamed to crates/red_knot_python_semantic/src/types.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use ruff_index::newtype_index;
66
use ruff_python_ast as ast;
77

88
use crate::name::Name;
9-
use crate::red_knot::semantic_index::ast_ids::{AstIdNode, ScopeAstIdNode};
10-
use crate::red_knot::semantic_index::symbol::{FileScopeId, PublicSymbolId, ScopeId};
11-
use crate::red_knot::semantic_index::{
9+
use crate::semantic_index::ast_ids::{AstIdNode, ScopeAstIdNode};
10+
use crate::semantic_index::symbol::{FileScopeId, PublicSymbolId, ScopeId};
11+
use crate::semantic_index::{
1212
public_symbol, root_scope, semantic_index, symbol_table, NodeWithScopeId,
1313
};
14-
use crate::red_knot::types::infer::{TypeInference, TypeInferenceBuilder};
15-
use crate::red_knot::FxIndexSet;
14+
use crate::types::infer::{TypeInference, TypeInferenceBuilder};
1615
use crate::Db;
16+
use crate::FxIndexSet;
1717

1818
mod display;
1919
mod infer;
@@ -62,7 +62,7 @@ pub(crate) fn expression_ty(db: &dyn Db, file: VfsFile, expression: &ast::Expr)
6262
/// This being a query ensures that the invalidation short-circuits if the type of this symbol didn't change.
6363
#[salsa::tracked]
6464
pub(crate) fn public_symbol_ty(db: &dyn Db, symbol: PublicSymbolId) -> Type {
65-
let _ = tracing::debug_span!("public_symbol_ty", "{:?}", symbol.debug(db));
65+
let _ = tracing::debug_span!("public_symbol_ty", symbol = ?symbol.debug(db)).enter();
6666

6767
let file = symbol.file(db);
6868
let scope = root_scope(db, file);
@@ -71,7 +71,7 @@ pub(crate) fn public_symbol_ty(db: &dyn Db, symbol: PublicSymbolId) -> Type {
7171
inference.symbol_ty(symbol.scoped_symbol_id(db))
7272
}
7373

74-
/// Shorthand for [`public_symbol_ty()`] that takes a symbol name instead of a [`PublicSymbolId`].
74+
/// Shorthand for `public_symbol_ty` that takes a symbol name instead of a [`PublicSymbolId`].
7575
pub fn public_symbol_ty_by_name(db: &dyn Db, file: VfsFile, name: &str) -> Option<Type> {
7676
let symbol = public_symbol(db, file, name)?;
7777
Some(public_symbol_ty(db, symbol))
@@ -500,10 +500,8 @@ mod tests {
500500
assert_will_not_run_function_query, assert_will_run_function_query, TestDb,
501501
};
502502
use crate::module::resolver::{set_module_resolution_settings, ModuleResolutionSettings};
503-
use crate::red_knot::semantic_index::root_scope;
504-
use crate::red_knot::types::{
505-
expression_ty, infer_types, public_symbol_ty_by_name, TypingContext,
506-
};
503+
use crate::semantic_index::root_scope;
504+
use crate::types::{expression_ty, infer_types, public_symbol_ty_by_name, TypingContext};
507505

508506
fn setup_db() -> TestDb {
509507
let mut db = TestDb::new();

crates/ruff_python_semantic/src/red_knot/types/display.rs renamed to crates/red_knot_python_semantic/src/types/display.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fmt::{Display, Formatter};
44

5-
use crate::red_knot::types::{IntersectionType, Type, TypingContext, UnionType};
5+
use crate::types::{IntersectionType, Type, TypingContext, UnionType};
66

77
impl Type {
88
pub fn display<'a>(&'a self, context: &'a TypingContext) -> DisplayType<'a> {

0 commit comments

Comments
 (0)