Skip to content

Commit ca7d2da

Browse files
committed
Add various Update implementations
1 parent 21bea37 commit ca7d2da

File tree

22 files changed

+92
-168
lines changed

22 files changed

+92
-168
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/red_knot_python_semantic/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ license = { workspace = true }
1212

1313
[dependencies]
1414
ruff_db = { workspace = true }
15-
ruff_index = { workspace = true }
15+
ruff_index = { workspace = true, features = ["salsa"] }
1616
ruff_macros = { workspace = true }
17-
ruff_python_ast = { workspace = true }
17+
ruff_python_ast = { workspace = true, features = ["salsa"] }
1818
ruff_python_parser = { workspace = true }
1919
ruff_python_stdlib = { workspace = true }
2020
ruff_source_file = { workspace = true }
@@ -31,7 +31,7 @@ drop_bomb = { workspace = true }
3131
indexmap = { workspace = true }
3232
itertools = { workspace = true }
3333
ordermap = { workspace = true }
34-
salsa = { workspace = true }
34+
salsa = { workspace = true, features = ["compact_str"] }
3535
thiserror = { workspace = true }
3636
tracing = { workspace = true }
3737
rustc-hash = { workspace = true }

crates/red_knot_python_semantic/src/ast_node_ref.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ unsafe impl<T> Send for AstNodeRef<T> where T: Send {}
8686
#[allow(unsafe_code)]
8787
unsafe impl<T> Sync for AstNodeRef<T> where T: Sync {}
8888

89+
#[allow(unsafe_code)]
90+
unsafe impl<T> salsa::Update for AstNodeRef<T> {
91+
unsafe fn maybe_update(old_pointer: *mut Self, new_value: Self) -> bool {
92+
let old_node: &mut AstNodeRef<T> = unsafe { &mut *old_pointer };
93+
94+
if old_node._parsed == new_value._parsed {
95+
false
96+
} else {
97+
*old_node = new_value;
98+
true
99+
}
100+
}
101+
}
102+
89103
#[cfg(test)]
90104
mod tests {
91105
use crate::ast_node_ref::AstNodeRef;

crates/red_knot_python_semantic/src/semantic_index.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use std::iter::FusedIterator;
22
use std::sync::Arc;
33

4-
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
5-
use salsa::plumbing::AsId;
6-
74
use ruff_db::files::File;
85
use ruff_db::parsed::parsed_module;
96
use ruff_index::{IndexSlice, IndexVec};
107

8+
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
9+
use salsa::plumbing::AsId;
10+
use salsa::Update;
11+
1112
use crate::module_name::ModuleName;
1213
use crate::semantic_index::ast_ids::node_key::ExpressionNodeKey;
1314
use crate::semantic_index::ast_ids::AstIds;
@@ -123,7 +124,7 @@ pub(crate) fn global_scope(db: &dyn Db, file: File) -> ScopeId<'_> {
123124
}
124125

125126
/// The symbol tables and use-def maps for all scopes in a file.
126-
#[derive(Debug)]
127+
#[derive(Debug, Update)]
127128
pub(crate) struct SemanticIndex<'db> {
128129
/// List of all symbol tables in this file, indexed by scope.
129130
symbol_tables: IndexVec<FileScopeId, Arc<SymbolTable>>,

crates/red_knot_python_semantic/src/semantic_index/ast_ids.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ fn ast_ids<'db>(db: &'db dyn Db, scope: ScopeId) -> &'db AstIds {
4949
semantic_index(db, scope.file(db)).ast_ids(scope.file_scope_id(db))
5050
}
5151

52+
// Always consider AstIds as changed.
53+
#[allow(unsafe_code)]
54+
unsafe impl salsa::Update for AstIds {
55+
unsafe fn maybe_update(old_pointer: *mut Self, new_value: Self) -> bool {
56+
*old_pointer = new_value;
57+
true
58+
}
59+
}
60+
5261
/// Uniquely identifies a use of a name in a [`crate::semantic_index::symbol::FileScopeId`].
5362
#[newtype_index]
5463
pub struct ScopedUseId;

