Allow lifetimes in arguments in tracked fns with >1 parameters (#880)

This commit is contained in:
Chayim Refael Friedman 2025-05-26 07:50:43 +03:00 committed by GitHub
parent 0414d89327
commit 2a54667121
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 8 deletions

View file

@ -28,6 +28,9 @@ macro_rules! setup_tracked_fn {
// Types of the function arguments (may reference `$generics`).
input_tys: [$($input_ty:ty),*],
// Types of the function arguments as should be put in interned struct.
interned_input_tys: [$($interned_input_ty:ty),*],
// Return type of the function (may reference `$generics`).
output_ty: $output_ty:ty,
@ -135,12 +138,12 @@ macro_rules! setup_tracked_fn {
};
const DEBUG_NAME: &'static str = concat!(stringify!($fn_name), "::interned_arguments");
type Fields<$db_lt> = ($($input_ty),*);
type Fields<$db_lt> = ($($interned_input_ty),*);
type Struct<$db_lt> = $InternedData<$db_lt>;
}
} else {
type $InternedData<$db_lt> = ($($input_ty),*);
type $InternedData<$db_lt> = ($($interned_input_ty),*);
}
}
@ -185,7 +188,7 @@ macro_rules! setup_tracked_fn {
type SalsaStruct<$db_lt> = $InternedData<$db_lt>;
type Input<$db_lt> = ($($input_ty),*);
type Input<$db_lt> = ($($interned_input_ty),*);
type Output<$db_lt> = $output_ty;
@ -193,7 +196,7 @@ macro_rules! setup_tracked_fn {
$($values_equal)+
fn execute<$db_lt>($db: &$db_lt Self::DbView, ($($input_id),*): ($($input_ty),*)) -> Self::Output<$db_lt> {
fn execute<$db_lt>($db: &$db_lt Self::DbView, ($($input_id),*): ($($interned_input_ty),*)) -> Self::Output<$db_lt> {
$($assert_return_type_is_update)*
$($inner_fn)*
@ -201,7 +204,7 @@ macro_rules! setup_tracked_fn {
$inner($db, $($input_id),*)
}
fn cycle_initial<$db_lt>(db: &$db_lt Self::DbView, ($($input_id),*): ($($input_ty),*)) -> Self::Output<$db_lt> {
fn cycle_initial<$db_lt>(db: &$db_lt Self::DbView, ($($input_id),*): ($($interned_input_ty),*)) -> Self::Output<$db_lt> {
$($cycle_recovery_initial)*(db, $($input_id),*)
}
@ -209,7 +212,7 @@ macro_rules! setup_tracked_fn {
db: &$db_lt dyn $Db,
value: &Self::Output<$db_lt>,
count: u32,
($($input_id),*): ($($input_ty),*)
($($input_id),*): ($($interned_input_ty),*)
) -> $zalsa::CycleRecoveryAction<Self::Output<$db_lt>> {
$($cycle_recovery_fn)*(db, value, count, $($input_id),*)
}
@ -305,7 +308,7 @@ macro_rules! setup_tracked_fn {
impl $fn_name {
pub fn accumulated<$db_lt, A: salsa::Accumulator>(
$db: &$db_lt dyn $Db,
$($input_id: $input_ty,)*
$($input_id: $interned_input_ty,)*
) -> Vec<&$db_lt A> {
use salsa::plumbing as $zalsa;
let key = $zalsa::macro_if! {
@ -322,7 +325,7 @@ macro_rules! setup_tracked_fn {
$zalsa::macro_if! { $is_specifiable =>
pub fn specify<$db_lt>(
$db: &$db_lt dyn $Db,
$($input_id: $input_ty,)*
$($input_id: $interned_input_ty,)*
value: $output_ty,
) {
let key = $zalsa::AsId::as_id(&($($input_id),*));

View file

@ -80,6 +80,16 @@ impl Macro {
let db_lt = db_lifetime::db_lifetime(&item.sig.generics);
let input_ids = self.input_ids(&item);
let input_tys = self.input_tys(&item)?;
let interned_input_tys = input_tys.iter().map(|&ty| {
let mut ty = ty.clone();
syn::visit_mut::visit_type_mut(
&mut ToDbLifetimeVisitor {
db_lifetime: db_lt.clone(),
},
&mut ty,
);
ty
});
let output_ty = self.output_ty(&db_lt, &item)?;
let (cycle_recovery_fn, cycle_recovery_initial, cycle_recovery_strategy) =
self.cycle_recovery()?;
@ -196,6 +206,7 @@ impl Macro {
db: #db_ident,
input_ids: [#(#input_ids),*],
input_tys: [#(#input_tys),*],
interned_input_tys: [#(#interned_input_tys),*],
output_ty: #output_ty,
inner_fn: { #inner_fn },
cycle_recovery_fn: #cycle_recovery_fn,
@ -286,6 +297,16 @@ impl Macro {
}
}
struct ToDbLifetimeVisitor {
db_lifetime: syn::Lifetime,
}
impl syn::visit_mut::VisitMut for ToDbLifetimeVisitor {
fn visit_lifetime_mut(&mut self, i: &mut syn::Lifetime) {
i.clone_from(&self.db_lifetime);
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
enum FunctionType {
Constant,

View file

@ -0,0 +1,14 @@
#[salsa::interned]
struct Interned<'db> {
field: i32,
}
#[salsa::tracked]
fn foo<'a>(_db: &'a dyn salsa::Database, _: Interned<'_>, _: Interned<'a>) {}
#[test]
fn the_test() {
let db = salsa::DatabaseImpl::new();
let i = Interned::new(&db, 123);
foo(&db, i, i);
}