Skip to content

Commit f384ab5

Browse files
authored
Emit self ty for query debug name of assoc function queries (#927)
1 parent d44f638 commit f384ab5

File tree

10 files changed

+133
-7
lines changed

10 files changed

+133
-7
lines changed

components/salsa-macro-rules/src/setup_tracked_fn.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ macro_rules! setup_tracked_fn {
6666

6767
assert_return_type_is_update: {$($assert_return_type_is_update:tt)*},
6868

69+
$(self_ty: $self_ty:ty,)?
70+
6971
// Annoyingly macro-rules hygiene does not extend to items defined in the macro.
7072
// We have the procedural macro generate names for those items that are
7173
// not used elsewhere in the user's code.
@@ -139,7 +141,7 @@ macro_rules! setup_tracked_fn {
139141
file: file!(),
140142
line: line!(),
141143
};
142-
const DEBUG_NAME: &'static str = concat!(stringify!($fn_name), "::interned_arguments");
144+
const DEBUG_NAME: &'static str = concat!($(stringify!($self_ty), "::",)? stringify!($fn_name), "::interned_arguments");
143145

144146
type Fields<$db_lt> = ($($interned_input_ty),*);
145147

@@ -194,7 +196,7 @@ macro_rules! setup_tracked_fn {
194196
file: file!(),
195197
line: line!(),
196198
};
197-
const DEBUG_NAME: &'static str = stringify!($fn_name);
199+
const DEBUG_NAME: &'static str = concat!($(stringify!($self_ty), "::", )? stringify!($fn_name));
198200

199201
type DbView = dyn $Db;
200202

components/salsa-macros/src/accumulator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl AllowedOptions for Accumulator {
4646
const ID: bool = false;
4747
const REVISIONS: bool = false;
4848
const HEAP_SIZE: bool = false;
49+
const SELF_TY: bool = false;
4950
}
5051

5152
struct StructMacro {

components/salsa-macros/src/input.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ impl crate::options::AllowedOptions for InputStruct {
6666
const REVISIONS: bool = false;
6767

6868
const HEAP_SIZE: bool = false;
69+
70+
const SELF_TY: bool = false;
6971
}
7072

7173
impl SalsaStructAllowedOptions for InputStruct {

components/salsa-macros/src/interned.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ impl crate::options::AllowedOptions for InternedStruct {
6666
const REVISIONS: bool = true;
6767

6868
const HEAP_SIZE: bool = false;
69+
70+
const SELF_TY: bool = false;
6971
}
7072

7173
impl SalsaStructAllowedOptions for InternedStruct {

components/salsa-macros/src/options.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ pub(crate) struct Options<A: AllowedOptions> {
105105
/// If this is `Some`, the value is the provided `heap_size` function.
106106
pub heap_size_fn: Option<syn::Path>,
107107

108+
/// The `self_ty = <Ty>` option is used to set the the self type of the tracked impl for tracked
109+
/// functions. This is merely used to refine the query name.
110+
pub self_ty: Option<syn::Type>,
111+
108112
/// Remember the `A` parameter, which plays no role after parsing.
109113
phantom: PhantomData<A>,
110114
}
@@ -130,6 +134,7 @@ impl<A: AllowedOptions> Default for Options<A> {
130134
id: Default::default(),
131135
revisions: Default::default(),
132136
heap_size_fn: Default::default(),
137+
self_ty: Default::default(),
133138
}
134139
}
135140
}
@@ -153,6 +158,7 @@ pub(crate) trait AllowedOptions {
153158
const ID: bool;
154159
const REVISIONS: bool;
155160
const HEAP_SIZE: bool;
161+
const SELF_TY: bool;
156162
}
157163

158164
type Equals = syn::Token![=];
@@ -416,6 +422,22 @@ impl<A: AllowedOptions> syn::parse::Parse for Options<A> {
416422
"`heap_size` option not allowed here",
417423
));
418424
}
425+
} else if ident == "self_ty" {
426+
if A::SELF_TY {
427+
let _eq = Equals::parse(input)?;
428+
let ty = syn::Type::parse(input)?;
429+
if let Some(old) = options.self_ty.replace(ty) {
430+
return Err(syn::Error::new(
431+
old.span(),
432+
"option `self_ty` provided twice",
433+
));
434+
}
435+
} else {
436+
return Err(syn::Error::new(
437+
ident.span(),
438+
"`self_ty` option not allowed here",
439+
));
440+
}
419441
} else {
420442
return Err(syn::Error::new(
421443
ident.span(),
@@ -433,3 +455,82 @@ impl<A: AllowedOptions> syn::parse::Parse for Options<A> {
433455
Ok(options)
434456
}
435457
}
458+
impl<A: AllowedOptions> quote::ToTokens for Options<A> {
459+
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
460+
let Self {
461+
returns,
462+
no_eq,
463+
debug,
464+
no_lifetime,
465+
singleton,
466+
specify,
467+
non_update_return_type,
468+
db_path,
469+
cycle_fn,
470+
cycle_initial,
471+
cycle_result,
472+
data,
473+
lru,
474+
constructor_name,
475+
id,
476+
revisions,
477+
heap_size_fn,
478+
self_ty,
479+
phantom: _,
480+
} = self;
481+
if let Some(returns) = returns {
482+
tokens.extend(quote::quote! { returns(#returns), });
483+
};
484+
if no_eq.is_some() {
485+
tokens.extend(quote::quote! { no_eq, });
486+
}
487+
if debug.is_some() {
488+
tokens.extend(quote::quote! { debug, });
489+
}
490+
if no_lifetime.is_some() {
491+
tokens.extend(quote::quote! { no_lifetime, });
492+
}
493+
if singleton.is_some() {
494+
tokens.extend(quote::quote! { singleton, });
495+
}
496+
if specify.is_some() {
497+
tokens.extend(quote::quote! { specify, });
498+
}
499+
if non_update_return_type.is_some() {
500+
tokens.extend(quote::quote! { unsafe(non_update_return_type), });
501+
}
502+
if let Some(db_path) = db_path {
503+
tokens.extend(quote::quote! { db = #db_path, });
504+
}
505+
if let Some(cycle_fn) = cycle_fn {
506+
tokens.extend(quote::quote! { cycle_fn = #cycle_fn, });
507+
}
508+
if let Some(cycle_initial) = cycle_initial {
509+
tokens.extend(quote::quote! { cycle_initial = #cycle_initial, });
510+
}
511+
if let Some(cycle_result) = cycle_result {
512+
tokens.extend(quote::quote! { cycle_result = #cycle_result, });
513+
}
514+
if let Some(data) = data {
515+
tokens.extend(quote::quote! { data = #data, });
516+
}
517+
if let Some(lru) = lru {
518+
tokens.extend(quote::quote! { lru = #lru, });
519+
}
520+
if let Some(constructor_name) = constructor_name {
521+
tokens.extend(quote::quote! { constructor = #constructor_name, });
522+
}
523+
if let Some(id) = id {
524+
tokens.extend(quote::quote! { id = #id, });
525+
}
526+
if let Some(revisions) = revisions {
527+
tokens.extend(quote::quote! { revisions = #revisions, });
528+
}
529+
if let Some(heap_size_fn) = heap_size_fn {
530+
tokens.extend(quote::quote! { heap_size_fn = #heap_size_fn, });
531+
}
532+
if let Some(self_ty) = self_ty {
533+
tokens.extend(quote::quote! { self_ty = #self_ty, });
534+
}
535+
}
536+
}

components/salsa-macros/src/tracked_fn.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ impl crate::options::AllowedOptions for TrackedFn {
5959
const REVISIONS: bool = false;
6060

6161
const HEAP_SIZE: bool = true;
62+
63+
const SELF_TY: bool = true;
6264
}
6365

6466
struct Macro {
@@ -199,6 +201,10 @@ impl Macro {
199201
} else {
200202
quote! {}
201203
};
204+
let self_ty = match &self.args.self_ty {
205+
Some(ty) => quote! { self_ty: #ty, },
206+
None => quote! {},
207+
};
202208

203209
Ok(crate::debug::dump_tokens(
204210
fn_name,
@@ -224,6 +230,7 @@ impl Macro {
224230
lru: #lru,
225231
return_mode: #return_mode,
226232
assert_return_type_is_update: { #assert_return_type_is_update },
233+
#self_ty
227234
unused_names: [
228235
#zalsa,
229236
#Configuration,

components/salsa-macros/src/tracked_impl.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Macro {
6969
return Ok(());
7070
};
7171

72-
let self_ty = &impl_item.self_ty;
72+
let self_ty = &*impl_item.self_ty;
7373

7474
let Some(tracked_attr_index) = fn_item.attrs.iter().position(|a| self.is_tracked_attr(a))
7575
else {
@@ -83,11 +83,20 @@ impl Macro {
8383
let mut change = ChangeSelfPath::new(self_ty, trait_);
8484
change.visit_impl_item_fn_mut(fn_item);
8585

86-
let salsa_tracked_attr = fn_item.attrs.remove(tracked_attr_index);
87-
let args: FnArgs = match &salsa_tracked_attr.meta {
86+
let mut salsa_tracked_attr = fn_item.attrs.remove(tracked_attr_index);
87+
let mut args: FnArgs = match &salsa_tracked_attr.meta {
8888
syn::Meta::Path(..) => Default::default(),
8989
_ => salsa_tracked_attr.parse_args()?,
9090
};
91+
if args.self_ty.is_none() {
92+
// If the user did not specify a self_ty, we use the impl's self_ty
93+
args.self_ty = Some(self_ty.clone());
94+
}
95+
salsa_tracked_attr.meta = syn::Meta::List(syn::MetaList {
96+
path: salsa_tracked_attr.path().clone(),
97+
delimiter: syn::MacroDelimiter::Paren(syn::token::Paren::default()),
98+
tokens: quote! {#args},
99+
});
91100

92101
let InnerTrait = self.hygiene.ident("InnerTrait");
93102
let inner_fn_name = self.hygiene.ident(&fn_item.sig.ident.to_string());

components/salsa-macros/src/tracked_struct.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ impl crate::options::AllowedOptions for TrackedStruct {
6262
const REVISIONS: bool = false;
6363

6464
const HEAP_SIZE: bool = false;
65+
66+
const SELF_TY: bool = false;
6567
}
6668

6769
impl SalsaStructAllowedOptions for TrackedStruct {

tests/tracked_assoc_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ fn debug_name() {
8080
assert_eq!(output.field(&db), 88);
8181
db.assert_logs(expect![[r#"
8282
[
83-
"salsa_event(WillExecute { database_key: tracked_trait_fn_(Id(0)) })",
83+
"salsa_event(WillExecute { database_key: MyOutput < 'db >::tracked_trait_fn_(Id(0)) })",
8484
]"#]]);
8585
}

tests/tracked_method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ fn debug_name() {
5454
assert_eq!(object.tracked_trait_fn(&db), 88);
5555
db.assert_logs(expect![[r#"
5656
[
57-
"salsa_event(WillExecute { database_key: tracked_trait_fn_(Id(0)) })",
57+
"salsa_event(WillExecute { database_key: MyInput::tracked_trait_fn_(Id(0)) })",
5858
]"#]]);
5959
}

0 commit comments

Comments
 (0)