Skip to content

Commit 044511d

Browse files
committed
const drop
1 parent 79879c4 commit 044511d

File tree

6 files changed

+32
-27
lines changed

6 files changed

+32
-27
lines changed

library/core/src/array/mod.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub fn repeat<T: Clone, const N: usize>(val: T) -> [T; N] {
105105
#[inline]
106106
#[stable(feature = "array_from_fn", since = "1.63.0")]
107107
#[rustc_const_unstable(feature = "const_array", issue = "none")]
108-
pub const fn from_fn<T, const N: usize, F>(f: F) -> [T; N]
108+
pub const fn from_fn<T: [const] Destruct, const N: usize, F>(f: F) -> [T; N]
109109
where
110110
F: [const] FnMut(usize) -> T + [const] Destruct,
111111
{
@@ -148,6 +148,7 @@ pub const fn try_from_fn<R, const N: usize, F>(cb: F) -> ChangeOutputType<R, [R:
148148
where
149149
F: [const] FnMut(usize) -> R + [const] Destruct,
150150
R: [const] Try<Residual: Residual<[R::Output; N]>>,
151+
R::Output: [const] Destruct,
151152
<R::Residual as Residual<[R::Output; N]>>::TryType: [const] Try,
152153
{
153154
let mut array = [const { MaybeUninit::uninit() }; N];
@@ -548,6 +549,7 @@ impl<T, const N: usize> [T; N] {
548549
pub const fn map<F, U>(self, f: F) -> [U; N]
549550
where
550551
F: [const] FnMut(T) -> U + [const] Destruct,
552+
U: [const] Destruct,
551553
{
552554
self.try_map(NeverShortCircuit::wrap_mut_1(f)).0
553555
}
@@ -588,6 +590,7 @@ impl<T, const N: usize> [T; N] {
588590
where
589591
F: [const] FnMut(T) -> R + [const] Destruct,
590592
R: [const] Try<Residual: Residual<[R::Output; N]>>,
593+
R::Output: [const] Destruct,
591594
<R::Residual as Residual<[R::Output; N]>>::TryType: [const] Try,
592595
{
593596
#[rustc_const_unstable(feature = "array_try_map", issue = "79711")]
@@ -624,13 +627,11 @@ impl<T, const N: usize> [T; N] {
624627
for Guardian<'_, T, U, N, F>
625628
{
626629
fn drop(&mut self) {
627-
if mem::needs_drop::<T>() {
628-
let mut n = self.moved;
629-
while n != N {
630-
// SAFETY: moved must always be < N
631-
unsafe { self.array.as_mut_ptr().add(n).read() };
632-
n += 1;
633-
}
630+
let mut n = self.moved;
631+
while n != N {
632+
// SAFETY: moved must always be < N
633+
unsafe { self.array.as_mut_ptr().add(n).drop_in_place() };
634+
n += 1;
634635
}
635636
}
636637
}
@@ -938,6 +939,7 @@ const fn try_from_fn_erased<T, R, F>(
938939
mut generator: F,
939940
) -> ControlFlow<R::Residual>
940941
where
942+
T: [const] Destruct,
941943
R: [const] Try<Output = T>,
942944
F: [const] FnMut(usize) -> R + [const] Destruct,
943945
{
@@ -992,20 +994,14 @@ impl<T> Guard<'_, T> {
992994
}
993995

994996
#[rustc_const_unstable(feature = "array_try_from_fn", issue = "89379")]
995-
impl<T> const Drop for Guard<'_, T> {
997+
impl<T: [const] Destruct> const Drop for Guard<'_, T> {
996998
#[inline]
997999
fn drop(&mut self) {
9981000
debug_assert!(self.initialized <= self.array_mut.len());
999-
crate::intrinsics::const_eval_select!(
1000-
@capture [T] { x: &mut Guard<'_, T> = self } -> ():
1001-
if const {}
1002-
else {
1003-
// SAFETY: this slice will contain only initialized objects.
1004-
unsafe {
1005-
x.array_mut.get_unchecked_mut(..x.initialized).assume_init_drop();
1006-
}
1007-
}
1008-
);
1001+
// SAFETY: this slice will contain only initialized objects.
1002+
unsafe {
1003+
self.array_mut.get_unchecked_mut(..self.initialized).assume_init_drop();
1004+
}
10091005
}
10101006
}
10111007

library/core/src/mem/maybe_uninit.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,8 +1389,12 @@ impl<T> [MaybeUninit<T>] {
13891389
/// non-null. Dropping such a `Vec<T>` however will cause undefined
13901390
/// behaviour.
13911391
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
1392+
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
13921393
#[inline(always)]
1393-
pub unsafe fn assume_init_drop(&mut self) {
1394+
pub const unsafe fn assume_init_drop(&mut self)
1395+
where
1396+
T: [const] crate::marker::Destruct,
1397+
{
13941398
if !self.is_empty() {
13951399
// SAFETY: the caller must guarantee that every element of `self`
13961400
// is initialized and satisfies all invariants of `T`.

library/core/src/ptr/mod.rs

Lines changed: 3 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,8 @@ 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 = "none")]
805+
pub const unsafe fn drop_in_place<T: PointeeSized + [const] Destruct>(to_drop: *mut T) {
805806
// Code here does not matter - this is replaced by the
806807
// real drop glue by the compiler.
807808

library/core/src/ptr/mut_ptr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,12 @@ impl<T: PointeeSized> *mut T {
13901390
///
13911391
/// [`ptr::drop_in_place`]: crate::ptr::drop_in_place()
13921392
#[stable(feature = "pointer_methods", since = "1.26.0")]
1393+
#[rustc_const_unstable(feature = "const_drop_in_place", issue = "none")]
13931394
#[inline(always)]
1394-
pub unsafe fn drop_in_place(self) {
1395+
pub const unsafe fn drop_in_place(self)
1396+
where
1397+
T: [const] Destruct,
1398+
{
13951399
// SAFETY: the caller must uphold the safety contract for `drop_in_place`.
13961400
unsafe { drop_in_place(self) }
13971401
}

src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: trying to retag from <TAG> for Unique permission at ALLOC[0x0], but that tag only grants SharedReadOnly permission for this location
22
--> RUSTLIB/core/src/ptr/mod.rs:LL:CC
33
|
4-
LL | pub unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T) {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this error occurs as part of retag at ALLOC[0x0..0x1]
4+
LL | pub const unsafe fn drop_in_place<T: PointeeSized + [const] Destruct>(to_drop: *mut T) {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this error occurs as part of retag at ALLOC[0x0..0x1]
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: constructing invalid value: encountered an unaligned reference (required ALIGN byte alignment but found ALIGN)
22
--> RUSTLIB/core/src/ptr/mod.rs:LL:CC
33
|
4-
LL | pub unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T) {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
4+
LL | pub const unsafe fn drop_in_place<T: PointeeSized + [const] Destruct>(to_drop: *mut T) {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

0 commit comments

Comments
 (0)