Skip to content
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

Allow interned values as tracked function arguments #523

Merged
merged 1 commit into from
Jul 22, 2024
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
6 changes: 6 additions & 0 deletions components/salsa-macro-rules/src/setup_interned_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ macro_rules! setup_interned_struct {
}
}

impl<$db_lt> $zalsa::LookupId<$db_lt> for $Struct<$db_lt> {
fn lookup_id(id: salsa::Id, db: &$db_lt dyn $zalsa::Database) -> Self {
$Configuration::ingredient(db).interned_value(id)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure about this but it compiles and the test passes ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this looks good.

}
}

unsafe impl Send for $Struct<'_> {}

unsafe impl Sync for $Struct<'_> {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
error[E0277]: the trait bound `MyInterned<'_>: LookupId<'_>` is not satisfied
--> tests/compile-fail/specify-does-not-work-if-the-key-is-a-salsa-interned.rs:15:1
|
15 | #[salsa::tracked(specify)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromId` is not implemented for `MyInterned<'_>`, which is required by `MyInterned<'_>: LookupId<'_>`
|
= help: the trait `LookupId<'db>` is implemented for `MyTracked<'db>`
= note: required for `MyInterned<'_>` to implement `LookupId<'_>`
= note: this error originates in the macro `salsa::plumbing::setup_tracked_fn` which comes from the expansion of the attribute macro `salsa::tracked` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `MyInterned<'_>: TrackedStructInDb` is not satisfied
--> tests/compile-fail/specify-does-not-work-if-the-key-is-a-salsa-interned.rs:15:1
|
Expand Down
29 changes: 29 additions & 0 deletions tests/tracked_fn_on_interned.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Test that a `tracked` fn on a `salsa::interned`
//! compiles and executes successfully.

#[salsa::interned]
struct Name<'db> {
name: String,
}

#[salsa::tracked]
fn tracked_fn<'db>(db: &'db dyn salsa::Database, name: Name<'db>) -> String {
name.name(db).clone()
}

#[salsa::db]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}

#[salsa::db]
impl salsa::Database for Database {}

#[test]
fn execute() {
let db = Database::default();
let name = Name::new(&db, "Salsa".to_string());

assert_eq!(tracked_fn(&db, name), "Salsa");
}
Loading