Skip to content

Commit 7edce6e

Browse files
authored
Fix returns(deref | as_ref | as_deref) in tracked methods (#857)
1 parent 13a2bd7 commit 7edce6e

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ macro_rules! setup_input_struct {
110110

111111
pub fn ingredient_mut(db: &mut dyn $zalsa::Database) -> (&mut $zalsa_struct::IngredientImpl<Self>, &mut $zalsa::Runtime) {
112112
let zalsa_mut = db.zalsa_mut();
113-
let current_revision = zalsa_mut.new_revision();
113+
zalsa_mut.new_revision();
114114
let index = zalsa_mut.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
115115
let (ingredient, runtime) = zalsa_mut.lookup_ingredient_mut(index);
116116
let ingredient = ingredient.assert_type_mut::<$zalsa_struct::IngredientImpl<Self>>();

components/salsa-macros/src/tracked_impl.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,22 @@ impl Macro {
298298
) -> syn::Result<()> {
299299
if let Some(returns) = &args.returns {
300300
if let syn::ReturnType::Type(_, t) = &mut sig.output {
301-
**t = parse_quote!(& #db_lt #t)
301+
if returns == "copy" || returns == "clone" {
302+
// leave as is
303+
} else if returns == "ref" {
304+
**t = parse_quote!(& #db_lt #t)
305+
} else if returns == "deref" {
306+
**t = parse_quote!(& #db_lt <#t as ::core::ops::Deref>::Target)
307+
} else if returns == "as_ref" {
308+
**t = parse_quote!(<#t as ::salsa::SalsaAsRef>::AsRef<#db_lt>)
309+
} else if returns == "as_deref" {
310+
**t = parse_quote!(<#t as ::salsa::SalsaAsDeref>::AsDeref<#db_lt>)
311+
} else {
312+
return Err(syn::Error::new_spanned(
313+
returns,
314+
format!("Unknown returns mode `{returns}`"),
315+
));
316+
}
302317
} else {
303318
return Err(syn::Error::new_spanned(
304319
returns,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use salsa::Database;
2+
3+
#[salsa::input]
4+
struct Input {
5+
number: usize,
6+
}
7+
8+
#[salsa::tracked]
9+
impl Input {
10+
#[salsa::tracked(returns(deref))]
11+
fn test(self, db: &dyn salsa::Database) -> Vec<String> {
12+
(0..self.number(db)).map(|i| format!("test {i}")).collect()
13+
}
14+
}
15+
16+
#[test]
17+
fn invoke() {
18+
salsa::DatabaseImpl::new().attach(|db| {
19+
let input = Input::new(db, 3);
20+
let x: &[String] = input.test(db);
21+
22+
assert_eq!(
23+
x,
24+
&[
25+
"test 0".to_string(),
26+
"test 1".to_string(),
27+
"test 2".to_string()
28+
]
29+
);
30+
})
31+
}

0 commit comments

Comments
 (0)