diff --git a/library/alloc/src/boxed/convert.rs b/library/alloc/src/boxed/convert.rs index 8062658020239..11285bb51898a 100644 --- a/library/alloc/src/boxed/convert.rs +++ b/library/alloc/src/boxed/convert.rs @@ -392,7 +392,9 @@ impl Box { #[inline] #[unstable(feature = "downcast_unchecked", issue = "90850")] pub unsafe fn downcast_unchecked(self) -> Box { - debug_assert!(self.is::()); + if core::ub_checks::check_library_ub() { + assert!(self.is::()); + } unsafe { let (raw, alloc): (*mut dyn Any, _) = Box::into_raw_with_allocator(self); Box::from_raw_in(raw as *mut T, alloc) @@ -451,7 +453,9 @@ impl Box { #[inline] #[unstable(feature = "downcast_unchecked", issue = "90850")] pub unsafe fn downcast_unchecked(self) -> Box { - debug_assert!(self.is::()); + if core::ub_checks::check_library_ub() { + assert!(self.is::()); + } unsafe { let (raw, alloc): (*mut (dyn Any + Send), _) = Box::into_raw_with_allocator(self); Box::from_raw_in(raw as *mut T, alloc) @@ -510,7 +514,9 @@ impl Box { #[inline] #[unstable(feature = "downcast_unchecked", issue = "90850")] pub unsafe fn downcast_unchecked(self) -> Box { - debug_assert!(self.is::()); + if core::ub_checks::check_library_ub() { + assert!(self.is::()); + } unsafe { let (raw, alloc): (*mut (dyn Any + Send + Sync), _) = Box::into_raw_with_allocator(self); diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 529b583cdd2bc..2b3d9a3158c2f 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -2042,6 +2042,9 @@ impl Rc { #[inline] #[unstable(feature = "downcast_unchecked", issue = "90850")] pub unsafe fn downcast_unchecked(self) -> Rc { + if core::ub_checks::check_library_ub() { + assert!((*self).is::()); + } unsafe { let (ptr, alloc) = Rc::into_inner_with_allocator(self); Rc::from_inner_in(ptr.cast(), alloc) diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 29caa7bc5393c..3e494676df7e5 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -2754,6 +2754,9 @@ impl Arc { where T: Any + Send + Sync, { + if core::ub_checks::check_library_ub() { + assert!((*self).is::()); + } unsafe { let (ptr, alloc) = Arc::into_inner_with_allocator(self); Arc::from_inner_in(ptr.cast(), alloc) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index e7d9763d46ef5..f0ba24d30846c 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -292,7 +292,9 @@ impl dyn Any { #[unstable(feature = "downcast_unchecked", issue = "90850")] #[inline] pub unsafe fn downcast_ref_unchecked(&self) -> &T { - debug_assert!(self.is::()); + if core::ub_checks::check_library_ub() { + assert!(self.is::()); + } // SAFETY: caller guarantees that T is the correct type unsafe { &*(self as *const dyn Any as *const T) } } @@ -322,7 +324,9 @@ impl dyn Any { #[unstable(feature = "downcast_unchecked", issue = "90850")] #[inline] pub unsafe fn downcast_mut_unchecked(&mut self) -> &mut T { - debug_assert!(self.is::()); + if core::ub_checks::check_library_ub() { + assert!(self.is::()); + } // SAFETY: caller guarantees that T is the correct type unsafe { &mut *(self as *mut dyn Any as *mut T) } }