Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

salsa 3.0 #1033

Merged
merged 8 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ resolver = "2"
[profile.dev.package.solc]
opt-level = 3

[workspace.dependencies]
salsa = { git = "https://github.com/salsa-rs/salsa", rev = "e4d65a656fc68d0fb759b292ceae2aff2c785c5d" }

[profile.dev]
# Set to 0 to make the build faster and debugging more difficult.
debug = 1
3 changes: 2 additions & 1 deletion crates/common2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ description = "Provides HIR definition and lowering for Fe lang."
semver = "1.0.17"
camino = "1.1.4"
smol_str = "0.1.24"
salsa = { git = "https://github.com/salsa-rs/salsa", rev = "a1bf3a6" }
salsa.workspace = true
indexmap = "2.2"
parser = { path = "../parser2", package = "fe-parser2" }
paste = "1.0.15"
2 changes: 1 addition & 1 deletion crates/common2/src/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
ops::{Deref, DerefMut},
};

use salsa::update::Update;
use salsa::Update;

#[derive(Debug, Clone)]
pub struct IndexMap<K, V, S = RandomState>(indexmap::IndexMap<K, V, S>);
Expand Down
8 changes: 3 additions & 5 deletions crates/common2/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use camino::Utf8PathBuf;
use salsa::Setter;
use smol_str::SmolStr;

use crate::{indexmap::IndexSet, InputDb};
Expand Down Expand Up @@ -74,9 +75,6 @@ impl InputIngot {

#[salsa::input]
pub struct InputFile {
/// A ingot id which the file belongs to.
pub ingot: InputIngot,

/// A path to the file from the ingot root directory.
#[return_ref]
pub path: Utf8PathBuf,
Expand All @@ -86,8 +84,8 @@ pub struct InputFile {
}

impl InputFile {
pub fn abs_path(&self, db: &dyn InputDb) -> Utf8PathBuf {
self.ingot(db).path(db).join(self.path(db))
pub fn abs_path(&self, db: &dyn InputDb, ingot: InputIngot) -> Utf8PathBuf {
ingot.path(db).join(self.path(db))
}
}

Expand Down
34 changes: 26 additions & 8 deletions crates/common2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
pub mod diagnostics;
pub mod indexmap;
pub mod input;

pub use input::{InputFile, InputIngot};

#[salsa::jar(db = InputDb)]
pub struct Jar(InputIngot, InputFile);
#[salsa::db]
pub trait InputDb: salsa::Database {
fn as_input_db(&self) -> &dyn InputDb;
}

#[doc(hidden)]
pub use paste::paste;

#[macro_export]
macro_rules! impl_db_traits {
($db_type:ty, $($trait_name:ident),+ $(,)?) => {
#[salsa::db]
impl salsa::Database for $db_type {
fn salsa_event(&self, _event: &dyn Fn() -> salsa::Event) {}
}

pub trait InputDb: salsa::DbWithJar<Jar> {
fn as_input_db(&self) -> &dyn InputDb {
<Self as salsa::DbWithJar<Jar>>::as_jar_db::<'_>(self)
}
$(
$crate::paste! {
#[salsa::db]
impl $trait_name for $db_type {
fn [<as_ $trait_name:snake>](&self) -> &dyn $trait_name {
self
}
}
}
)+
};
}
impl<DB> InputDb for DB where DB: ?Sized + salsa::DbWithJar<Jar> {}
2 changes: 1 addition & 1 deletion crates/driver2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "Provides Fe driver"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
salsa = { git = "https://github.com/salsa-rs/salsa", rev = "a1bf3a6" }
salsa.workspace = true
codespan-reporting = "0.11"

hir = { path = "../hir", package = "fe-hir" }
Expand Down
47 changes: 20 additions & 27 deletions crates/driver2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use codespan_reporting::term::{
};
use common::{
diagnostics::CompleteDiagnostic,
impl_db_traits,
indexmap::IndexSet,
input::{IngotKind, Version},
InputDb, InputFile, InputIngot,
Expand All @@ -28,31 +29,27 @@ use hir_analysis::{

use crate::diagnostics::ToCsDiag;

#[salsa::jar(db = DriverDb)]
pub struct Jar(diagnostics::file_line_starts);

#[salsa::db]
pub trait DriverDb:
salsa::DbWithJar<Jar> + HirAnalysisDb + HirDb + LowerHirDb + SpannedHirDb + InputDb
{
}

impl<DB> DriverDb for DB where
DB: salsa::DbWithJar<Jar> + HirAnalysisDb + HirDb + LowerHirDb + SpannedHirDb + InputDb
salsa::Database + HirAnalysisDb + HirDb + LowerHirDb + SpannedHirDb + InputDb
{
fn as_driver_db(&self) -> &dyn DriverDb;
}

#[derive(Default)]
#[salsa::db(
common::Jar,
hir::Jar,
hir::LowerJar,
hir::SpannedJar,
hir_analysis::Jar,
Jar
)]
#[derive(Default, Clone)]
#[salsa::db]
pub struct DriverDataBase {
storage: salsa::Storage<Self>,
}
impl_db_traits!(
DriverDataBase,
InputDb,
HirDb,
LowerHirDb,
SpannedHirDb,
HirAnalysisDb,
DriverDb
);

impl DriverDataBase {
// TODO: An temporary implementation for ui testing.
Expand All @@ -72,7 +69,7 @@ impl DriverDataBase {
DiagnosticsCollection(pass_manager.run_on_module(top_mod))
}

pub fn standalone(&mut self, file_path: &path::Path, source: &str) -> InputFile {
pub fn standalone(&mut self, file_path: &path::Path, source: &str) -> (InputIngot, InputFile) {
let kind = IngotKind::StandAlone;

// We set the ingot version to 0.0.0 for stand-alone file.
Expand All @@ -87,21 +84,17 @@ impl DriverDataBase {
);

let file_name = root_file.file_name().unwrap().to_str().unwrap();
let input_file = InputFile::new(self, ingot, file_name.into(), source.to_string());
let input_file = InputFile::new(self, file_name.into(), source.to_string());
ingot.set_root_file(self, input_file);
ingot.set_files(self, [input_file].into_iter().collect());
input_file
(ingot, input_file)
}

pub fn top_mod(&self, input: InputFile) -> TopLevelMod {
map_file_to_mod(self, input)
pub fn top_mod(&self, ingot: InputIngot, input: InputFile) -> TopLevelMod {
map_file_to_mod(self, ingot, input)
}
}

impl salsa::Database for DriverDataBase {
fn salsa_event(&self, _: salsa::Event) {}
}

pub struct DiagnosticsCollection<'db>(Vec<Box<dyn DiagnosticVoucher<'db> + 'db>>);
impl<'db> DiagnosticsCollection<'db> {
pub fn emit(&self, db: &'db DriverDataBase) {
Expand Down
4 changes: 2 additions & 2 deletions crates/driver2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub fn main() {
let source = std::fs::read_to_string(&args.file_path).unwrap();

let mut db = DriverDataBase::default();
let input_file = db.standalone(path, &source);
let top_mod = db.top_mod(input_file);
let (ingot, file) = db.standalone(path, &source);
let top_mod = db.top_mod(ingot, file);
let diags = db.run_on_top_mod(top_mod);
diags.emit(&db);

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository = "https://github.com/ethereum/fe"
description = "Provides HIR semantic analysis for Fe lang"

[dependencies]
salsa = { git = "https://github.com/salsa-rs/salsa", rev = "a1bf3a6" }
salsa.workspace = true
smallvec = "1.10"
rustc-hash = "1.1.0"
either = "1.8"
Expand Down
Loading
Loading