Improve assert message when creating a tracked struct outside of a tracked function

This commit is contained in:
DropDemBits 2023-06-13 16:34:48 -04:00
parent d4a94fbf07
commit 22646a8fb4
No known key found for this signature in database
GPG key ID: D550F8DFBB392533
2 changed files with 35 additions and 1 deletions

View file

@ -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);

View file

@ -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<Jar> {}
#[salsa::tracked(jar = Jar)]
struct MyTracked {
field: u32,
}
#[salsa::db(Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
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);
}