Skip to content

Commit f838cbc

Browse files
committed
Auto merge of rust-lang#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 []

library/core/src/str/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3072,7 +3072,8 @@ impl AsRef<[u8]> for str {
30723072
}
30733073

30743074
#[stable(feature = "rust1", since = "1.0.0")]
3075-
impl Default for &str {
3075+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
3076+
impl const Default for &str {
30763077
/// Creates an empty str
30773078
#[inline]
30783079
fn default() -> Self {
@@ -3081,7 +3082,8 @@ impl Default for &str {
30813082
}
30823083

30833084
#[stable(feature = "default_mut_str", since = "1.28.0")]
3084-
impl Default for &mut str {
3085+
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
3086+
impl const Default for &mut str {
30853087
/// Creates an empty mutable str
30863088
#[inline]
30873089
fn default() -> Self {

src/tools/clippy/clippy_utils/src/visitors.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::msrvs::Msrv;
12
use crate::ty::needs_ordered_drop;
23
use crate::{get_enclosing_block, path_to_local_id};
4+
use crate::qualify_min_const_fn::is_stable_const_fn;
35
use core::ops::ControlFlow;
46
use rustc_ast::visit::{VisitorResult, try_visit};
57
use rustc_hir::def::{CtorKind, DefKind, Res};
@@ -343,13 +345,13 @@ pub fn is_const_evaluatable<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) ->
343345
.cx
344346
.qpath_res(p, hir_id)
345347
.opt_def_id()
346-
.is_some_and(|id| self.cx.tcx.is_const_fn(id)) => {},
348+
.is_some_and(|id| is_stable_const_fn(self.cx, id, Msrv::default())) => {},
347349
ExprKind::MethodCall(..)
348350
if self
349351
.cx
350352
.typeck_results()
351353
.type_dependent_def_id(e.hir_id)
352-
.is_some_and(|id| self.cx.tcx.is_const_fn(id)) => {},
354+
.is_some_and(|id| is_stable_const_fn(self.cx, id, Msrv::default())) => {},
353355
ExprKind::Binary(_, lhs, rhs)
354356
if self.cx.typeck_results().expr_ty(lhs).peel_refs().is_primitive_ty()
355357
&& self.cx.typeck_results().expr_ty(rhs).peel_refs().is_primitive_ty() => {},

tests/ui/consts/rustc-impl-const-stability.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@ known-bug: #110395
33

44
#![crate_type = "lib"]
5-
#![feature(staged_api, const_trait_impl)]
5+
#![feature(staged_api, const_trait_impl, const_default)]
66
#![stable(feature = "foo", since = "1.0.0")]
77

88
#[stable(feature = "potato", since = "1.27.0")]
@@ -12,8 +12,8 @@ pub struct Data {
1212

1313
#[stable(feature = "potato", since = "1.27.0")]
1414
#[rustc_const_unstable(feature = "data_foo", issue = "none")]
15-
impl const Default for Data {
16-
fn default() -> Data {
17-
Data { _data: 42 }
15+
impl const std::fmt::Debug for Data {
16+
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
17+
Ok(())
1818
}
1919
}

tests/ui/consts/rustc-impl-const-stability.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: const `impl` for trait `Default` which is not marked with `#[const_trait]`
1+
error: const `impl` for trait `Debug` which is not marked with `#[const_trait]`
22
--> $DIR/rustc-impl-const-stability.rs:15:12
33
|
4-
LL | impl const Default for Data {
5-
| ^^^^^^^ this trait is not `const`
4+
LL | impl const std::fmt::Debug for Data {
5+
| ^^^^^^^^^^^^^^^ this trait is not `const`
66
|
77
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
88
= note: adding a non-const method body in the future would be a breaking change

tests/ui/lint/dead-code/unused-struct-derive-default.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(dead_code)]
1+
#![deny(dead_code)] //~ NOTE the lint level is defined here
22

33
#[derive(Default)]
44
struct T; //~ ERROR struct `T` is never constructed
@@ -7,7 +7,7 @@ struct T; //~ ERROR struct `T` is never constructed
77
struct Used;
88

99
#[derive(Default)]
10-
enum E {
10+
enum E { //~ NOTE variant in this enum
1111
#[default]
1212
A,
1313
B, //~ ERROR variant `B` is never constructed

tests/ui/specialization/const_trait_impl.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#![feature(const_trait_impl, min_specialization, rustc_attrs)]
44

5+
use std::fmt::Debug;
6+
57
#[rustc_specialization_trait]
68
#[const_trait]
79
pub unsafe trait Sup {
@@ -31,19 +33,19 @@ pub trait A {
3133
fn a() -> u32;
3234
}
3335

34-
impl<T: [const] Default> const A for T {
36+
impl<T: [const] Debug> const A for T {
3537
default fn a() -> u32 {
3638
2
3739
}
3840
}
3941

40-
impl<T: [const] Default + [const] Sup> const A for T {
42+
impl<T: [const] Debug + [const] Sup> const A for T {
4143
default fn a() -> u32 {
4244
3
4345
}
4446
}
4547

46-
impl<T: [const] Default + [const] Sub> const A for T {
48+
impl<T: [const] Debug + [const] Sub> const A for T {
4749
fn a() -> u32 {
4850
T::foo()
4951
}

tests/ui/specialization/const_trait_impl.stderr

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
error: `[const]` can only be applied to `#[const_trait]` traits
2-
--> $DIR/const_trait_impl.rs:34:9
2+
--> $DIR/const_trait_impl.rs:36:9
33
|
4-
LL | impl<T: [const] Default> const A for T {
5-
| ^^^^^^^ can't be applied to `Default`
4+
LL | impl<T: [const] Debug> const A for T {
5+
| ^^^^^^^ can't be applied to `Debug`
66
|
7-
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
8-
--> $SRC_DIR/core/src/default.rs:LL:COL
7+
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
8+
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
99

1010
error: `[const]` can only be applied to `#[const_trait]` traits
11-
--> $DIR/const_trait_impl.rs:40:9
11+
--> $DIR/const_trait_impl.rs:42:9
1212
|
13-
LL | impl<T: [const] Default + [const] Sup> const A for T {
14-
| ^^^^^^^ can't be applied to `Default`
13+
LL | impl<T: [const] Debug + [const] Sup> const A for T {
14+
| ^^^^^^^ can't be applied to `Debug`
1515
|
16-
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
17-
--> $SRC_DIR/core/src/default.rs:LL:COL
16+
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
17+
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
1818

1919
error: `[const]` can only be applied to `#[const_trait]` traits
20-
--> $DIR/const_trait_impl.rs:46:9
20+
--> $DIR/const_trait_impl.rs:48:9
2121
|
22-
LL | impl<T: [const] Default + [const] Sub> const A for T {
23-
| ^^^^^^^ can't be applied to `Default`
22+
LL | impl<T: [const] Debug + [const] Sub> const A for T {
23+
| ^^^^^^^ can't be applied to `Debug`
2424
|
25-
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
26-
--> $SRC_DIR/core/src/default.rs:LL:COL
25+
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
26+
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
2727

2828
error: `[const]` can only be applied to `#[const_trait]` traits
29-
--> $DIR/const_trait_impl.rs:40:9
29+
--> $DIR/const_trait_impl.rs:42:9
3030
|
31-
LL | impl<T: [const] Default + [const] Sup> const A for T {
32-
| ^^^^^^^ can't be applied to `Default`
31+
LL | impl<T: [const] Debug + [const] Sup> const A for T {
32+
| ^^^^^^^ can't be applied to `Debug`
3333
|
34-
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
35-
--> $SRC_DIR/core/src/default.rs:LL:COL
34+
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
35+
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
3636
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3737

3838
error: `[const]` can only be applied to `#[const_trait]` traits
39-
--> $DIR/const_trait_impl.rs:34:9
39+
--> $DIR/const_trait_impl.rs:36:9
4040
|
41-
LL | impl<T: [const] Default> const A for T {
42-
| ^^^^^^^ can't be applied to `Default`
41+
LL | impl<T: [const] Debug> const A for T {
42+
| ^^^^^^^ can't be applied to `Debug`
4343
|
44-
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
45-
--> $SRC_DIR/core/src/default.rs:LL:COL
44+
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
45+
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
4646
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
4747

4848
error: `[const]` can only be applied to `#[const_trait]` traits
49-
--> $DIR/const_trait_impl.rs:46:9
49+
--> $DIR/const_trait_impl.rs:48:9
5050
|
51-
LL | impl<T: [const] Default + [const] Sub> const A for T {
52-
| ^^^^^^^ can't be applied to `Default`
51+
LL | impl<T: [const] Debug + [const] Sub> const A for T {
52+
| ^^^^^^^ can't be applied to `Debug`
5353
|
54-
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
55-
--> $SRC_DIR/core/src/default.rs:LL:COL
54+
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
55+
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
5656
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
5757

5858
error: aborting due to 6 previous errors
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ run-pass
2+
#![feature(const_trait_impl, const_default)]
3+
#![allow(dead_code)]
4+
// alloc::string
5+
const STRING: String = Default::default();
6+
// alloc::vec
7+
const VEC: Vec<()> = Default::default();
8+
9+
fn main() {}

0 commit comments

Comments
 (0)