Fix empty borrow location warnings (#4593)
* Fix empty borrow location warnings * add error tests for rc variants * fix clippy
This commit is contained in:
1 parent
525c00ddac
commit
f95d8e4682
3 files changed
+146
-34
No files matched your search
@@ -19,6 +19,9 @@ use crate::{
|
|||||||
type RwLockStorageEntryRef = RwLockReadGuard<'static, StorageEntry<RwLockStorageEntryData>>;
|
type RwLockStorageEntryRef = RwLockReadGuard<'static, StorageEntry<RwLockStorageEntryData>>;
|
||||||
type RwLockStorageEntryMut = RwLockWriteGuard<'static, StorageEntry<RwLockStorageEntryData>>;
|
type RwLockStorageEntryMut = RwLockWriteGuard<'static, StorageEntry<RwLockStorageEntryData>>;
|
||||||
|
|
||||||
|
type AnyRef = MappedRwLockReadGuard<'static, Box<dyn Any + Send + Sync + 'static>>;
|
||||||
|
type AnyRefMut = MappedRwLockWriteGuard<'static, Box<dyn Any + Send + Sync + 'static>>;
|
||||||
|
|
||||||
pub(crate) enum RwLockStorageEntryData {
|
pub(crate) enum RwLockStorageEntryData {
|
||||||
Reference(GenerationalPointer<SyncStorage>),
|
Reference(GenerationalPointer<SyncStorage>),
|
||||||
Rc(RcStorageEntry<Box<dyn Any + Send + Sync>>),
|
Rc(RcStorageEntry<Box<dyn Any + Send + Sync>>),
|
||||||
@@ -59,13 +62,16 @@ pub struct SyncStorage {
|
|||||||
impl SyncStorage {
|
impl SyncStorage {
|
||||||
pub(crate) fn read(
|
pub(crate) fn read(
|
||||||
pointer: GenerationalPointer<Self>,
|
pointer: GenerationalPointer<Self>,
|
||||||
) -> BorrowResult<MappedRwLockReadGuard<'static, Box<dyn Any + Send + Sync + 'static>>> {
|
) -> BorrowResult<(AnyRef, GenerationalPointer<Self>)> {
|
||||||
Self::get_split_ref(pointer).map(|(_, guard)| {
|
Self::get_split_ref(pointer).map(|(resolved, guard)| {
|
||||||
RwLockReadGuard::map(guard, |data| match &data.data {
|
(
|
||||||
RwLockStorageEntryData::Data(data) => data,
|
RwLockReadGuard::map(guard, |data| match &data.data {
|
||||||
RwLockStorageEntryData::Rc(data) => &data.data,
|
RwLockStorageEntryData::Data(data) => data,
|
||||||
_ => unreachable!(),
|
RwLockStorageEntryData::Rc(data) => &data.data,
|
||||||
})
|
_ => unreachable!(),
|
||||||
|
}),
|
||||||
|
resolved,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,14 +105,16 @@ impl SyncStorage {
|
|||||||
|
|
||||||
pub(crate) fn write(
|
pub(crate) fn write(
|
||||||
pointer: GenerationalPointer<Self>,
|
pointer: GenerationalPointer<Self>,
|
||||||
) -> BorrowMutResult<MappedRwLockWriteGuard<'static, Box<dyn Any + Send + Sync + 'static>>>
|
) -> BorrowMutResult<(AnyRefMut, GenerationalPointer<Self>)> {
|
||||||
{
|
Self::get_split_mut(pointer).map(|(resolved, guard)| {
|
||||||
Self::get_split_mut(pointer).map(|(_, guard)| {
|
(
|
||||||
RwLockWriteGuard::map(guard, |data| match &mut data.data {
|
RwLockWriteGuard::map(guard, |data| match &mut data.data {
|
||||||
RwLockStorageEntryData::Data(data) => data,
|
RwLockStorageEntryData::Data(data) => data,
|
||||||
RwLockStorageEntryData::Rc(data) => &mut data.data,
|
RwLockStorageEntryData::Rc(data) => &mut data.data,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
}),
|
||||||
|
resolved,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,7 +285,7 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
|
|||||||
fn try_read(
|
fn try_read(
|
||||||
pointer: GenerationalPointer<Self>,
|
pointer: GenerationalPointer<Self>,
|
||||||
) -> Result<Self::Ref<'static, T>, error::BorrowError> {
|
) -> Result<Self::Ref<'static, T>, error::BorrowError> {
|
||||||
let read = Self::read(pointer)?;
|
let (read, pointer) = Self::read(pointer)?;
|
||||||
|
|
||||||
let read = MappedRwLockReadGuard::try_map(read, |any| {
|
let read = MappedRwLockReadGuard::try_map(read, |any| {
|
||||||
// Then try to downcast
|
// Then try to downcast
|
||||||
@@ -298,7 +306,7 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
|
|||||||
fn try_write(
|
fn try_write(
|
||||||
pointer: GenerationalPointer<Self>,
|
pointer: GenerationalPointer<Self>,
|
||||||
) -> Result<Self::Mut<'static, T>, error::BorrowMutError> {
|
) -> Result<Self::Mut<'static, T>, error::BorrowMutError> {
|
||||||
let write = Self::write(pointer)?;
|
let (write, pointer) = Self::write(pointer)?;
|
||||||
|
|
||||||
let write = MappedRwLockWriteGuard::try_map(write, |any| {
|
let write = MappedRwLockWriteGuard::try_map(write, |any| {
|
||||||
// Then try to downcast
|
// Then try to downcast
|
||||||
|
|||||||
@@ -13,8 +13,9 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
type RefCellStorageEntryRef = Ref<'static, StorageEntry<RefCellStorageEntryData>>;
|
type RefCellStorageEntryRef = Ref<'static, StorageEntry<RefCellStorageEntryData>>;
|
||||||
|
|
||||||
type RefCellStorageEntryMut = RefMut<'static, StorageEntry<RefCellStorageEntryData>>;
|
type RefCellStorageEntryMut = RefMut<'static, StorageEntry<RefCellStorageEntryData>>;
|
||||||
|
type AnyRef = Ref<'static, Box<dyn Any>>;
|
||||||
|
type AnyRefMut = RefMut<'static, Box<dyn Any>>;
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static UNSYNC_RUNTIME: RefCell<Vec<&'static UnsyncStorage>> = const { RefCell::new(Vec::new()) };
|
static UNSYNC_RUNTIME: RefCell<Vec<&'static UnsyncStorage>> = const { RefCell::new(Vec::new()) };
|
||||||
@@ -54,13 +55,16 @@ pub struct UnsyncStorage {
|
|||||||
impl UnsyncStorage {
|
impl UnsyncStorage {
|
||||||
pub(crate) fn read(
|
pub(crate) fn read(
|
||||||
pointer: GenerationalPointer<Self>,
|
pointer: GenerationalPointer<Self>,
|
||||||
) -> BorrowResult<Ref<'static, Box<dyn Any>>> {
|
) -> BorrowResult<(AnyRef, GenerationalPointer<Self>)> {
|
||||||
Self::get_split_ref(pointer).map(|(_, guard)| {
|
Self::get_split_ref(pointer).map(|(resolved, guard)| {
|
||||||
Ref::map(guard, |data| match &data.data {
|
(
|
||||||
RefCellStorageEntryData::Data(data) => data,
|
Ref::map(guard, |data| match &data.data {
|
||||||
RefCellStorageEntryData::Rc(data) => &data.data,
|
RefCellStorageEntryData::Data(data) => data,
|
||||||
_ => unreachable!(),
|
RefCellStorageEntryData::Rc(data) => &data.data,
|
||||||
})
|
_ => unreachable!(),
|
||||||
|
}),
|
||||||
|
resolved,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,13 +102,16 @@ impl UnsyncStorage {
|
|||||||
|
|
||||||
pub(crate) fn write(
|
pub(crate) fn write(
|
||||||
pointer: GenerationalPointer<Self>,
|
pointer: GenerationalPointer<Self>,
|
||||||
) -> BorrowMutResult<RefMut<'static, Box<dyn Any>>> {
|
) -> BorrowMutResult<(AnyRefMut, GenerationalPointer<Self>)> {
|
||||||
Self::get_split_mut(pointer).map(|(_, guard)| {
|
Self::get_split_mut(pointer).map(|(resolved, guard)| {
|
||||||
RefMut::map(guard, |data| match &mut data.data {
|
(
|
||||||
RefCellStorageEntryData::Data(data) => data,
|
RefMut::map(guard, |data| match &mut data.data {
|
||||||
RefCellStorageEntryData::Rc(data) => &mut data.data,
|
RefCellStorageEntryData::Data(data) => data,
|
||||||
_ => unreachable!(),
|
RefCellStorageEntryData::Rc(data) => &mut data.data,
|
||||||
})
|
_ => unreachable!(),
|
||||||
|
}),
|
||||||
|
resolved,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,7 +281,7 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
|
|||||||
fn try_read(
|
fn try_read(
|
||||||
pointer: GenerationalPointer<Self>,
|
pointer: GenerationalPointer<Self>,
|
||||||
) -> Result<Self::Ref<'static, T>, error::BorrowError> {
|
) -> Result<Self::Ref<'static, T>, error::BorrowError> {
|
||||||
let read = Self::read(pointer)?;
|
let (read, pointer) = Self::read(pointer)?;
|
||||||
|
|
||||||
let ref_ = Ref::filter_map(read, |any| {
|
let ref_ = Ref::filter_map(read, |any| {
|
||||||
// Then try to downcast
|
// Then try to downcast
|
||||||
@@ -295,7 +302,7 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
|
|||||||
fn try_write(
|
fn try_write(
|
||||||
pointer: GenerationalPointer<Self>,
|
pointer: GenerationalPointer<Self>,
|
||||||
) -> Result<Self::Mut<'static, T>, error::BorrowMutError> {
|
) -> Result<Self::Mut<'static, T>, error::BorrowMutError> {
|
||||||
let write = Self::write(pointer)?;
|
let (write, pointer) = Self::write(pointer)?;
|
||||||
|
|
||||||
let ref_mut = RefMut::filter_map(write, |any| {
|
let ref_mut = RefMut::filter_map(write, |any| {
|
||||||
// Then try to downcast
|
// Then try to downcast
|
||||||
|
|||||||
@@ -33,6 +33,18 @@ fn create_at_location<S: Storage<i32>>(
|
|||||||
(value, location)
|
(value, location)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
fn create_at_location_rc<S: Storage<i32>>(
|
||||||
|
owner: &Owner<S>,
|
||||||
|
) -> (
|
||||||
|
GenerationalBox<i32, S>,
|
||||||
|
&'static std::panic::Location<'static>,
|
||||||
|
) {
|
||||||
|
let location = std::panic::Location::caller();
|
||||||
|
let value = owner.insert_rc(1);
|
||||||
|
(value, location)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn read_while_writing_error() {
|
fn read_while_writing_error() {
|
||||||
fn read_while_writing_error_test<S: Storage<i32> + 'static>() {
|
fn read_while_writing_error_test<S: Storage<i32> + 'static>() {
|
||||||
@@ -54,6 +66,27 @@ fn read_while_writing_error() {
|
|||||||
read_while_writing_error_test::<UnsyncStorage>();
|
read_while_writing_error_test::<UnsyncStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn read_while_writing_error_rc() {
|
||||||
|
fn read_while_writing_error_test<S: Storage<i32> + 'static>() {
|
||||||
|
let owner = S::owner();
|
||||||
|
let value = owner.insert_rc(1);
|
||||||
|
|
||||||
|
let (write, location) = write_at_location(value);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
value.try_read().err(),
|
||||||
|
Some(BorrowError::AlreadyBorrowedMut(
|
||||||
|
AlreadyBorrowedMutError::new(location)
|
||||||
|
))
|
||||||
|
);
|
||||||
|
drop(write);
|
||||||
|
}
|
||||||
|
|
||||||
|
// For sync storage this will deadlock
|
||||||
|
read_while_writing_error_test::<UnsyncStorage>();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn read_after_dropped_error() {
|
fn read_after_dropped_error() {
|
||||||
fn read_after_dropped_error_test<S: Storage<i32> + 'static>() {
|
fn read_after_dropped_error_test<S: Storage<i32> + 'static>() {
|
||||||
@@ -70,6 +103,22 @@ fn read_after_dropped_error() {
|
|||||||
read_after_dropped_error_test::<SyncStorage>();
|
read_after_dropped_error_test::<SyncStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn read_after_dropped_error_rc() {
|
||||||
|
fn read_after_dropped_error_test<S: Storage<i32> + 'static>() {
|
||||||
|
let owner = S::owner();
|
||||||
|
let (value, location) = create_at_location_rc(&owner);
|
||||||
|
drop(owner);
|
||||||
|
assert_eq!(
|
||||||
|
value.try_read().err(),
|
||||||
|
Some(BorrowError::Dropped(ValueDroppedError::new(location)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
read_after_dropped_error_test::<UnsyncStorage>();
|
||||||
|
read_after_dropped_error_test::<SyncStorage>();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn write_while_writing_error() {
|
fn write_while_writing_error() {
|
||||||
fn write_while_writing_error_test<S: Storage<i32> + 'static>() {
|
fn write_while_writing_error_test<S: Storage<i32> + 'static>() {
|
||||||
@@ -96,6 +145,32 @@ fn write_while_writing_error() {
|
|||||||
write_while_writing_error_test::<UnsyncStorage>();
|
write_while_writing_error_test::<UnsyncStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn write_while_writing_error_rc() {
|
||||||
|
fn write_while_writing_error_test<S: Storage<i32> + 'static>() {
|
||||||
|
let owner = S::owner();
|
||||||
|
let value = owner.insert_rc(1);
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
let (write, location) = write_at_location(value);
|
||||||
|
|
||||||
|
let write_result = value.try_write();
|
||||||
|
assert!(write_result.is_err());
|
||||||
|
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
|
||||||
|
assert_eq!(
|
||||||
|
write_result.err(),
|
||||||
|
Some(BorrowMutError::AlreadyBorrowedMut(
|
||||||
|
AlreadyBorrowedMutError::new(location)
|
||||||
|
))
|
||||||
|
);
|
||||||
|
|
||||||
|
drop(write);
|
||||||
|
}
|
||||||
|
|
||||||
|
// For sync storage this will deadlock
|
||||||
|
write_while_writing_error_test::<UnsyncStorage>();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn write_while_reading_error() {
|
fn write_while_reading_error() {
|
||||||
fn write_while_reading_error_test<S: Storage<i32> + 'static>() {
|
fn write_while_reading_error_test<S: Storage<i32> + 'static>() {
|
||||||
@@ -117,3 +192,25 @@ fn write_while_reading_error() {
|
|||||||
// For sync storage this will deadlock
|
// For sync storage this will deadlock
|
||||||
write_while_reading_error_test::<UnsyncStorage>();
|
write_while_reading_error_test::<UnsyncStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn write_while_reading_error_rc() {
|
||||||
|
fn write_while_reading_error_test<S: Storage<i32> + 'static>() {
|
||||||
|
let owner = S::owner();
|
||||||
|
let value = owner.insert_rc(1);
|
||||||
|
|
||||||
|
let (read, location) = read_at_location(value);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
value.try_write().err(),
|
||||||
|
Some(BorrowMutError::AlreadyBorrowed(AlreadyBorrowedError::new(
|
||||||
|
vec![location]
|
||||||
|
)))
|
||||||
|
);
|
||||||
|
|
||||||
|
drop(read);
|
||||||
|
}
|
||||||
|
|
||||||
|
// For sync storage this will deadlock
|
||||||
|
write_while_reading_error_test::<UnsyncStorage>();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user