crates/red_knot_python_semantic/src/semantic_index/attribute_assignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hash::FxHashMap;
99

1010
/// Describes an (annotated) attribute assignment that we discovered in a method
1111
/// body, typically of the form `self.x: int`, `self.x: int = …` or `self.x = …`.
12-
#[derive(Debug, Clone, PartialEq, Eq)]
12+
#[derive(Debug, Clone, PartialEq, Eq, salsa::Update)]
1313
pub(crate) enum AttributeAssignment<'db> {
1414
/// An attribute assignment with an explicit type annotation, either
1515
/// `self.x: <annotation>` or `self.x: <annotation> = …`.

crates/red_knot_python_semantic/src/semantic_index/constraint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use crate::db::Db;
55
use crate::semantic_index::expression::Expression;
66
use crate::semantic_index::symbol::{FileScopeId, ScopeId};
77

8-
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
8+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, salsa::Update)]
99
pub(crate) struct Constraint<'db> {
1010
pub(crate) node: ConstraintNode<'db>,
1111
pub(crate) is_positive: bool,
1212
}
1313

14-
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
14+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, salsa::Update)]
1515
pub(crate) enum ConstraintNode<'db> {
1616
Expression(Expression<'db>),
1717
Pattern(PatternConstraint<'db>),

crates/red_knot_python_semantic/src/semantic_index/symbol.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl FileScopeId {
175175
}
176176
}
177177

178-
#[derive(Debug)]
178+
#[derive(Debug, salsa::Update)]
179179
pub struct Scope {
180180
pub(super) parent: Option<FileScopeId>,
181181
pub(super) node: NodeWithScopeKind,
@@ -214,7 +214,7 @@ impl ScopeKind {
214214
}
215215

216216
/// Symbol table for a specific [`Scope`].
217-
#[derive(Debug, Default)]
217+
#[derive(Debug, Default, salsa::Update)]
218218
pub struct SymbolTable {
219219
/// The symbols in this scope.
220220
symbols: IndexVec<ScopedSymbolId, Symbol>,
@@ -422,7 +422,7 @@ impl NodeWithScopeRef<'_> {
422422
}
423423

424424
/// Node that introduces a new scope.
425-
#[derive(Clone, Debug)]
425+
#[derive(Clone, Debug, salsa::Update)]
426426
pub enum NodeWithScopeKind {
427427
Module,
428428
Class(AstNodeRef<ast::StmtClassDef>),

crates/red_knot_python_semantic/src/semantic_index/use_def.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ mod symbol_state;
278278
type AllConstraints<'db> = IndexVec<ScopedConstraintId, Constraint<'db>>;
279279

280280
/// Applicable definitions and constraints for every use of a name.
281-
#[derive(Debug, PartialEq, Eq)]
281+
#[derive(Debug, PartialEq, Eq, salsa::Update)]
282282
pub(crate) struct UseDefMap<'db> {
283283
/// Array of [`Definition`] in this scope. Only the first entry should be `None`;
284284
/// this represents the implicit "unbound"/"undeclared" definition of every symbol.
@@ -384,7 +384,7 @@ impl<'db> UseDefMap<'db> {
384384
}
385385

386386
/// Either live bindings or live declarations for a symbol.
387-
#[derive(Debug, PartialEq, Eq)]
387+
#[derive(Debug, PartialEq, Eq, salsa::Update)]
388388
enum SymbolDefinitions {
389389
Bindings(SymbolBindings),
390390
Declarations(SymbolDeclarations),

crates/red_knot_python_semantic/src/symbol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) enum Boundness {
2626
/// possibly_unbound: Symbol::Type(Type::IntLiteral(2), Boundness::PossiblyUnbound),
2727
/// non_existent: Symbol::Unbound,
2828
/// ```
29-
#[derive(Debug, Clone, PartialEq, Eq)]
29+
#[derive(Debug, Clone, PartialEq, Eq, salsa::Update)]
3030
pub(crate) enum Symbol<'db> {
3131
Type(Type<'db>, Boundness),
3232
Unbound,

0 commit comments

Comments
 (0)