Skip to content

Commit 5f29c34

Browse files
committed
add failing test for interned input reuse
1 parent b54e365 commit 5f29c34

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/revision.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ impl Revision {
2525
#[inline]
2626
pub(crate) const fn start() -> Self {
2727
Self {
28-
generation: NonZeroUsize::new(START).unwrap(),
28+
// SAFETY: `START` is non-zero.
29+
generation: unsafe { NonZeroUsize::new_unchecked(START) },
2930
}
3031
}
3132

tests/interned-revisions.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,46 @@ fn test_reuse() {
214214
"WillExecute { database_key: function(Id(9)) }",
215215
]"#]]);
216216
}
217+
218+
#[test]
219+
fn test_reuse_interned_input() {
220+
// A query that creates an interned value.
221+
#[salsa::tracked]
222+
fn create_interned<'db>(db: &'db dyn Database, input: Input) -> Interned<'db> {
223+
Interned::new(db, input.field1(db))
224+
}
225+
226+
#[salsa::tracked]
227+
fn use_interned<'db>(db: &'db dyn Database, interned: Interned<'db>) -> usize {
228+
let field = interned.field1(db);
229+
field
230+
}
231+
232+
let mut db = common::EventLoggerDatabase::default();
233+
let input = Input::new(&db, 0);
234+
235+
// Create and use I0 in R0.
236+
let interned = create_interned(&db, input);
237+
let result = use_interned(&db, interned);
238+
assert_eq!(result, 0);
239+
240+
// Create and use I1 in a number of revisions, marking I0 as stale.
241+
input.set_field1(&mut db).to(1);
242+
for _ in 0..10 {
243+
let interned = create_interned(&db, input);
244+
let result = use_interned(&db, interned);
245+
assert_eq!(result, 1);
246+
247+
// Trigger a new revision.
248+
input.set_field1(&mut db).to(1);
249+
}
250+
251+
// Create I2, reusing the stale slot of I0.
252+
input.set_field1(&mut db).to(2);
253+
let interned = create_interned(&db, input);
254+
255+
// Use I2. The function should not be memoized with the value of I0, despite I2 and I0
256+
// sharing the same slot.
257+
let result = use_interned(&db, interned);
258+
assert_eq!(result, 2);
259+
}

0 commit comments

Comments
 (0)