Skip to content

Commit 5642e45

Browse files
committed
const drop
1 parent 79879c4 commit 5642e45

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

library/core/src/array/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -996,16 +996,10 @@ impl<T> const Drop for Guard<'_, T> {
996996
#[inline]
997997
fn drop(&mut self) {
998998
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-
);
999+
// SAFETY: this slice will contain only initialized objects.
1000+
unsafe {
1001+
self.array_mut.get_unchecked_mut(..self.initialized).assume_init_drop();
1002+
}
10091003
}
10101004
}
10111005

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

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)