diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 40759cb0ba83c..73cd98d55a572 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -107,6 +107,7 @@ #![feature(coerce_unsized)] #![feature(const_eval_select)] #![feature(const_heap)] +#![feature(const_trait_impl)] #![feature(core_intrinsics)] #![feature(deprecated_suggestion)] #![feature(deref_pure_trait)] diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 23d060d2158cd..d24ac8ad8d36f 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2483,7 +2483,8 @@ impl_eq! { Cow<'a, str>, &'b str } impl_eq! { Cow<'a, str>, String } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for String { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for String { /// Creates an empty `String`. #[inline] fn default() -> String { diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 5f4b85b58a98c..22f9c5d5b8f01 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -3771,7 +3771,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec { } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for Vec { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for Vec { /// Creates an empty `Vec`. /// /// The vector will not allocate until elements are pushed onto it. diff --git a/library/core/src/default.rs b/library/core/src/default.rs index 4c30290ff263b..fb79cb10f5e52 100644 --- a/library/core/src/default.rs +++ b/library/core/src/default.rs @@ -103,6 +103,7 @@ use crate::ascii::Char as AsciiChar; /// ``` #[cfg_attr(not(test), rustc_diagnostic_item = "Default")] #[stable(feature = "rust1", since = "1.0.0")] +#[const_trait] #[rustc_trivial_field_reads] pub trait Default: Sized { /// Returns the "default value" for a type. @@ -150,7 +151,8 @@ pub macro Default($item:item) { macro_rules! default_impl { ($t:ty, $v:expr, $doc:tt) => { #[stable(feature = "rust1", since = "1.0.0")] - impl Default for $t { + #[rustc_const_unstable(feature = "const_default", issue = "none")] + impl const Default for $t { #[inline(always)] #[doc = $doc] fn default() -> $t { diff --git a/library/core/src/iter/sources/empty.rs b/library/core/src/iter/sources/empty.rs index 3c3acceded889..a33cf21cb6aad 100644 --- a/library/core/src/iter/sources/empty.rs +++ b/library/core/src/iter/sources/empty.rs @@ -81,7 +81,8 @@ impl Clone for Empty { // not #[derive] because that adds a Default bound on T, // which isn't necessary. #[stable(feature = "iter_empty", since = "1.2.0")] -impl Default for Empty { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for Empty { fn default() -> Empty { Empty(marker::PhantomData) } diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 1620b949590d0..dc7fdfdba4b42 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -793,7 +793,8 @@ impl Clone for PhantomData { } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for PhantomData { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for PhantomData { fn default() -> Self { Self } diff --git a/library/core/src/option.rs b/library/core/src/option.rs index f4ac7af63961b..576f310e82287 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2045,7 +2045,8 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for Option { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for Option { /// Returns [`None`][Option::None]. /// /// # Examples diff --git a/library/core/src/ptr/alignment.rs b/library/core/src/ptr/alignment.rs index 74a1d40f4e734..a193313ed2aef 100644 --- a/library/core/src/ptr/alignment.rs +++ b/library/core/src/ptr/alignment.rs @@ -223,7 +223,8 @@ impl hash::Hash for Alignment { /// Returns [`Alignment::MIN`], which is valid for any type. #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl Default for Alignment { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for Alignment { fn default() -> Alignment { Alignment::MIN } diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index de00bdf8594f1..754e41d3fa1fa 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -4924,7 +4924,8 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for &[T] { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for &[T] { /// Creates an empty slice. fn default() -> Self { &[] @@ -4932,7 +4933,8 @@ impl Default for &[T] { } #[stable(feature = "mut_slice_default", since = "1.5.0")] -impl Default for &mut [T] { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for &mut [T] { /// Creates a mutable empty slice. fn default() -> Self { &mut [] diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 8a473b398bb5f..e894489b1daff 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -2794,7 +2794,8 @@ impl AsRef<[u8]> for str { } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for &str { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for &str { /// Creates an empty str #[inline] fn default() -> Self { @@ -2803,7 +2804,8 @@ impl Default for &str { } #[stable(feature = "default_mut_str", since = "1.28.0")] -impl Default for &mut str { +#[rustc_const_unstable(feature = "const_default", issue = "none")] +impl const Default for &mut str { /// Creates an empty mutable str #[inline] fn default() -> Self { diff --git a/tests/ui/const-generics/std/const-generics-alloc.rs b/tests/ui/const-generics/std/const-generics-alloc.rs new file mode 100644 index 0000000000000..3d3d05d58de90 --- /dev/null +++ b/tests/ui/const-generics/std/const-generics-alloc.rs @@ -0,0 +1,9 @@ +//@ run-pass +#![feature(const_trait_impl)] +#![allow(dead_code)] +// alloc::string +const STRING: String = Default::default(); +// alloc::vec +const VEC: Vec<()> = Default::default(); + +fn main() {} diff --git a/tests/ui/const-generics/std/const-generics-core.rs b/tests/ui/const-generics/std/const-generics-core.rs new file mode 100644 index 0000000000000..ba88f0644e8a6 --- /dev/null +++ b/tests/ui/const-generics/std/const-generics-core.rs @@ -0,0 +1,39 @@ +//@ run-pass +#![feature(const_trait_impl, ptr_alignment_type, ascii_char, f16, f128)] +#![allow(dead_code)] +// core::default +const UNIT: () = Default::default(); +const BOOL: bool = Default::default(); +const CHAR: char = Default::default(); +const ASCII_CHAR: std::ascii::Char = Default::default(); +const USIZE: usize = Default::default(); +const U8: u8 = Default::default(); +const U16: u16 = Default::default(); +const U32: u32 = Default::default(); +const U64: u64 = Default::default(); +const U128: u128 = Default::default(); +const I8: i8 = Default::default(); +const I16: i16 = Default::default(); +const I32: i32 = Default::default(); +const I64: i64 = Default::default(); +const I128: i128 = Default::default(); +const F16: f16 = Default::default(); +const F32: f32 = Default::default(); +const F64: f64 = Default::default(); +const F128: f128 = Default::default(); +// core::marker +const PHANTOM: std::marker::PhantomData<()> = Default::default(); +// core::option +const OPT: Option = Default::default(); +// core::iter::sources::empty +const EMPTY: std::iter::Empty<()> = Default::default(); +// core::ptr::alignment +const ALIGNMENT: std::ptr::Alignment = Default::default(); +// core::slice +const SLICE: &[()] = Default::default(); +const MUT_SLICE: &mut [()] = Default::default(); +//core::str +const STR: &str = Default::default(); +const MUT_STR: &mut str = Default::default(); + +fn main() {}