panic when reading fields of tracked structs from older revisions

This commit is contained in:
XFFXFF 2022-09-13 08:18:19 +08:00
parent 2ffe4a78a8
commit 4c4096e39b
2 changed files with 56 additions and 4 deletions

View file

@ -170,12 +170,18 @@ where
// during this revision or is otherwise stale.
return false;
}
QueryOrigin::BaseInput | QueryOrigin::Field => {
// BaseInput: This value was `set` by the mutator thread -- ie, it's a base input and it cannot be out of date.
// Field: This value is the value of a field of some tracked struct S. It is always updated whenever S is created.
// So if a query has access to S, then they will have an up-to-date value.
QueryOrigin::BaseInput => {
// This value was `set` by the mutator thread -- ie, it's a base input and it cannot be out of date.
return true;
}
QueryOrigin::Field => {
// This value is the value of a field of some tracked struct S.
// The fact that we are here means that we are accessing fields from old revisions, which is not allowed.
panic!(
"accessing fields of tracked struct {:?} from older revisions",
std::any::type_name::<<C as Configuration>::SalsaStruct>()
)
}
QueryOrigin::DerivedUntracked(_) => {
// Untracked inputs? Have to assume that it changed.
return false;

View file

@ -0,0 +1,46 @@
use test_log::test;
#[salsa::jar(db = Db)]
struct Jar(MyInput, MyTracked, tracked_fn);
trait Db: salsa::DbWithJar<Jar> {}
#[salsa::input(jar = Jar)]
struct MyInput {
field: u32,
}
#[salsa::tracked(jar = Jar)]
struct MyTracked {
field: u32,
}
#[salsa::tracked(jar = Jar)]
fn tracked_fn(db: &dyn Db, input: MyInput) -> MyTracked {
MyTracked::new(db, input.field(db) / 2)
}
#[salsa::db(Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
impl salsa::Database for Database {}
impl Db for Database {}
#[test]
#[should_panic(expected = "accessing fields of tracked struct")]
fn execute() {
let mut db = Database::default();
let input = MyInput::new(&mut db, 22);
let tracked = tracked_fn(&db, input);
// modify the input and change the revision
input.set_field(&mut db).to(24);
// panic when reading fields of tracked structs from older revisions
tracked.field(&db);
}