Skip to content

Fix returns(deref | as_ref | as_deref) in tracked methods #857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
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
2 changes: 1 addition & 1 deletion components/salsa-macro-rules/src/setup_input_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ macro_rules! setup_input_struct {

pub fn ingredient_mut(db: &mut dyn $zalsa::Database) -> (&mut $zalsa_struct::IngredientImpl<Self>, &mut $zalsa::Runtime) {
let zalsa_mut = db.zalsa_mut();
let current_revision = zalsa_mut.new_revision();
zalsa_mut.new_revision();
let index = zalsa_mut.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
let (ingredient, runtime) = zalsa_mut.lookup_ingredient_mut(index);
let ingredient = ingredient.assert_type_mut::<$zalsa_struct::IngredientImpl<Self>>();
Expand Down
17 changes: 16 additions & 1 deletion components/salsa-macros/src/tracked_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,22 @@ impl Macro {
) -> syn::Result<()> {
if let Some(returns) = &args.returns {
if let syn::ReturnType::Type(_, t) = &mut sig.output {
**t = parse_quote!(& #db_lt #t)
if returns == "copy" || returns == "clone" {
// leave as is
} else if returns == "ref" {
**t = parse_quote!(& #db_lt #t)
} else if returns == "deref" {
**t = parse_quote!(& #db_lt <#t as ::core::ops::Deref>::Target)
} else if returns == "as_ref" {
**t = parse_quote!(<#t as ::salsa::SalsaAsRef>::AsRef<#db_lt>)
} else if returns == "as_deref" {
**t = parse_quote!(<#t as ::salsa::SalsaAsDeref>::AsDeref<#db_lt>)
} else {
return Err(syn::Error::new_spanned(
returns,
format!("Unknown returns mode `{returns}`"),
));
}
} else {
return Err(syn::Error::new_spanned(
returns,
Expand Down
31 changes: 31 additions & 0 deletions tests/tracked_method_inherent_return_deref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use salsa::Database;

#[salsa::input]
struct Input {
number: usize,
}

#[salsa::tracked]
impl Input {
#[salsa::tracked(returns(deref))]
fn test(self, db: &dyn salsa::Database) -> Vec<String> {
(0..self.number(db)).map(|i| format!("test {i}")).collect()
}
}

#[test]
fn invoke() {
salsa::DatabaseImpl::new().attach(|db| {
let input = Input::new(db, 3);
let x: &[String] = input.test(db);

assert_eq!(
x,
&[
"test 0".to_string(),
"test 1".to_string(),
"test 2".to_string()
]
);
})
}