Skip to content

Commit 7316ca4

Browse files
committed
Revert "HACK: Remove all const trait impls and ~const from libcore/libstd"
This reverts commit 7dc2fab2895041f82fabc6525c895cc574b3d044.
1 parent 1ee0614 commit 7316ca4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1116
-408
lines changed

compiler/rustc_ast/src/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<T> {
127127
}
128128

129129
impl<T> P<[T]> {
130-
pub fn new() -> P<[T]> {
130+
pub const fn new() -> P<[T]> {
131131
P { ptr: Box::default() }
132132
}
133133

library/alloc/src/alloc.rs

+19-4
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

@@ -323,12 +325,16 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
323325

324326
#[cfg_attr(not(test), lang = "box_free")]
325327
#[inline]
328+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
326329
// This signature has to be the same as `Box`, otherwise an ICE will happen.
327330
// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
328331
// well.
329332
// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
330333
// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
331-
pub(crate) unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: Unique<T>, alloc: A) {
334+
pub(crate) const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Destruct>(
335+
ptr: Unique<T>,
336+
alloc: A,
337+
) {
332338
unsafe {
333339
let size = size_of_val(ptr.as_ref());
334340
let align = min_align_of_val(ptr.as_ref());
@@ -360,12 +366,21 @@ extern "Rust" {
360366
/// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
361367
/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
362368
#[stable(feature = "global_alloc", since = "1.28.0")]
369+
#[rustc_const_unstable(feature = "const_alloc_error", issue = "92523")]
363370
#[cfg(all(not(no_global_oom_handling), not(test)))]
364371
#[cold]
365-
pub fn handle_alloc_error(layout: Layout) -> ! {
366-
unsafe {
367-
__rust_alloc_error_handler(layout.size(), layout.align());
372+
pub const fn handle_alloc_error(layout: Layout) -> ! {
373+
const fn ct_error(_: Layout) -> ! {
374+
panic!("allocation failed");
375+
}
376+
377+
fn rt_error(layout: Layout) -> ! {
378+
unsafe {
379+
__rust_alloc_error_handler(layout.size(), layout.align());
380+
}
368381
}
382+
383+
unsafe { core::intrinsics::const_eval_select((layout,), ct_error, rt_error) }
369384
}
370385

371386
// For alloc test `std::alloc::handle_alloc_error` can be used directly.

library/alloc/src/borrow.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,10 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
329329
}
330330

331331
#[stable(feature = "rust1", since = "1.0.0")]
332-
impl<B: ?Sized + ToOwned> Deref for Cow<'_, B>
332+
#[rustc_const_unstable(feature = "const_deref", issue = "88955")]
333+
impl<B: ?Sized + ToOwned> const Deref for Cow<'_, B>
333334
where
334-
B::Owned: Borrow<B>,
335+
B::Owned: ~const Borrow<B>,
335336
{
336337
type Target = B;
337338

library/alloc/src/boxed.rs

+50-28
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ use core::hash::{Hash, Hasher};
157157
#[cfg(not(no_global_oom_handling))]
158158
use core::iter::FromIterator;
159159
use core::iter::{FusedIterator, Iterator};
160-
use core::marker::{Unpin, Unsize};
160+
use core::marker::{Destruct, Unpin, Unsize};
161161
use core::mem;
162162
use core::ops::{
163163
CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver,
@@ -373,11 +373,12 @@ impl<T, A: Allocator> Box<T, A> {
373373
/// ```
374374
#[cfg(not(no_global_oom_handling))]
375375
#[unstable(feature = "allocator_api", issue = "32838")]
376+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
376377
#[must_use]
377378
#[inline]
378-
pub fn new_in(x: T, alloc: A) -> Self
379+
pub const fn new_in(x: T, alloc: A) -> Self
379380
where
380-
A: Allocator,
381+
A: ~const Allocator + ~const Destruct,
381382
{
382383
let mut boxed = Self::new_uninit_in(alloc);
383384
unsafe {
@@ -402,10 +403,12 @@ impl<T, A: Allocator> Box<T, A> {
402403
/// # Ok::<(), std::alloc::AllocError>(())
403404
/// ```
404405
#[unstable(feature = "allocator_api", issue = "32838")]
406+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
405407
#[inline]
406-
pub fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
408+
pub const fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
407409
where
408-
A: Allocator,
410+
T: ~const Destruct,
411+
A: ~const Allocator + ~const Destruct,
409412
{
410413
let mut boxed = Self::try_new_uninit_in(alloc)?;
411414
unsafe {
@@ -435,12 +438,13 @@ impl<T, A: Allocator> Box<T, A> {
435438
/// assert_eq!(*five, 5)
436439
/// ```
437440
#[unstable(feature = "allocator_api", issue = "32838")]
441+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
438442
#[cfg(not(no_global_oom_handling))]
439443
#[must_use]
440444
// #[unstable(feature = "new_uninit", issue = "63291")]
441-
pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
445+
pub const fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
442446
where
443-
A: Allocator,
447+
A: ~const Allocator + ~const Destruct,
444448
{
445449
let layout = Layout::new::<mem::MaybeUninit<T>>();
446450
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -475,9 +479,10 @@ impl<T, A: Allocator> Box<T, A> {
475479
/// ```
476480
#[unstable(feature = "allocator_api", issue = "32838")]
477481
// #[unstable(feature = "new_uninit", issue = "63291")]
478-
pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
482+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
483+
pub const fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
479484
where
480-
A: Allocator,
485+
A: ~const Allocator + ~const Destruct,
481486
{
482487
let layout = Layout::new::<mem::MaybeUninit<T>>();
483488
let ptr = alloc.allocate(layout)?.cast();
@@ -505,12 +510,13 @@ impl<T, A: Allocator> Box<T, A> {
505510
///
506511
/// [zeroed]: mem::MaybeUninit::zeroed
507512
#[unstable(feature = "allocator_api", issue = "32838")]
513+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
508514
#[cfg(not(no_global_oom_handling))]
509515
// #[unstable(feature = "new_uninit", issue = "63291")]
510516
#[must_use]
511-
pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
517+
pub const fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
512518
where
513-
A: Allocator,
519+
A: ~const Allocator + ~const Destruct,
514520
{
515521
let layout = Layout::new::<mem::MaybeUninit<T>>();
516522
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -545,9 +551,10 @@ impl<T, A: Allocator> Box<T, A> {
545551
/// [zeroed]: mem::MaybeUninit::zeroed
546552
#[unstable(feature = "allocator_api", issue = "32838")]
547553
// #[unstable(feature = "new_uninit", issue = "63291")]
548-
pub fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
554+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
555+
pub const fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
549556
where
550-
A: Allocator,
557+
A: ~const Allocator + ~const Destruct,
551558
{
552559
let layout = Layout::new::<mem::MaybeUninit<T>>();
553560
let ptr = alloc.allocate_zeroed(layout)?.cast();
@@ -563,11 +570,12 @@ impl<T, A: Allocator> Box<T, A> {
563570
/// construct a (pinned) `Box` in a different way than with [`Box::new_in`].
564571
#[cfg(not(no_global_oom_handling))]
565572
#[unstable(feature = "allocator_api", issue = "32838")]
573+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
566574
#[must_use]
567575
#[inline(always)]
568-
pub fn pin_in(x: T, alloc: A) -> Pin<Self>
576+
pub const fn pin_in(x: T, alloc: A) -> Pin<Self>
569577
where
570-
A: 'static + Allocator,
578+
A: 'static + ~const Allocator + ~const Destruct,
571579
{
572580
Self::into_pin(Self::new_in(x, alloc))
573581
}
@@ -576,7 +584,8 @@ impl<T, A: Allocator> Box<T, A> {
576584
///
577585
/// This conversion does not allocate on the heap and happens in place.
578586
#[unstable(feature = "box_into_boxed_slice", issue = "71582")]
579-
pub fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
587+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
588+
pub const fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
580589
let (raw, alloc) = Box::into_raw_with_allocator(boxed);
581590
unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) }
582591
}
@@ -593,8 +602,12 @@ impl<T, A: Allocator> Box<T, A> {
593602
/// assert_eq!(Box::into_inner(c), 5);
594603
/// ```
595604
#[unstable(feature = "box_into_inner", issue = "80437")]
605+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
596606
#[inline]
597-
pub fn into_inner(boxed: Self) -> T {
607+
pub const fn into_inner(boxed: Self) -> T
608+
where
609+
Self: ~const Destruct,
610+
{
598611
*boxed
599612
}
600613
}
@@ -808,8 +821,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
808821
/// assert_eq!(*five, 5)
809822
/// ```
810823
#[unstable(feature = "new_uninit", issue = "63291")]
824+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
811825
#[inline]
812-
pub unsafe fn assume_init(self) -> Box<T, A> {
826+
pub const unsafe fn assume_init(self) -> Box<T, A> {
813827
let (raw, alloc) = Box::into_raw_with_allocator(self);
814828
unsafe { Box::from_raw_in(raw as *mut T, alloc) }
815829
}
@@ -842,8 +856,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
842856
/// }
843857
/// ```
844858
#[unstable(feature = "new_uninit", issue = "63291")]
859+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
845860
#[inline]
846-
pub fn write(mut boxed: Self, value: T) -> Box<T, A> {
861+
pub const fn write(mut boxed: Self, value: T) -> Box<T, A> {
847862
unsafe {
848863
(*boxed).write(value);
849864
boxed.assume_init()
@@ -1086,8 +1101,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10861101
///
10871102
/// [memory layout]: self#memory-layout
10881103
#[unstable(feature = "allocator_api", issue = "32838")]
1104+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
10891105
#[inline]
1090-
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
1106+
pub const fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
10911107
let (leaked, alloc) = Box::into_unique(b);
10921108
(leaked.as_ptr(), alloc)
10931109
}
@@ -1097,9 +1113,10 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10971113
issue = "none",
10981114
reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
10991115
)]
1116+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
11001117
#[inline]
11011118
#[doc(hidden)]
1102-
pub fn into_unique(b: Self) -> (Unique<T>, A) {
1119+
pub const fn into_unique(b: Self) -> (Unique<T>, A) {
11031120
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
11041121
// raw pointer for the type system. Turning it directly into a raw pointer would not be
11051122
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
@@ -1157,8 +1174,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11571174
/// assert_eq!(*static_ref, [4, 2, 3]);
11581175
/// ```
11591176
#[stable(feature = "box_leak", since = "1.26.0")]
1177+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
11601178
#[inline]
1161-
pub fn leak<'a>(b: Self) -> &'a mut T
1179+
pub const fn leak<'a>(b: Self) -> &'a mut T
11621180
where
11631181
A: 'a,
11641182
{
@@ -1228,7 +1246,7 @@ impl<T: Default> Default for Box<T> {
12281246
#[cfg(not(no_global_oom_handling))]
12291247
#[stable(feature = "rust1", since = "1.0.0")]
12301248
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
1231-
impl<T> Default for Box<[T]> {
1249+
impl<T> const Default for Box<[T]> {
12321250
fn default() -> Self {
12331251
let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling();
12341252
Box(ptr, Global)
@@ -1238,7 +1256,7 @@ impl<T> Default for Box<[T]> {
12381256
#[cfg(not(no_global_oom_handling))]
12391257
#[stable(feature = "default_box_extra", since = "1.17.0")]
12401258
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
1241-
impl Default for Box<str> {
1259+
impl const Default for Box<str> {
12421260
fn default() -> Self {
12431261
// SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`.
12441262
let ptr: Unique<str> = unsafe {
@@ -1434,7 +1452,8 @@ impl<T> From<T> for Box<T> {
14341452
}
14351453

14361454
#[stable(feature = "pin", since = "1.33.0")]
1437-
impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Pin<Box<T, A>>
1455+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
1456+
impl<T: ?Sized, A: Allocator> const From<Box<T, A>> for Pin<Box<T, A>>
14381457
where
14391458
A: 'static,
14401459
{
@@ -1822,7 +1841,8 @@ impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
18221841
}
18231842

18241843
#[stable(feature = "rust1", since = "1.0.0")]
1825-
impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
1844+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
1845+
impl<T: ?Sized, A: Allocator> const Deref for Box<T, A> {
18261846
type Target = T;
18271847

18281848
fn deref(&self) -> &T {
@@ -1831,7 +1851,8 @@ impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
18311851
}
18321852

18331853
#[stable(feature = "rust1", since = "1.0.0")]
1834-
impl<T: ?Sized, A: Allocator> DerefMut for Box<T, A> {
1854+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
1855+
impl<T: ?Sized, A: Allocator> const DerefMut for Box<T, A> {
18351856
fn deref_mut(&mut self) -> &mut T {
18361857
&mut **self
18371858
}
@@ -2010,7 +2031,8 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
20102031
* could have a method to project a Pin<T> from it.
20112032
*/
20122033
#[stable(feature = "pin", since = "1.33.0")]
2013-
impl<T: ?Sized, A: Allocator> Unpin for Box<T, A> where A: 'static {}
2034+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
2035+
impl<T: ?Sized, A: Allocator> const Unpin for Box<T, A> where A: 'static {}
20142036

20152037
#[unstable(feature = "generator_trait", issue = "43122")]
20162038
impl<G: ?Sized + Generator<R> + Unpin, R, A: Allocator> Generator<R> for Box<G, A>

library/alloc/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@
9595
#![feature(assert_matches)]
9696
#![feature(async_iterator)]
9797
#![feature(coerce_unsized)]
98+
#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
9899
#![feature(const_box)]
99100
#![cfg_attr(not(no_global_oom_handling), feature(const_btree_new))]
100101
#![feature(const_cow_is_borrowed)]
102+
#![feature(const_convert)]
101103
#![feature(const_size_of_val)]
102104
#![feature(const_align_of_val)]
103105
#![feature(const_ptr_read)]
@@ -107,6 +109,7 @@
107109
#![feature(core_c_str)]
108110
#![feature(core_intrinsics)]
109111
#![feature(core_ffi_c)]
112+
#![feature(const_eval_select)]
110113
#![feature(const_pin)]
111114
#![feature(cstr_from_bytes_until_nul)]
112115
#![feature(dispatch_from_dyn)]
@@ -147,6 +150,7 @@
147150
#![feature(allow_internal_unstable)]
148151
#![feature(associated_type_bounds)]
149152
#![feature(cfg_sanitize)]
153+
#![feature(const_deref)]
150154
#![feature(const_mut_refs)]
151155
#![feature(const_ptr_write)]
152156
#![feature(const_precise_live_drops)]

library/alloc/src/string.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,8 @@ impl_eq! { Cow<'a, str>, &'b str }
22122212
impl_eq! { Cow<'a, str>, String }
22132213

22142214
#[stable(feature = "rust1", since = "1.0.0")]
2215-
impl Default for String {
2215+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
2216+
impl const Default for String {
22162217
/// Creates an empty `String`.
22172218
#[inline]
22182219
fn default() -> String {

library/alloc/src/vec/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2924,7 +2924,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
29242924
}
29252925

29262926
#[stable(feature = "rust1", since = "1.0.0")]
2927-
impl<T> Default for Vec<T> {
2927+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
2928+
impl<T> const Default for Vec<T> {
29282929
/// Creates an empty `Vec<T>`.
29292930
fn default() -> Vec<T> {
29302931
Vec::new()

0 commit comments

Comments
 (0)