Merge pull request #532 from salsa-rs/fix-guard-assertion

Fix assertion for same DB in `DbGuard`
This commit is contained in:
Niko Matsakis 2024-07-28 09:56:42 +00:00 committed by GitHub
commit b0ee16211e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -83,12 +83,16 @@ impl LocalState {
impl<'s> DbGuard<'s> {
fn new(state: &'s LocalState, db: &dyn Database) -> Self {
if let Some(current_db) = state.database.get() {
let new_db = NonNull::from(db);
// Already attached? Assert that the database has not changed.
assert_eq!(
current_db,
NonNull::from(db),
"cannot change database mid-query",
);
// NOTE: It's important to use `addr_eq` here because `NonNull::eq` not only compares the address but also the type's metadata.
if !std::ptr::addr_eq(current_db.as_ptr(), new_db.as_ptr()) {
panic!(
"Cannot change database mid-query. current: {current_db:?}, new: {new_db:?}",
);
}
Self { state: None }
} else {
// Otherwise, set the database.