Skip to content

Commit

Permalink
use coarse-grained tracked structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Feb 8, 2025
1 parent 3a806ec commit 21bea37
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 40 deletions.
14 changes: 11 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ rayon = { version = "1.10.0" }
regex = { version = "1.10.2" }
rustc-hash = { version = "2.0.0" }
# When updating salsa, make sure to also update the revision in `fuzz/Cargo.toml`
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "88a1d7774d78f048fbd77d40abca9ebd729fd1f0" }
salsa = { git = "https://github.com/ibraheemdev/salsa.git", rev = "22f742a53d9eafca80fe5a0a6b188b56243c741c" }
schemars = { version = "0.8.16" }
seahash = { version = "4.1.0" }
serde = { version = "1.0.197", features = ["derive"] }
Expand Down
20 changes: 7 additions & 13 deletions crates/red_knot_python_semantic/src/ast_node_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ruff_db::parsed::ParsedModule;
/// Holding on to any [`AstNodeRef`] prevents the [`ParsedModule`] from being released.
///
/// ## Equality
/// Two `AstNodeRef` are considered equal if their wrapped nodes are equal.
/// Two `AstNodeRef` are considered equal if their pointer addresses are equal.
#[derive(Clone)]
pub struct AstNodeRef<T> {
/// Owned reference to the node's [`ParsedModule`].
Expand Down Expand Up @@ -67,23 +67,17 @@ where
}
}

impl<T> PartialEq for AstNodeRef<T>
where
T: PartialEq,
{
impl<T> PartialEq for AstNodeRef<T> {
fn eq(&self, other: &Self) -> bool {
self.node().eq(other.node())
self.node.eq(&other.node)
}
}

impl<T> Eq for AstNodeRef<T> where T: Eq {}
impl<T> Eq for AstNodeRef<T> {}

impl<T> Hash for AstNodeRef<T>
where
T: Hash,
{
impl<T> Hash for AstNodeRef<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.node().hash(state);
self.node.hash(state);
}
}

