Skip to content

Commit

Permalink
[red-knot] Upgrade to the *new* *new* salsa (#12406)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Jul 29, 2024
1 parent 9495331 commit e18b4e4
Show file tree
Hide file tree
Showing 30 changed files with 477 additions and 409 deletions.
118 changes: 111 additions & 7 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"

[workspace.package]
edition = "2021"
rust-version = "1.75"
rust-version = "1.76"
homepage = "https://docs.astral.sh/ruff"
documentation = "https://docs.astral.sh/ruff"
repository = "https://github.com/astral-sh/ruff"
Expand Down Expand Up @@ -107,7 +107,7 @@ rand = { version = "0.8.5" }
rayon = { version = "1.10.0" }
regex = { version = "1.10.2" }
rustc-hash = { version = "2.0.0" }
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "a1bf3a613f451af7fc0a59411c56abc47fe8e8e1" }
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "cd339fc1c9a6ea0ffb1d09bd3bffb5633f776ef3" }
schemars = { version = "0.8.16" }
seahash = { version = "4.1.0" }
serde = { version = "1.0.197", features = ["derive"] }
Expand Down
74 changes: 26 additions & 48 deletions crates/red_knot/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
use std::panic::{AssertUnwindSafe, RefUnwindSafe};
use std::sync::Arc;

use salsa::{Cancelled, Database, DbWithJar};
use salsa::Cancelled;

use red_knot_module_resolver::{vendored_typeshed_stubs, Db as ResolverDb, Jar as ResolverJar};
use red_knot_python_semantic::{Db as SemanticDb, Jar as SemanticJar};
use red_knot_module_resolver::{vendored_typeshed_stubs, Db as ResolverDb};
use red_knot_python_semantic::Db as SemanticDb;
use ruff_db::files::{File, Files};
use ruff_db::program::{Program, ProgramSettings};
use ruff_db::system::System;
use ruff_db::vendored::VendoredFileSystem;
use ruff_db::{Db as SourceDb, Jar as SourceJar, Upcast};
use ruff_db::{Db as SourceDb, Upcast};

use crate::lint::{lint_semantic, lint_syntax, unwind_if_cancelled, Diagnostics};
use crate::workspace::{check_file, Package, Package_files, Workspace, WorkspaceMetadata};
use crate::lint::Diagnostics;
use crate::workspace::{check_file, Workspace, WorkspaceMetadata};

mod changes;

pub trait Db: DbWithJar<Jar> + SemanticDb + Upcast<dyn SemanticDb> {}
#[salsa::db]
pub trait Db: SemanticDb + Upcast<dyn SemanticDb> {}

#[salsa::jar(db=Db)]
pub struct Jar(
Workspace,
Package,
Package_files,
lint_syntax,
lint_semantic,
unwind_if_cancelled,
);

#[salsa::db(SourceJar, ResolverJar, SemanticJar, Jar)]
#[salsa::db]
pub struct RootDatabase {
workspace: Option<Workspace>,
storage: salsa::Storage<RootDatabase>,
Expand Down Expand Up @@ -127,10 +118,13 @@ impl Upcast<dyn ResolverDb> for RootDatabase {
}
}

#[salsa::db]
impl ResolverDb for RootDatabase {}

#[salsa::db]
impl SemanticDb for RootDatabase {}

#[salsa::db]
impl SourceDb for RootDatabase {
fn vendored(&self) -> &VendoredFileSystem {
vendored_typeshed_stubs()
Expand All @@ -145,33 +139,23 @@ impl SourceDb for RootDatabase {
}
}

impl Database for RootDatabase {}
#[salsa::db]
impl salsa::Database for RootDatabase {}

#[salsa::db]
impl Db for RootDatabase {}

impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<Self> {
salsa::Snapshot::new(Self {
workspace: self.workspace,
storage: self.storage.snapshot(),
files: self.files.snapshot(),
system: self.system.clone(),
})
}
}

#[cfg(test)]
pub(crate) mod tests {
use red_knot_module_resolver::{vendored_typeshed_stubs, Db as ResolverDb, Jar as ResolverJar};
use red_knot_python_semantic::{Db as SemanticDb, Jar as SemanticJar};
use crate::db::Db;
use red_knot_module_resolver::{vendored_typeshed_stubs, Db as ResolverDb};
use red_knot_python_semantic::Db as SemanticDb;
use ruff_db::files::Files;
use ruff_db::system::{DbWithTestSystem, System, TestSystem};
use ruff_db::vendored::VendoredFileSystem;
use ruff_db::{Db as SourceDb, Jar as SourceJar, Upcast};
use ruff_db::{Db as SourceDb, Upcast};

use super::{Db, Jar};

#[salsa::db(Jar, SemanticJar, ResolverJar, SourceJar)]
#[salsa::db]
pub(crate) struct TestDb {
storage: salsa::Storage<Self>,
files: Files,
Expand All @@ -184,7 +168,7 @@ pub(crate) mod tests {
Self {
storage: salsa::Storage::default(),
system: TestSystem::default(),
vendored: vendored_typeshed_stubs().snapshot(),
vendored: vendored_typeshed_stubs().clone(),
files: Files::default(),
}
}
Expand All @@ -200,6 +184,7 @@ pub(crate) mod tests {
}
}

#[salsa::db]
impl SourceDb for TestDb {
fn vendored(&self) -> &VendoredFileSystem {
&self.vendored
Expand Down Expand Up @@ -241,20 +226,13 @@ pub(crate) mod tests {
}
}

#[salsa::db]
impl red_knot_module_resolver::Db for TestDb {}
#[salsa::db]
impl red_knot_python_semantic::Db for TestDb {}
#[salsa::db]
impl Db for TestDb {}

#[salsa::db]
impl salsa::Database for TestDb {}

impl salsa::ParallelDatabase for TestDb {
fn snapshot(&self) -> salsa::Snapshot<Self> {
salsa::Snapshot::new(Self {
storage: self.storage.snapshot(),
files: self.files.snapshot(),
system: self.system.snapshot(),
vendored: self.vendored.snapshot(),
})
}
}
}
2 changes: 0 additions & 2 deletions crates/red_knot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::db::Jar;

pub mod db;
pub mod lint;
pub mod watch;
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn lint_lines(source: &str, diagnostics: &mut Vec<String>) {
#[allow(unreachable_pub)]
#[salsa::tracked(return_ref)]
pub fn lint_semantic(db: &dyn Db, file_id: File) -> Diagnostics {
let _span = trace_span!("lint_semantic", ?file_id).entered();
let _span = trace_span!("lint_semantic", file=?file_id.path(db)).entered();

let source = source_text(db.upcast(), file_id);
let parsed = parsed_module(db.upcast(), file_id);
Expand Down
Loading

0 comments on commit e18b4e4

Please sign in to comment.