Skip to content

Commit f838cbc

Browse files
committed
Auto merge of #134628 - estebank:const-default, r=oli-obk
Make `Default` const and add some `const Default` impls Full list of `impl const Default` types: - () - bool - char - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData<T> - Option<T> - std::iter::Empty<T> - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec<T>
2 parents 040e2f8 + 8f8099f commit f838cbc

28 files changed

+192
-160
lines changed

library/alloc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@
107107
#![feature(char_max_len)]
108108
#![feature(clone_to_uninit)]
109109
#![feature(coerce_unsized)]
110+
#![feature(const_default)]
110111
#![feature(const_eval_select)]
111112
#![feature(const_heap)]
113+
#![feature(const_trait_impl)]
112114
#![feature(core_intrinsics)]
113115
#![feature(deprecated_suggestion)]
114116
#![feature(deref_pure_trait)]

library/alloc/src/string.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,8 @@ impl_eq! { Cow<'a, str>, &'b str }
26112611
impl_eq! { Cow<'a, str>, String }
26122612

26132613
#[stable(feature = "rust1", since = "1.0.0")]
2614-
impl Default for String {
2614+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
2615+
impl const Default for String {
26152616
/// Creates an empty `String`.
26162617
#[inline]
26172618
fn default() -> String {

library/alloc/src/vec/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3895,7 +3895,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
38953895
}
38963896

38973897
#[stable(feature = "rust1", since = "1.0.0")]
3898-
impl<T> Default for Vec<T> {
3898+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
3899+
impl<T> const Default for Vec<T> {
38993900
/// Creates an empty `Vec<T>`.
39003901
///
39013902
/// The vector will not allocate until elements are pushed onto it.

library/core/src/cell.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ impl<T: Copy> Clone for Cell<T> {
333333
}
334334

335335
#[stable(feature = "rust1", since = "1.0.0")]
336-
impl<T: Default> Default for Cell<T> {
336+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
337+
impl<T: ~const Default> const Default for Cell<T> {
337338
/// Creates a `Cell<T>`, with the `Default` value for T.
338339
#[inline]
339340
fn default() -> Cell<T> {
@@ -1323,7 +1324,8 @@ impl<T: Clone> Clone for RefCell<T> {
13231324
}
13241325

13251326
#[stable(feature = "rust1", since = "1.0.0")]
1326-
impl<T: Default> Default for RefCell<T> {
1327+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
1328+
impl<T: ~const Default> const Default for RefCell<T> {
13271329
/// Creates a `RefCell<T>`, with the `Default` value for T.
13281330
#[inline]
13291331
fn default() -> RefCell<T> {
@@ -2330,7 +2332,8 @@ impl<T: ?Sized> UnsafeCell<T> {
23302332
}
23312333

23322334
#[stable(feature = "unsafe_cell_default", since = "1.10.0")]
2333-
impl<T: Default> Default for UnsafeCell<T> {
2335+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
2336+
impl<T: ~const Default> const Default for UnsafeCell<T> {
23342337
/// Creates an `UnsafeCell`, with the `Default` value for T.
23352338
fn default() -> UnsafeCell<T> {
23362339
UnsafeCell::new(Default::default())
@@ -2434,7 +2437,8 @@ impl<T: ?Sized> SyncUnsafeCell<T> {
24342437
}
24352438

24362439
#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
2437-
impl<T: Default> Default for SyncUnsafeCell<T> {
2440+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
2441+
impl<T: ~const Default> const Default for SyncUnsafeCell<T> {
24382442
/// Creates an `SyncUnsafeCell`, with the `Default` value for T.
24392443
fn default() -> SyncUnsafeCell<T> {
24402444
SyncUnsafeCell::new(Default::default())

library/core/src/default.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ use crate::ascii::Char as AsciiChar;
103103
/// ```
104104
#[rustc_diagnostic_item = "Default"]
105105
#[stable(feature = "rust1", since = "1.0.0")]
106+
#[const_trait]
107+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
106108
pub trait Default: Sized {
107109
/// Returns the "default value" for a type.
108110
///
@@ -149,7 +151,8 @@ pub macro Default($item:item) {
149151
macro_rules! default_impl {
150152
($t:ty, $v:expr, $doc:tt) => {
151153
#[stable(feature = "rust1", since = "1.0.0")]
152-
impl Default for $t {
154+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
155+
impl const Default for $t {
153156
#[inline(always)]
154157
#[doc = $doc]
155158
fn default() -> $t {

library/core/src/iter/sources/empty.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ impl<T> Clone for Empty<T> {
8181
// not #[derive] because that adds a Default bound on T,
8282
// which isn't necessary.
8383
#[stable(feature = "iter_empty", since = "1.2.0")]
84-
impl<T> Default for Empty<T> {
84+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
85+
impl<T> const Default for Empty<T> {
8586
fn default() -> Empty<T> {
8687
Empty(marker::PhantomData)
8788
}

library/core/src/marker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,8 @@ impl<T: PointeeSized> Clone for PhantomData<T> {
858858
}
859859

860860
#[stable(feature = "rust1", since = "1.0.0")]
861-
impl<T: PointeeSized> Default for PhantomData<T> {
861+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
862+
impl<T: PointeeSized> const Default for PhantomData<T> {
862863
fn default() -> Self {
863864
Self
864865
}

library/core/src/option.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,8 @@ where
21112111
impl<T> crate::clone::UseCloned for Option<T> where T: crate::clone::UseCloned {}
21122112

21132113
#[stable(feature = "rust1", since = "1.0.0")]
2114-
impl<T> Default for Option<T> {
2114+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
2115+
impl<T> const Default for Option<T> {
21152116
/// Returns [`None`][Option::None].
21162117
///
21172118
/// # Examples

library/core/src/ptr/alignment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ impl hash::Hash for Alignment {
230230

231231
/// Returns [`Alignment::MIN`], which is valid for any type.
232232
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
233-
impl Default for Alignment {
233+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
234+
impl const Default for Alignment {
234235
fn default() -> Alignment {
235236
Alignment::MIN
236237
}

library/core/src/slice/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5158,15 +5158,17 @@ where
51585158
}
51595159

51605160
#[stable(feature = "rust1", since = "1.0.0")]
5161-
impl<T> Default for &[T] {
5161+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
5162+
impl<T> const Default for &[T] {
51625163
/// Creates an empty slice.
51635164
fn default() -> Self {
51645165
&[]
51655166
}
51665167
}
51675168

51685169
#[stable(feature = "mut_slice_default", since = "1.5.0")]
5169-
impl<T> Default for &mut [T] {
5170+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
5171+
impl<T> const Default for &mut [T] {
51705172
/// Creates a mutable empty slice.
51715173
fn default() -> Self {
51725174
&mut []

0 commit comments

Comments
 (0)