Rollup merge of #97675 - nvzqz:unsized-needs-drop, r=dtolnay
Make `std::mem::needs_drop` accept `?Sized` This change attempts to make `needs_drop` work with types like `[u8]` and `str`. This enables code in types like `Arc<T>` that was not possible before, such as https://github.com/rust-lang/rust/pull/97676.
This commit is contained in:
commit
cf68fd7e8d
6 changed files with 18 additions and 4 deletions
|
@ -567,7 +567,7 @@ pub mod intrinsics {
|
||||||
pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
|
pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
|
||||||
pub fn transmute<T, U>(e: T) -> U;
|
pub fn transmute<T, U>(e: T) -> U;
|
||||||
pub fn ctlz_nonzero<T>(x: T) -> T;
|
pub fn ctlz_nonzero<T>(x: T) -> T;
|
||||||
pub fn needs_drop<T>() -> bool;
|
pub fn needs_drop<T: ?::Sized>() -> bool;
|
||||||
pub fn bitreverse<T>(x: T) -> T;
|
pub fn bitreverse<T>(x: T) -> T;
|
||||||
pub fn bswap<T>(x: T) -> T;
|
pub fn bswap<T>(x: T) -> T;
|
||||||
pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
|
pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
|
||||||
|
|
|
@ -55,6 +55,11 @@ struct NoisyDrop {
|
||||||
inner: NoisyDropInner,
|
inner: NoisyDropInner,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct NoisyDropUnsized {
|
||||||
|
inner: NoisyDropInner,
|
||||||
|
text: str,
|
||||||
|
}
|
||||||
|
|
||||||
struct NoisyDropInner;
|
struct NoisyDropInner;
|
||||||
|
|
||||||
impl Drop for NoisyDrop {
|
impl Drop for NoisyDrop {
|
||||||
|
@ -170,7 +175,9 @@ fn main() {
|
||||||
assert_eq!(intrinsics::min_align_of_val(&a) as u8, intrinsics::min_align_of::<&str>() as u8);
|
assert_eq!(intrinsics::min_align_of_val(&a) as u8, intrinsics::min_align_of::<&str>() as u8);
|
||||||
|
|
||||||
assert!(!intrinsics::needs_drop::<u8>());
|
assert!(!intrinsics::needs_drop::<u8>());
|
||||||
|
assert!(!intrinsics::needs_drop::<[u8]>());
|
||||||
assert!(intrinsics::needs_drop::<NoisyDrop>());
|
assert!(intrinsics::needs_drop::<NoisyDrop>());
|
||||||
|
assert!(intrinsics::needs_drop::<NoisyDropUnsized>());
|
||||||
|
|
||||||
Unique {
|
Unique {
|
||||||
pointer: NonNull(1 as *mut &str),
|
pointer: NonNull(1 as *mut &str),
|
||||||
|
|
|
@ -514,7 +514,7 @@ pub mod intrinsics {
|
||||||
pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
|
pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
|
||||||
pub fn transmute<T, U>(e: T) -> U;
|
pub fn transmute<T, U>(e: T) -> U;
|
||||||
pub fn ctlz_nonzero<T>(x: T) -> T;
|
pub fn ctlz_nonzero<T>(x: T) -> T;
|
||||||
pub fn needs_drop<T>() -> bool;
|
pub fn needs_drop<T: ?::Sized>() -> bool;
|
||||||
pub fn bitreverse<T>(x: T) -> T;
|
pub fn bitreverse<T>(x: T) -> T;
|
||||||
pub fn bswap<T>(x: T) -> T;
|
pub fn bswap<T>(x: T) -> T;
|
||||||
pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
|
pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
|
||||||
|
|
|
@ -47,6 +47,11 @@ struct NoisyDrop {
|
||||||
inner: NoisyDropInner,
|
inner: NoisyDropInner,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct NoisyDropUnsized {
|
||||||
|
inner: NoisyDropInner,
|
||||||
|
text: str,
|
||||||
|
}
|
||||||
|
|
||||||
struct NoisyDropInner;
|
struct NoisyDropInner;
|
||||||
|
|
||||||
impl Drop for NoisyDrop {
|
impl Drop for NoisyDrop {
|
||||||
|
@ -184,7 +189,9 @@ fn main() {
|
||||||
assert_eq!(intrinsics::min_align_of_val(&a) as u8, intrinsics::min_align_of::<&str>() as u8);
|
assert_eq!(intrinsics::min_align_of_val(&a) as u8, intrinsics::min_align_of::<&str>() as u8);
|
||||||
|
|
||||||
assert!(!intrinsics::needs_drop::<u8>());
|
assert!(!intrinsics::needs_drop::<u8>());
|
||||||
|
assert!(!intrinsics::needs_drop::<[u8]>());
|
||||||
assert!(intrinsics::needs_drop::<NoisyDrop>());
|
assert!(intrinsics::needs_drop::<NoisyDrop>());
|
||||||
|
assert!(intrinsics::needs_drop::<NoisyDropUnsized>());
|
||||||
|
|
||||||
Unique {
|
Unique {
|
||||||
pointer: 0 as *const &str,
|
pointer: 0 as *const &str,
|
||||||
|
|
|
@ -1162,7 +1162,7 @@ extern "rust-intrinsic" {
|
||||||
///
|
///
|
||||||
/// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
|
/// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
|
||||||
#[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
|
#[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
|
||||||
pub fn needs_drop<T>() -> bool;
|
pub fn needs_drop<T: ?Sized>() -> bool;
|
||||||
|
|
||||||
/// Calculates the offset from a pointer.
|
/// Calculates the offset from a pointer.
|
||||||
///
|
///
|
||||||
|
|
|
@ -592,7 +592,7 @@ pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
|
||||||
#[stable(feature = "needs_drop", since = "1.21.0")]
|
#[stable(feature = "needs_drop", since = "1.21.0")]
|
||||||
#[rustc_const_stable(feature = "const_mem_needs_drop", since = "1.36.0")]
|
#[rustc_const_stable(feature = "const_mem_needs_drop", since = "1.36.0")]
|
||||||
#[rustc_diagnostic_item = "needs_drop"]
|
#[rustc_diagnostic_item = "needs_drop"]
|
||||||
pub const fn needs_drop<T>() -> bool {
|
pub const fn needs_drop<T: ?Sized>() -> bool {
|
||||||
intrinsics::needs_drop::<T>()
|
intrinsics::needs_drop::<T>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue