give each function a SalsaStruct assoc type

This commit is contained in:
Niko Matsakis 2022-08-08 00:30:15 -04:00
parent 27a240aa86
commit 85d88b8df0
4 changed files with 14 additions and 5 deletions

View file

@ -1,5 +1,6 @@
pub(crate) struct Configuration {
pub(crate) jar_ty: syn::Type,
pub(crate) salsa_struct_ty: syn::Type,
pub(crate) key_ty: syn::Type,
pub(crate) value_ty: syn::Type,
pub(crate) cycle_strategy: CycleRecoveryStrategy,
@ -12,6 +13,7 @@ impl Configuration {
pub(crate) fn to_impl(&self, self_ty: &syn::Type) -> syn::ItemImpl {
let Configuration {
jar_ty,
salsa_struct_ty,
key_ty,
value_ty,
cycle_strategy,
@ -22,6 +24,7 @@ impl Configuration {
parse_quote! {
impl salsa::function::Configuration for #self_ty {
type Jar = #jar_ty;
type SalsaStruct = #salsa_struct_ty;
type Key = #key_ty;
type Value = #value_ty;
const CYCLE_STRATEGY: salsa::cycle::CycleRecoveryStrategy = #cycle_strategy;

View file

@ -221,6 +221,7 @@ impl SalsaStruct {
let item_impl: syn::ItemImpl = parse_quote! {
impl salsa::function::Configuration for #config_name {
type Jar = #jar_ty;
type SalsaStruct = #ident;
type Key = #ident;
type Value = #value_field_ty;
const CYCLE_STRATEGY: salsa::cycle::CycleRecoveryStrategy = salsa::cycle::CycleRecoveryStrategy::Panic;

View file

@ -101,10 +101,7 @@ fn configuration_struct(item_fn: &syn::ItemFn) -> syn::ItemStruct {
parse_quote! {
#[allow(non_camel_case_types)]
#visibility struct #fn_name
where
#salsa_struct_ty: salsa::AsId, // require that the salsa struct is, well, a salsa struct!
{
#visibility struct #fn_name {
intern_map: #intern_map,
function: salsa::function::FunctionIngredient<Self>,
}
@ -127,10 +124,11 @@ fn salsa_struct_ty(item_fn: &syn::ItemFn) -> &syn::Type {
fn fn_configuration(args: &Args, item_fn: &syn::ItemFn) -> Configuration {
let jar_ty = args.jar_ty();
let salsa_struct_ty = salsa_struct_ty(item_fn).clone();
let key_ty = if requires_interning(item_fn) {
parse_quote!(salsa::id::Id)
} else {
salsa_struct_ty(item_fn).clone()
salsa_struct_ty.clone()
};
let value_ty = configuration::value_ty(&item_fn.sig);
@ -187,6 +185,7 @@ fn fn_configuration(args: &Args, item_fn: &syn::ItemFn) -> Configuration {
Configuration {
jar_ty,
salsa_struct_ty,
key_ty,
value_ty,
cycle_strategy,

View file

@ -9,6 +9,7 @@ use crate::{
jar::Jar,
key::{DatabaseKeyIndex, DependencyIndex},
runtime::local_state::QueryInputs,
salsa_struct::SalsaStructInDb,
Cycle, DbWithJar, Id, Revision,
};
@ -69,6 +70,11 @@ pub struct FunctionIngredient<C: Configuration> {
pub trait Configuration {
type Jar: for<'db> Jar<'db>;
/// The "salsa struct type" that this function is associated with.
/// This can be just `salsa::Id` for functions that intern their arguments
/// and are not clearly associated with any one salsa struct.
type SalsaStruct: for<'db> SalsaStructInDb<DynDb<'db, Self>>;
/// What key is used to index the memo. Typically a salsa struct id,
/// but if this memoized function has multiple argments it will be a `salsa::Id`
/// that results from interning those arguments.