salsa/salsa-2022-tests/tests/debug.rs
Niko Matsakis 389aa66bcf print all fields in debug() but ignore deps
In a previous PR we added the `include_all_fields`
parameter to `DebugWithDb` to allow it to
not create spurious dependencies.

This PR takes a different approach: we simply
ignore the dependencies created during debug
operations. This is risky as it can create
incorrect dependencies, but it is way more
convenient and seems like what users probably
want.

It also means that `DebugWithDb` has a simpler
signature that matches the `Debug` trait again,
which seems good to me.
2024-04-03 05:59:11 -04:00

81 lines
1.9 KiB
Rust

//! Test that `DeriveWithDb` is correctly derived.
use expect_test::expect;
use salsa::DebugWithDb;
#[salsa::jar(db = Db)]
struct Jar(MyInput, ComplexStruct, leak_debug_string);
trait Db: salsa::DbWithJar<Jar> {}
#[salsa::input]
struct MyInput {
field: u32,
}
#[derive(Debug, Eq, PartialEq, Clone)]
struct NotSalsa {
field: String,
}
#[salsa::input]
struct ComplexStruct {
my_input: MyInput,
not_salsa: NotSalsa,
}
#[salsa::db(Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
impl salsa::Database for Database {}
impl Db for Database {}
#[test]
fn input() {
let db = Database::default();
let input = MyInput::new(&db, 22);
let not_salsa = NotSalsa {
field: "it's salsa time".to_string(),
};
let complex_struct = ComplexStruct::new(&db, input, not_salsa);
// debug includes all fields
let actual = format!("{:?}", complex_struct.debug(&db));
let expected = expect![[
r#"ComplexStruct { [salsa id]: 0, my_input: MyInput { [salsa id]: 0, field: 22 }, not_salsa: NotSalsa { field: "it's salsa time" } }"#
]];
expected.assert_eq(&actual);
}
#[salsa::tracked]
fn leak_debug_string(db: &dyn Db, input: MyInput) -> String {
format!("{:?}", input.debug(db))
}
/// Test that field reads that occur as part of `Debug` are not tracked.
/// Intentionally leaks the debug string.
/// Don't try this at home, kids.
#[test]
fn untracked_dependencies() {
let mut db = Database::default();
let input = MyInput::new(&db, 22);
let s = leak_debug_string(&db, input);
expect![[r#"
"MyInput { [salsa id]: 0, field: 22 }"
"#]]
.assert_debug_eq(&s);
input.set_field(&mut db).to(23);
// check that we reuse the cached result for debug string
// even though the dependency changed.
let s = leak_debug_string(&db, input);
assert!(s.contains(", field: 22 }"));
}