Skip to content

Commit 5eeb8f9

Browse files
committed
Auto merge of rust-lang#94901 - fee1-dead:destructable, r=oli-obk
Rename `~const Drop` to `~const Destruct` r? `@oli-obk` Completely switching to `~const Destructible` would be rather complicated, so it seems best to add it for now and wait for it to be backported to beta in the next release. The rationale is to prevent complications such as rust-lang#92149 and rust-lang#94803 by introducing an entirely new trait. And `~const Destructible` reads a bit better than `~const Drop`. Name Bikesheddable.
2 parents 4506607 + fdf44c1 commit 5eeb8f9

File tree

9 files changed

+119
-56
lines changed

9 files changed

+119
-56
lines changed

alloc/src/alloc.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use core::ptr::{self, NonNull};
1414
#[doc(inline)]
1515
pub use core::alloc::*;
1616

17+
use core::marker::Destruct;
18+
1719
#[cfg(test)]
1820
mod tests;
1921

@@ -324,12 +326,16 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
324326
#[cfg_attr(not(test), lang = "box_free")]
325327
#[inline]
326328
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
329+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
327330
// This signature has to be the same as `Box`, otherwise an ICE will happen.
328331
// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
329332
// well.
330333
// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
331334
// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
332-
pub(crate) const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Drop>(
335+
pub(crate) const unsafe fn box_free<
336+
T: ?Sized,
337+
A: ~const Allocator + ~const Drop + ~const Destruct,
338+
>(
333339
ptr: Unique<T>,
334340
alloc: A,
335341
) {

alloc/src/borrow.rs

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
331331

332332
#[stable(feature = "rust1", since = "1.0.0")]
333333
#[rustc_const_unstable(feature = "const_deref", issue = "88955")]
334+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
334335
impl<B: ?Sized + ToOwned> const Deref for Cow<'_, B>
335336
where
336337
B::Owned: ~const Borrow<B>,

alloc/src/boxed.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ use core::hash::{Hash, Hasher};
143143
#[cfg(not(no_global_oom_handling))]
144144
use core::iter::FromIterator;
145145
use core::iter::{FusedIterator, Iterator};
146-
use core::marker::{Unpin, Unsize};
146+
use core::marker::{Destruct, Unpin, Unsize};
147147
use core::mem;
148148
use core::ops::{
149149
CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver,
@@ -349,9 +349,10 @@ impl<T, A: Allocator> Box<T, A> {
349349
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
350350
#[must_use]
351351
#[inline]
352+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
352353
pub const fn new_in(x: T, alloc: A) -> Self
353354
where
354-
A: ~const Allocator + ~const Drop,
355+
A: ~const Allocator + ~const Drop + ~const Destruct,
355356
{
356357
let mut boxed = Self::new_uninit_in(alloc);
357358
unsafe {
@@ -378,10 +379,11 @@ impl<T, A: Allocator> Box<T, A> {
378379
#[unstable(feature = "allocator_api", issue = "32838")]
379380
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
380381
#[inline]
382+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
381383
pub const fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
382384
where
383-
T: ~const Drop,
384-
A: ~const Allocator + ~const Drop,
385+
T: ~const Drop + ~const Destruct,
386+
A: ~const Allocator + ~const Drop + ~const Destruct,
385387
{
386388
let mut boxed = Self::try_new_uninit_in(alloc)?;
387389
unsafe {
@@ -415,9 +417,10 @@ impl<T, A: Allocator> Box<T, A> {
415417
#[cfg(not(no_global_oom_handling))]
416418
#[must_use]
417419
// #[unstable(feature = "new_uninit", issue = "63291")]
420+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
418421
pub const fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
419422
where
420-
A: ~const Allocator + ~const Drop,
423+
A: ~const Allocator + ~const Drop + ~const Destruct,
421424
{
422425
let layout = Layout::new::<mem::MaybeUninit<T>>();
423426
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -453,9 +456,10 @@ impl<T, A: Allocator> Box<T, A> {
453456
#[unstable(feature = "allocator_api", issue = "32838")]
454457
// #[unstable(feature = "new_uninit", issue = "63291")]
455458
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
459+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
456460
pub const fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
457461
where
458-
A: ~const Allocator + ~const Drop,
462+
A: ~const Allocator + ~const Drop + ~const Destruct,
459463
{
460464
let layout = Layout::new::<mem::MaybeUninit<T>>();
461465
let ptr = alloc.allocate(layout)?.cast();
@@ -487,9 +491,10 @@ impl<T, A: Allocator> Box<T, A> {
487491
#[cfg(not(no_global_oom_handling))]
488492
// #[unstable(feature = "new_uninit", issue = "63291")]
489493
#[must_use]
494+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
490495
pub const fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
491496
where
492-
A: ~const Allocator + ~const Drop,
497+
A: ~const Allocator + ~const Drop + ~const Destruct,
493498
{
494499
let layout = Layout::new::<mem::MaybeUninit<T>>();
495500
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -525,9 +530,10 @@ impl<T, A: Allocator> Box<T, A> {
525530
#[unstable(feature = "allocator_api", issue = "32838")]
526531
// #[unstable(feature = "new_uninit", issue = "63291")]
527532
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
533+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
528534
pub const fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
529535
where
530-
A: ~const Allocator + ~const Drop,
536+
A: ~const Allocator + ~const Drop + ~const Destruct,
531537
{
532538
let layout = Layout::new::<mem::MaybeUninit<T>>();
533539
let ptr = alloc.allocate_zeroed(layout)?.cast();
@@ -541,9 +547,10 @@ impl<T, A: Allocator> Box<T, A> {
541547
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
542548
#[must_use]
543549
#[inline(always)]
550+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
544551
pub const fn pin_in(x: T, alloc: A) -> Pin<Self>
545552
where
546-
A: 'static + ~const Allocator + ~const Drop,
553+
A: 'static + ~const Allocator + ~const Drop + ~const Destruct,
547554
{
548555
Self::into_pin(Self::new_in(x, alloc))
549556
}
@@ -572,9 +579,10 @@ impl<T, A: Allocator> Box<T, A> {
572579
#[unstable(feature = "box_into_inner", issue = "80437")]
573580
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
574581
#[inline]
582+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
575583
pub const fn into_inner(boxed: Self) -> T
576584
where
577-
Self: ~const Drop,
585+
Self: ~const Drop + ~const Destruct,
578586
{
579587
*boxed
580588
}

core/src/bool.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! impl bool {}
22
3+
use crate::marker::Destruct;
4+
35
#[lang = "bool"]
46
impl bool {
57
/// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
@@ -16,9 +18,10 @@ impl bool {
1618
#[unstable(feature = "bool_to_option", issue = "80967")]
1719
#[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
1820
#[inline]
21+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
1922
pub const fn then_some<T>(self, t: T) -> Option<T>
2023
where
21-
T: ~const Drop,
24+
T: ~const Drop + ~const Destruct,
2225
{
2326
if self { Some(t) } else { None }
2427
}
@@ -35,10 +38,11 @@ impl bool {
3538
#[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
3639
#[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
3740
#[inline]
41+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
3842
pub const fn then<T, F>(self, f: F) -> Option<T>
3943
where
4044
F: ~const FnOnce() -> T,
41-
F: ~const Drop,
45+
F: ~const Drop + ~const Destruct,
4246
{
4347
if self { Some(f()) } else { None }
4448
}

core/src/clone.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
3737
#![stable(feature = "rust1", since = "1.0.0")]
3838

39+
use crate::marker::Destruct;
40+
3941
/// A common trait for the ability to explicitly duplicate an object.
4042
///
4143
/// Differs from [`Copy`] in that [`Copy`] is implicit and an inexpensive bit-wise copy, while
@@ -128,9 +130,10 @@ pub trait Clone: Sized {
128130
#[inline]
129131
#[stable(feature = "rust1", since = "1.0.0")]
130132
#[default_method_body_is_const]
133+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
131134
fn clone_from(&mut self, source: &Self)
132135
where
133-
Self: ~const Drop,
136+
Self: ~const Drop + ~const Destruct,
134137
{
135138
*self = source.clone()
136139
}

core/src/intrinsics.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
)]
5555
#![allow(missing_docs)]
5656

57-
use crate::marker::DiscriminantKind;
57+
use crate::marker::{Destruct, DiscriminantKind};
5858
use crate::mem;
5959

6060
// These imports are used for simplifying intra-doc links
@@ -2353,14 +2353,15 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
23532353
#[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
23542354
#[lang = "const_eval_select"]
23552355
#[rustc_do_not_const_check]
2356+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
23562357
pub const unsafe fn const_eval_select<ARG, F, G, RET>(
23572358
arg: ARG,
23582359
_called_in_const: F,
23592360
called_at_rt: G,
23602361
) -> RET
23612362
where
23622363
F: ~const FnOnce<ARG, Output = RET>,
2363-
G: FnOnce<ARG, Output = RET> + ~const Drop,
2364+
G: FnOnce<ARG, Output = RET> + ~const Drop + ~const Destruct,
23642365
{
23652366
called_at_rt.call_once(arg)
23662367
}
@@ -2372,14 +2373,15 @@ where
23722373
)]
23732374
#[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
23742375
#[lang = "const_eval_select_ct"]
2376+
#[cfg_attr(not(bootstrap), allow(drop_bounds))] // FIXME remove `~const Drop` and this attr when bumping
23752377
pub const unsafe fn const_eval_select_ct<ARG, F, G, RET>(
23762378
arg: ARG,
23772379
called_in_const: F,
23782380
_called_at_rt: G,
23792381
) -> RET
23802382
where
23812383
F: ~const FnOnce<ARG, Output = RET>,
2382-
G: FnOnce<ARG, Output = RET> + ~const Drop,
2384+
G: FnOnce<ARG, Output = RET> + ~const Drop + ~const Destruct,
23832385
{
23842386
called_in_const.call_once(arg)
23852387
}

core/src/marker.rs

+16
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,22 @@ impl<T: ?Sized> Unpin for *const T {}
792792
#[stable(feature = "pin_raw", since = "1.38.0")]
793793
impl<T: ?Sized> Unpin for *mut T {}
794794

795+
/// A marker for types that can be dropped.
796+
///
797+
/// This should be used for `~const` bounds,
798+
/// as non-const bounds will always hold for every type.
799+
#[unstable(feature = "const_trait_impl", issue = "67792")]
800+
#[cfg_attr(not(bootstrap), lang = "destruct")]
801+
#[cfg_attr(
802+
not(bootstrap),
803+
rustc_on_unimplemented(message = "can't drop `{Self}`", append_const_msg,)
804+
)]
805+
pub trait Destruct {}
806+
807+
#[cfg(bootstrap)]
808+
#[unstable(feature = "const_trait_impl", issue = "67792")]
809+
impl<T: ?Sized> const Destruct for T {}
810+
795811
/// Implementations of `Copy` for primitive types.
796812
///
797813
/// Implementations that cannot be described in Rust

0 commit comments

Comments
 (0)