Skip to content

Commit

Permalink
Make Default const and add some const Default impls
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
estebank committed Dec 21, 2024
1 parent 9e136a3 commit b910a27
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 2 additions & 1 deletion library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3771,7 +3771,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Default for Vec<T> {
#[rustc_const_unstable(feature = "const_default", issue = "none")]
impl<T> const Default for Vec<T> {
/// Creates an empty `Vec<T>`.
///
/// The vector will not allocate until elements are pushed onto it.
Expand Down
4 changes: 3 additions & 1 deletion library/core/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/iter/sources/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ impl<T> Clone for Empty<T> {
// not #[derive] because that adds a Default bound on T,
// which isn't necessary.
#[stable(feature = "iter_empty", since = "1.2.0")]
impl<T> Default for Empty<T> {
#[rustc_const_unstable(feature = "const_default", issue = "none")]
impl<T> const Default for Empty<T> {
fn default() -> Empty<T> {
Empty(marker::PhantomData)
}
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,8 @@ impl<T: ?Sized> Clone for PhantomData<T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Default for PhantomData<T> {
#[rustc_const_unstable(feature = "const_default", issue = "none")]
impl<T: ?Sized> const Default for PhantomData<T> {
fn default() -> Self {
Self
}
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,8 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Default for Option<T> {
#[rustc_const_unstable(feature = "const_default", issue = "none")]
impl<T> const Default for Option<T> {
/// Returns [`None`][Option::None].
///
/// # Examples
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/ptr/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4924,15 +4924,17 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Default for &[T] {
#[rustc_const_unstable(feature = "const_default", issue = "none")]
impl<T> const Default for &[T] {
/// Creates an empty slice.
fn default() -> Self {
&[]
}
}

#[stable(feature = "mut_slice_default", since = "1.5.0")]
impl<T> Default for &mut [T] {
#[rustc_const_unstable(feature = "const_default", issue = "none")]
impl<T> const Default for &mut [T] {
/// Creates a mutable empty slice.
fn default() -> Self {
&mut []
Expand Down
6 changes: 4 additions & 2 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/const-generics/std/const-generics-alloc.rs
Original file line number Diff line number Diff line change
@@ -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() {}
39 changes: 39 additions & 0 deletions tests/ui/const-generics/std/const-generics-core.rs
Original file line number Diff line number Diff line change
@@ -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<i32> = 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() {}

0 comments on commit b910a27

Please sign in to comment.