Skip to content
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
7 changes: 5 additions & 2 deletions components/salsa-macro-rules/src/setup_tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ macro_rules! setup_tracked_fn {
impl $Configuration {
fn fn_ingredient(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
$FN_CACHE.get_or_create(db.as_dyn_database(), || {
<dyn $Db as $Db>::zalsa_db(db);
<dyn $Db as $Db>::zalsa_register_downcaster(db);
db.zalsa().add_or_lookup_jar_by_type::<$Configuration>()
})
}

pub fn fn_ingredient_mut(db: &mut dyn $Db) -> &mut $zalsa::function::IngredientImpl<Self> {
<dyn $Db as $Db>::zalsa_register_downcaster(db);
let zalsa_mut = db.zalsa_mut();
let index = zalsa_mut.add_or_lookup_jar_by_type::<$Configuration>();
let (ingredient, _) = zalsa_mut.lookup_ingredient_mut(index);
Expand All @@ -159,6 +160,7 @@ macro_rules! setup_tracked_fn {
db: &dyn $Db,
) -> &$zalsa::interned::IngredientImpl<$Configuration> {
$INTERN_CACHE.get_or_create(db.as_dyn_database(), || {
<dyn $Db as $Db>::zalsa_register_downcaster(db);
db.zalsa().add_or_lookup_jar_by_type::<$Configuration>().successor(0)
})
}
Expand Down Expand Up @@ -249,7 +251,8 @@ macro_rules! setup_tracked_fn {
let fn_ingredient = <$zalsa::function::IngredientImpl<$Configuration>>::new(
first_index,
memo_ingredient_indices,
$lru
$lru,
zalsa.views().downcaster_for::<dyn $Db>()
);
$zalsa::macro_if! {
if $needs_interner {
Expand Down
33 changes: 27 additions & 6 deletions components/salsa-macros/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ impl DbMacro {
use salsa::plumbing as #zalsa;

unsafe impl #zalsa::HasStorage for #db {
#[inline(always)]
fn storage(&self) -> &#zalsa::Storage<Self> {
&self.#storage
}

#[inline(always)]
fn storage_mut(&mut self) -> &mut #zalsa::Storage<Self> {
&mut self.#storage
}
Expand All @@ -102,16 +104,26 @@ impl DbMacro {
}

fn add_salsa_view_method(&self, input: &mut syn::ItemTrait) -> syn::Result<()> {
let trait_name = &input.ident;
input.items.push(parse_quote! {
#[doc(hidden)]
fn zalsa_db(&self);
fn zalsa_register_downcaster(&self);
});

let comment = format!(" Downcast a [`dyn Database`] to a [`dyn {trait_name}`]");
input.items.push(parse_quote! {
#[doc = #comment]
///
/// # Safety
///
/// The input database must be of type `Self`.
#[doc(hidden)]
unsafe fn downcast(db: &dyn salsa::plumbing::Database) -> &dyn #trait_name where Self: Sized;
});
Ok(())
}

fn add_salsa_view_method_impl(&self, input: &mut syn::ItemImpl) -> syn::Result<()> {
let zalsa = self.hygiene.ident("zalsa");

let Some((_, TraitPath, _)) = &input.trait_ else {
return Err(syn::Error::new_spanned(
&input.self_ty,
Expand All @@ -121,9 +133,18 @@ impl DbMacro {

input.items.push(parse_quote! {
#[doc(hidden)]
fn zalsa_db(&self) {
use salsa::plumbing as #zalsa;
#zalsa::views(self).add::<Self, dyn #TraitPath>(|t| t);
#[inline(always)]
fn zalsa_register_downcaster(&self) {
salsa::plumbing::views(self).add(<Self as #TraitPath>::downcast);
}
});
input.items.push(parse_quote! {
#[doc(hidden)]
#[inline(always)]
unsafe fn downcast(db: &dyn salsa::plumbing::Database) -> &dyn #TraitPath where Self: Sized {
debug_assert_eq!(db.type_id(), ::core::any::TypeId::of::<Self>());
// SAFETY: Same as the safety of the `downcast` method.
unsafe { &*salsa::plumbing::transmute_data_ptr::<dyn salsa::plumbing::Database, Self>(db) }
}
});
Ok(())
Expand Down
2 changes: 0 additions & 2 deletions components/salsa-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#![recursion_limit = "256"]

extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;

Expand Down
2 changes: 1 addition & 1 deletion src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
self.index
}

fn maybe_changed_after(
unsafe fn maybe_changed_after(
&self,
_db: &dyn Database,
_input: Id,
Expand Down
3 changes: 2 additions & 1 deletion src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl dyn Database {
/// If the view has not been added to the database (see [`crate::views::Views`]).
#[track_caller]
pub fn as_view<DbView: ?Sized + Database>(&self) -> &DbView {
self.zalsa().views().try_view_as(self).unwrap()
let views = self.zalsa().views();
views.downcaster_for().downcast(self)
}
}
18 changes: 15 additions & 3 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
plumbing::MemoIngredientMap,
salsa_struct::SalsaStructInDb,
table::Table,
views::DatabaseDownCaster,
zalsa::{IngredientIndex, MemoIngredientIndex, Zalsa},
zalsa_local::QueryOrigin,
Cycle, Database, Id, Revision,
Expand Down Expand Up @@ -105,6 +106,14 @@ pub struct IngredientImpl<C: Configuration> {
/// Used to find memos to throw out when we have too many memoized values.
lru: lru::Lru,

/// A downcaster from `dyn Database` to `C::DbView`.
///
/// # Safety
///
/// The supplied database must be be the same as the database used to construct the [`Views`]
/// instances that this downcaster was derived from.
view_caster: DatabaseDownCaster<C::DbView>,

/// When `fetch` and friends executes, they return a reference to the
/// value stored in the memo that is extended to live as long as the `&self`
/// reference we start with. This means that whenever we remove something
Expand Down Expand Up @@ -135,12 +144,14 @@ where
index: IngredientIndex,
memo_ingredient_indices: <C::SalsaStruct<'static> as SalsaStructInDb>::MemoIngredientMap,
lru: usize,
view_caster: DatabaseDownCaster<C::DbView>,
) -> Self {
Self {
index,
memo_ingredient_indices,
lru: lru::Lru::new(lru),
deleted_entries: Default::default(),
view_caster,
}
}

Expand Down Expand Up @@ -213,13 +224,14 @@ where
self.index
}

fn maybe_changed_after(
unsafe fn maybe_changed_after(
&self,
db: &dyn Database,
input: Id,
revision: Revision,
) -> MaybeChangedAfter {
let db = db.as_view::<C::DbView>();
// SAFETY: The `db` belongs to the ingredient as per caller invariant
let db = unsafe { self.view_caster.downcast_unchecked(db) };
self.maybe_changed_after(db, input, revision)
}

Expand Down Expand Up @@ -279,7 +291,7 @@ where
db: &'db dyn Database,
key_index: Id,
) -> (Option<&'db AccumulatedMap>, InputAccumulatedValues) {
let db = db.as_view::<C::DbView>();
let db = self.view_caster.downcast(db);
self.accumulated_map(db, key_index)
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/ingredient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
fn debug_name(&self) -> &'static str;

/// Has the value for `input` in this ingredient changed after `revision`?
fn maybe_changed_after<'db>(
///
/// # Safety
///
/// The passed in database needs to be the same one that the ingredient was created with.
unsafe fn maybe_changed_after<'db>(
&'db self,
db: &'db dyn Database,
input: Id,
Expand Down
2 changes: 1 addition & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
self.ingredient_index
}

fn maybe_changed_after(
unsafe fn maybe_changed_after(
&self,
_db: &dyn Database,
_input: Id,
Expand Down
2 changes: 1 addition & 1 deletion src/input/input_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ where
CycleRecoveryStrategy::Panic
}

fn maybe_changed_after(
unsafe fn maybe_changed_after(
&self,
db: &dyn Database,
input: Id,
Expand Down
2 changes: 1 addition & 1 deletion src/interned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ where
self.ingredient_index
}

fn maybe_changed_after(
unsafe fn maybe_changed_after(
&self,
_db: &dyn Database,
_input: Id,
Expand Down
10 changes: 6 additions & 4 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ impl InputDependencyIndex {
last_verified_at: crate::Revision,
) -> MaybeChangedAfter {
match self.key_index {
Some(key_index) => db
.zalsa()
.lookup_ingredient(self.ingredient_index)
.maybe_changed_after(db, key_index, last_verified_at),
// SAFETY: The `db` belongs to the ingredient
Some(key_index) => unsafe {
db.zalsa()
.lookup_ingredient(self.ingredient_index)
.maybe_changed_after(db, key_index, last_verified_at)
},
// Data in tables themselves remain valid until the table as a whole is reset.
None => MaybeChangedAfter::No(InputAccumulatedValues::Empty),
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![forbid(unsafe_op_in_unsafe_fn)]

extern crate self as salsa;

mod accumulator;
mod active_query;
mod array;
Expand Down Expand Up @@ -107,6 +109,7 @@ pub mod plumbing {
pub use crate::update::helper::Dispatch as UpdateDispatch;
pub use crate::update::helper::Fallback as UpdateFallback;
pub use crate::update::Update;
pub use crate::zalsa::transmute_data_ptr;
pub use crate::zalsa::views;
pub use crate::zalsa::IngredientCache;
pub use crate::zalsa::IngredientIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ where
self.ingredient_index
}

fn maybe_changed_after(
unsafe fn maybe_changed_after(
&self,
db: &dyn Database,
input: Id,
Expand Down
2 changes: 1 addition & 1 deletion src/tracked_struct/tracked_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where
crate::cycle::CycleRecoveryStrategy::Panic
}

fn maybe_changed_after<'db>(
unsafe fn maybe_changed_after<'db>(
&'db self,
db: &'db dyn Database,
input: Id,
Expand Down
Loading