Skip to content

Commit b98434a

Browse files
committed
Bump revision in db.synthetic_write
1 parent e4ce917 commit b98434a

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/database.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ pub trait Database: DatabaseGen {
2121
/// will block until that snapshot is dropped -- if that snapshot
2222
/// is owned by the current thread, this could trigger deadlock.
2323
fn synthetic_write(&mut self, durability: Durability) {
24-
self.runtime_mut().report_tracked_write(durability);
24+
let runtime = self.runtime_mut();
25+
runtime.new_revision();
26+
runtime.report_tracked_write(durability);
2527
}
2628

2729
/// Reports that the query depends on some state unknown to salsa.

tests/synthetic_write.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//! Test that a constant `tracked` fn (has no inputs)
2+
//! compiles and executes successfully.
3+
#![allow(warnings)]
4+
5+
mod common;
6+
7+
use common::{HasLogger, Logger};
8+
use expect_test::expect;
9+
use salsa::{Database as _, Durability, Event, EventKind};
10+
11+
#[salsa::db]
12+
trait Db: salsa::Database + HasLogger {}
13+
14+
#[salsa::input]
15+
struct MyInput {
16+
field: u32,
17+
}
18+
19+
#[salsa::tracked]
20+
fn tracked_fn(db: &dyn Db, input: MyInput) -> u32 {
21+
input.field(db) * 2
22+
}
23+
24+
#[salsa::db]
25+
#[derive(Default)]
26+
struct Database {
27+
storage: salsa::Storage<Self>,
28+
logger: Logger,
29+
}
30+
31+
#[salsa::db]
32+
impl salsa::Database for Database {
33+
fn salsa_event(&self, event: Event) {
34+
if let EventKind::WillExecute { .. } | EventKind::DidValidateMemoizedValue { .. } =
35+
event.kind
36+
{
37+
self.push_log(format!("{:?}", event.kind));
38+
}
39+
}
40+
}
41+
42+
impl HasLogger for Database {
43+
fn logger(&self) -> &Logger {
44+
&self.logger
45+
}
46+
}
47+
48+
#[salsa::db]
49+
impl Db for Database {}
50+
51+
#[test]
52+
fn execute() {
53+
let mut db = Database::default();
54+
55+
let input = MyInput::new(&db, 22);
56+
assert_eq!(tracked_fn(&db, input), 44);
57+
58+
db.assert_logs(expect![[r#"
59+
[
60+
"WillExecute { database_key: tracked_fn(0) }",
61+
]"#]]);
62+
63+
// Bumps the revision
64+
db.synthetic_write(Durability::LOW);
65+
66+
// Query should re-run
67+
assert_eq!(tracked_fn(&db, input), 44);
68+
69+
db.assert_logs(expect![[r#"
70+
[
71+
"DidValidateMemoizedValue { database_key: tracked_fn(0) }",
72+
]"#]]);
73+
}

0 commit comments

Comments
 (0)