diff --git a/components/salsa-2022/src/runtime/local_state.rs b/components/salsa-2022/src/runtime/local_state.rs index 91f26ed..57724d3 100644 --- a/components/salsa-2022/src/runtime/local_state.rs +++ b/components/salsa-2022/src/runtime/local_state.rs @@ -291,7 +291,10 @@ impl LocalState { #[track_caller] pub(crate) fn disambiguate(&self, data_hash: u64) -> (DatabaseKeyIndex, Disambiguator) { - assert!(self.query_in_progress()); + assert!( + self.query_in_progress(), + "cannot create a tracked struct disambiguator outside of a tracked function" + ); self.with_query_stack(|stack| { let top_query = stack.last_mut().unwrap(); let disambiguator = top_query.disambiguate(data_hash); diff --git a/salsa-2022-tests/tests/panic-when-creating-tracked-struct-outside-of-tracked-fn.rs b/salsa-2022-tests/tests/panic-when-creating-tracked-struct-outside-of-tracked-fn.rs new file mode 100644 index 0000000..100b6bb --- /dev/null +++ b/salsa-2022-tests/tests/panic-when-creating-tracked-struct-outside-of-tracked-fn.rs @@ -0,0 +1,31 @@ +//! Test that creating a tracked struct outside of a +//! tracked function panics with an assert message. + +#[salsa::jar(db = Db)] +struct Jar(MyTracked); + +trait Db: salsa::DbWithJar {} + +#[salsa::tracked(jar = Jar)] +struct MyTracked { + field: u32, +} + +#[salsa::db(Jar)] +#[derive(Default)] +struct Database { + storage: salsa::Storage, +} + +impl salsa::Database for Database {} + +impl Db for Database {} + +#[test] +#[should_panic( + expected = "cannot create a tracked struct disambiguator outside of a tracked function" +)] +fn execute() { + let db = Database::default(); + MyTracked::new(&db, 0); +}