Skip to content

Commit a03d08a

Browse files
committed
Unstably constify ptr::drop_in_place and related methods
1 parent a1208bf commit a03d08a

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

library/core/src/mem/maybe_uninit.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::any::type_name;
2+
use crate::marker::Destruct;
23
use crate::mem::ManuallyDrop;
34
use crate::{fmt, intrinsics, ptr, slice};
45

@@ -714,7 +715,11 @@ impl<T> MaybeUninit<T> {
714715
///
715716
/// [`assume_init`]: MaybeUninit::assume_init
716717
#[stable(feature = "maybe_uninit_extra", since = "1.60.0")]
717-
pub unsafe fn assume_init_drop(&mut self) {
718+
#[rustc_const_unstable(feature = "const_drop_in_place", issue = "109342")]
719+
pub const unsafe fn assume_init_drop(&mut self)
720+
where
721+
T: [const] Destruct,
722+
{
718723
// SAFETY: the caller must guarantee that `self` is initialized and
719724
// satisfies all invariants of `T`.
720725
// Dropping the value in place is safe if that is the case.
@@ -1390,7 +1395,11 @@ impl<T> [MaybeUninit<T>] {
13901395
/// behaviour.
13911396
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
13921397
#[inline(always)]
1393-
pub unsafe fn assume_init_drop(&mut self) {
1398+
#[rustc_const_unstable(feature = "const_drop_in_place", issue = "109342")]
1399+
pub const unsafe fn assume_init_drop(&mut self)
1400+
where
1401+
T: [const] Destruct,
1402+
{
13941403
if !self.is_empty() {
13951404
// SAFETY: the caller must guarantee that every element of `self`
13961405
// is initialized and satisfies all invariants of `T`.

library/core/src/ptr/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@
403403

404404
use crate::cmp::Ordering;
405405
use crate::intrinsics::const_eval_select;
406-
use crate::marker::{FnPtr, PointeeSized};
406+
use crate::marker::{Destruct, FnPtr, PointeeSized};
407407
use crate::mem::{self, MaybeUninit, SizedTypeProperties};
408408
use crate::num::NonZero;
409409
use crate::{fmt, hash, intrinsics, ub_checks};
@@ -801,7 +801,11 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
801801
#[lang = "drop_in_place"]
802802
#[allow(unconditional_recursion)]
803803
#[rustc_diagnostic_item = "ptr_drop_in_place"]
804-
pub unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T) {
804+
#[rustc_const_unstable(feature = "const_drop_in_place", issue = "109342")]
805+
pub const unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T)
806+
where
807+
T: [const] Destruct,
808+
{
805809
// Code here does not matter - this is replaced by the
806810
// real drop glue by the compiler.
807811

library/core/src/ptr/mut_ptr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22
use crate::cmp::Ordering::{Equal, Greater, Less};
33
use crate::intrinsics::const_eval_select;
4-
use crate::marker::PointeeSized;
4+
use crate::marker::{Destruct, PointeeSized};
55
use crate::mem::{self, SizedTypeProperties};
66
use crate::slice::{self, SliceIndex};
77

@@ -1437,8 +1437,12 @@ impl<T: PointeeSized> *mut T {
14371437
///
14381438
/// [`ptr::drop_in_place`]: crate::ptr::drop_in_place()
14391439
#[stable(feature = "pointer_methods", since = "1.26.0")]
1440+
#[rustc_const_unstable(feature = "const_drop_in_place", issue = "109342")]
14401441
#[inline(always)]
1441-
pub unsafe fn drop_in_place(self) {
1442+
pub const unsafe fn drop_in_place(self)
1443+
where
1444+
T: [const] Destruct,
1445+
{
14421446
// SAFETY: the caller must uphold the safety contract for `drop_in_place`.
14431447
unsafe { drop_in_place(self) }
14441448
}

library/core/src/ptr/non_null.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::cmp::Ordering;
2-
use crate::marker::{PointeeSized, Unsize};
2+
use crate::marker::{Destruct, PointeeSized, Unsize};
33
use crate::mem::{MaybeUninit, SizedTypeProperties};
44
use crate::num::NonZero;
55
use crate::ops::{CoerceUnsized, DispatchFromDyn};
@@ -1118,7 +1118,11 @@ impl<T: PointeeSized> NonNull<T> {
11181118
/// [`ptr::drop_in_place`]: crate::ptr::drop_in_place()
11191119
#[inline(always)]
11201120
#[stable(feature = "non_null_convenience", since = "1.80.0")]
1121-
pub unsafe fn drop_in_place(self) {
1121+
#[rustc_const_unstable(feature = "const_drop_in_place", issue = "109342")]
1122+
pub const unsafe fn drop_in_place(self)
1123+
where
1124+
T: [const] Destruct,
1125+
{
11221126
// SAFETY: the caller must uphold the safety contract for `drop_in_place`.
11231127
unsafe { ptr::drop_in_place(self.as_ptr()) }
11241128
}

0 commit comments

Comments
 (0)