[red-knot] Upgrade to the *new* *new* salsa (#12406)

This commit is contained in:
Micha Reiser 2024-07-29 09:21:24 +02:00 committed by GitHub
parent 9495331a5f
commit e18b4e42d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 483 additions and 415 deletions

View file

@ -1,12 +1,8 @@
use std::hash::BuildHasherDefault;
use program::Program;
use rustc_hash::FxHasher;
use salsa::DbWithJar;
use crate::files::{File, Files};
use crate::parsed::parsed_module;
use crate::source::{line_index, source_text};
use crate::files::Files;
use crate::system::System;
use crate::vendored::VendoredFileSystem;
@ -22,11 +18,9 @@ pub mod vendored;
pub type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
pub type FxDashSet<K> = dashmap::DashSet<K, BuildHasherDefault<FxHasher>>;
#[salsa::jar(db=Db)]
pub struct Jar(File, Program, source_text, line_index, parsed_module);
/// Most basic database that gives access to files, the host system, source code, and parsed AST.
pub trait Db: DbWithJar<Jar> {
#[salsa::db]
pub trait Db: salsa::Database {
fn vendored(&self) -> &VendoredFileSystem;
fn system(&self) -> &dyn System;
fn files(&self) -> &Files;
@ -42,19 +36,17 @@ pub trait Upcast<T: ?Sized> {
mod tests {
use std::sync::Arc;
use salsa::DebugWithDb;
use crate::files::Files;
use crate::system::TestSystem;
use crate::system::{DbWithTestSystem, System};
use crate::vendored::VendoredFileSystem;
use crate::{Db, Jar};
use crate::Db;
/// Database that can be used for testing.
///
/// Uses an in memory filesystem and it stubs out the vendored files by default.
#[salsa::db]
#[derive(Default)]
#[salsa::db(Jar)]
pub(crate) struct TestDb {
storage: salsa::Storage<Self>,
files: Files,
@ -101,6 +93,7 @@ mod tests {
}
}
#[salsa::db]
impl Db for TestDb {
fn vendored(&self) -> &VendoredFileSystem {
&self.vendored
@ -125,23 +118,14 @@ mod tests {
}
}
#[salsa::db]
impl salsa::Database for TestDb {
fn salsa_event(&self, event: salsa::Event) {
tracing::trace!("event: {:?}", event.debug(self));
let mut events = self.events.lock().unwrap();
events.push(event);
}
}
impl salsa::ParallelDatabase for TestDb {
fn snapshot(&self) -> salsa::Snapshot<Self> {
salsa::Snapshot::new(Self {
storage: self.storage.snapshot(),
system: self.system.snapshot(),
files: self.files.snapshot(),
events: self.events.clone(),
vendored: self.vendored.snapshot(),
})
salsa::Database::attach(self, |_| {
tracing::trace!("event: {:?}", event);
let mut events = self.events.lock().unwrap();
events.push(event);
});
}
}
}