salsa/tests/cycles.rs

53 lines
1.1 KiB
Rust
Raw Normal View History

#[salsa::database(GroupStruct)]
2018-10-01 10:47:24 +00:00
#[derive(Default)]
struct DatabaseImpl {
2018-10-07 11:08:22 +00:00
runtime: salsa::Runtime<DatabaseImpl>,
2018-10-01 10:47:24 +00:00
}
2018-10-05 08:54:51 +00:00
impl salsa::Database for DatabaseImpl {
2018-10-07 11:08:22 +00:00
fn salsa_runtime(&self) -> &salsa::Runtime<DatabaseImpl> {
2018-10-01 10:47:24 +00:00
&self.runtime
}
}
#[salsa::query_group(GroupStruct)]
trait Database: salsa::Database {
// `a` and `b` depend on each other and form a cycle
fn memoized_a(&self) -> ();
fn memoized_b(&self) -> ();
fn volatile_a(&self) -> ();
fn volatile_b(&self) -> ();
2018-10-01 10:47:24 +00:00
}
2018-10-19 01:26:48 +00:00
fn memoized_a(db: &impl Database) -> () {
db.memoized_b()
2018-10-01 10:47:24 +00:00
}
2018-10-19 01:26:48 +00:00
fn memoized_b(db: &impl Database) -> () {
db.memoized_a()
2018-10-01 10:47:24 +00:00
}
2018-10-19 01:26:48 +00:00
fn volatile_a(db: &impl Database) -> () {
db.salsa_runtime().report_untracked_read();
2018-10-19 01:26:48 +00:00
db.volatile_b()
2018-10-01 10:47:24 +00:00
}
2018-10-19 01:26:48 +00:00
fn volatile_b(db: &impl Database) -> () {
db.salsa_runtime().report_untracked_read();
2018-10-19 01:26:48 +00:00
db.volatile_a()
2018-10-01 10:47:24 +00:00
}
#[test]
#[should_panic(expected = "cycle detected")]
fn cycle_memoized() {
2018-10-05 08:54:51 +00:00
let query = DatabaseImpl::default();
2018-10-19 01:26:48 +00:00
query.memoized_a();
2018-10-01 10:47:24 +00:00
}
#[test]
#[should_panic(expected = "cycle detected")]
fn cycle_volatile() {
2018-10-05 08:54:51 +00:00
let query = DatabaseImpl::default();
2018-10-19 01:26:48 +00:00
query.volatile_a();
2018-10-01 10:47:24 +00:00
}