Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions library/alloc/src/boxed/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ impl<A: Allocator> Box<dyn Any, A> {
#[inline]
#[unstable(feature = "downcast_unchecked", issue = "90850")]
pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
debug_assert!(self.is::<T>());
if core::ub_checks::check_library_ub() {
assert!(self.is::<T>());
}
unsafe {
let (raw, alloc): (*mut dyn Any, _) = Box::into_raw_with_allocator(self);
Box::from_raw_in(raw as *mut T, alloc)
Expand Down Expand Up @@ -451,7 +453,9 @@ impl<A: Allocator> Box<dyn Any + Send, A> {
#[inline]
#[unstable(feature = "downcast_unchecked", issue = "90850")]
pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
debug_assert!(self.is::<T>());
if core::ub_checks::check_library_ub() {
assert!(self.is::<T>());
}
unsafe {
let (raw, alloc): (*mut (dyn Any + Send), _) = Box::into_raw_with_allocator(self);
Box::from_raw_in(raw as *mut T, alloc)
Expand Down Expand Up @@ -510,7 +514,9 @@ impl<A: Allocator> Box<dyn Any + Send + Sync, A> {
#[inline]
#[unstable(feature = "downcast_unchecked", issue = "90850")]
pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
debug_assert!(self.is::<T>());
if core::ub_checks::check_library_ub() {
assert!(self.is::<T>());
}
unsafe {
let (raw, alloc): (*mut (dyn Any + Send + Sync), _) =
Box::into_raw_with_allocator(self);
Expand Down
3 changes: 3 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,9 @@ impl<A: Allocator> Rc<dyn Any, A> {
#[inline]
#[unstable(feature = "downcast_unchecked", issue = "90850")]
pub unsafe fn downcast_unchecked<T: Any>(self) -> Rc<T, A> {
if core::ub_checks::check_library_ub() {
assert!((*self).is::<T>());
}
unsafe {
let (ptr, alloc) = Rc::into_inner_with_allocator(self);
Rc::from_inner_in(ptr.cast(), alloc)
Expand Down
3 changes: 3 additions & 0 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2754,6 +2754,9 @@ impl<A: Allocator> Arc<dyn Any + Send + Sync, A> {
where
T: Any + Send + Sync,
{
if core::ub_checks::check_library_ub() {
assert!((*self).is::<T>());
}
unsafe {
let (ptr, alloc) = Arc::into_inner_with_allocator(self);
Arc::from_inner_in(ptr.cast(), alloc)
Expand Down
8 changes: 6 additions & 2 deletions library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ impl dyn Any {
#[unstable(feature = "downcast_unchecked", issue = "90850")]
#[inline]
pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
debug_assert!(self.is::<T>());
if core::ub_checks::check_library_ub() {
assert!(self.is::<T>());
}
// SAFETY: caller guarantees that T is the correct type
unsafe { &*(self as *const dyn Any as *const T) }
}
Expand Down Expand Up @@ -322,7 +324,9 @@ impl dyn Any {
#[unstable(feature = "downcast_unchecked", issue = "90850")]
#[inline]
pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
debug_assert!(self.is::<T>());
if core::ub_checks::check_library_ub() {
assert!(self.is::<T>());
}
// SAFETY: caller guarantees that T is the correct type
unsafe { &mut *(self as *mut dyn Any as *mut T) }
}
Expand Down
Loading