Expand Down Expand Up @@ -117,7 +111,7 @@ mod tests {
let stmt_cloned = &cloned.syntax().body[0];
let cloned_node = unsafe { AstNodeRef::new(cloned.clone(), stmt_cloned) };

assert_eq!(node1, cloned_node);
assert_ne!(node1, cloned_node);

let other_raw = parse_unchecked_source("2 + 2", PySourceType::Python);
let other = ParsedModule::new(other_raw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub(crate) fn search_paths(db: &dyn Db) -> SearchPathIterator {
}

#[derive(Debug, PartialEq, Eq)]
pub(crate) struct SearchPaths {
pub struct SearchPaths {
/// Search paths that have been statically determined purely from reading Ruff's configuration settings.
/// These shouldn't ever change unless the config settings themselves change.
static_paths: Vec<SearchPath>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ pub(crate) enum PatternConstraintKind<'db> {

#[salsa::tracked]
pub(crate) struct PatternConstraint<'db> {
#[id]
pub(crate) file: File,

#[id]
pub(crate) file_scope: FileScopeId,

#[no_eq]
Expand Down
21 changes: 9 additions & 12 deletions crates/red_knot_python_semantic/src/semantic_index/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ use crate::Db;
#[salsa::tracked]
pub struct Definition<'db> {
/// The file in which the definition occurs.
#[id]
pub(crate) file: File,

/// The scope in which the definition occurs.
#[id]
pub(crate) file_scope: FileScopeId,

/// The symbol defined.
#[id]
pub(crate) symbol: ScopedSymbolId,

#[no_eq]
Expand Down Expand Up @@ -435,7 +432,7 @@ impl DefinitionCategory {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash)]
pub enum DefinitionKind<'db> {
Import(AstNodeRef<ast::Alias>),
ImportFrom(ImportFromDefinitionKind),
Expand Down Expand Up @@ -540,7 +537,7 @@ impl DefinitionKind<'_> {
}
}

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Hash)]
pub(crate) enum TargetKind<'db> {
Sequence(Unpack<'db>),
Name,
Expand All @@ -555,7 +552,7 @@ impl<'db> From<Option<Unpack<'db>>> for TargetKind<'db> {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash)]
#[allow(dead_code)]
pub struct MatchPatternDefinitionKind {
pattern: AstNodeRef<ast::Pattern>,
Expand All @@ -573,7 +570,7 @@ impl MatchPatternDefinitionKind {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash)]
pub struct ComprehensionDefinitionKind {
iterable: AstNodeRef<ast::Expr>,
target: AstNodeRef<ast::ExprName>,
Expand All @@ -599,7 +596,7 @@ impl ComprehensionDefinitionKind {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash)]
pub struct ImportFromDefinitionKind {
node: AstNodeRef<ast::StmtImportFrom>,
alias_index: usize,
Expand All @@ -615,7 +612,7 @@ impl ImportFromDefinitionKind {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash)]
pub struct AssignmentDefinitionKind<'db> {
target: TargetKind<'db>,
value: AstNodeRef<ast::Expr>,
Expand All @@ -641,7 +638,7 @@ impl<'db> AssignmentDefinitionKind<'db> {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash)]
pub struct WithItemDefinitionKind {
node: AstNodeRef<ast::WithItem>,
target: AstNodeRef<ast::ExprName>,
Expand All @@ -662,7 +659,7 @@ impl WithItemDefinitionKind {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash)]
pub struct ForStmtDefinitionKind<'db> {
target: TargetKind<'db>,
iterable: AstNodeRef<ast::Expr>,
Expand Down Expand Up @@ -693,7 +690,7 @@ impl<'db> ForStmtDefinitionKind<'db> {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash)]
pub struct ExceptHandlerDefinitionKind {
handler: AstNodeRef<ast::ExceptHandlerExceptHandler>,
is_star: bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ pub(crate) enum ExpressionKind {
#[salsa::tracked]
pub(crate) struct Expression<'db> {
/// The file in which the expression occurs.
#[id]
pub(crate) file: File,

/// The scope in which the expression occurs.
#[id]
pub(crate) file_scope: FileScopeId,

/// The expression node.
Expand Down
2 changes: 0 additions & 2 deletions crates/red_knot_python_semantic/src/semantic_index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ pub struct ScopedSymbolId;
/// A cross-module identifier of a scope that can be used as a salsa query parameter.
#[salsa::tracked]
pub struct ScopeId<'db> {
#[id]
pub file: File,

#[id]
pub file_scope_id: FileScopeId,

#[no_eq]
Expand Down
4 changes: 1 addition & 3 deletions crates/red_knot_python_semantic/src/unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ use crate::Db;
/// * an argument of a cross-module query
#[salsa::tracked]
pub(crate) struct Unpack<'db> {
#[id]
pub(crate) file: File,

#[id]
pub(crate) file_scope: FileScopeId,

/// The target expression that is being unpacked. For example, in `(a, b) = (1, 2)`, the target
Expand Down Expand Up @@ -62,7 +60,7 @@ impl<'db> Unpack<'db> {
}

/// The expression that is being unpacked.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Hash)]
pub(crate) enum UnpackValue<'db> {
/// An iterable expression like the one in a `for` loop or a comprehension.
Iterable(Expression<'db>),
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ruff_python_formatter = { path = "../crates/ruff_python_formatter" }
ruff_text_size = { path = "../crates/ruff_text_size" }

libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer", default-features = false }
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "88a1d7774d78f048fbd77d40abca9ebd729fd1f0" }
salsa = { git = "https://github.com/ibraheemdev/salsa.git", rev = "22f742a53d9eafca80fe5a0a6b188b56243c741c" }
similar = { version = "2.5.0" }
tracing = { version = "0.1.40" }

Expand Down

0 comments on commit 21bea37

Please sign in to comment.