Skip to content

Commit adf1688

Browse files
committed
Auto merge of rust-lang#86808 - fee1-dead:constify-1, r=oli-obk
constified implementations of `Default`
2 parents 30a0a9b + 6bd2ecb commit adf1688

File tree

19 files changed

+77
-28
lines changed

19 files changed

+77
-28
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
#![feature(const_fn_trait_bound)]
9696
#![feature(cow_is_borrowed)]
9797
#![feature(const_cow_is_borrowed)]
98+
#![feature(const_trait_impl)]
9899
#![feature(destructuring_assignment)]
99100
#![feature(dispatch_from_dyn)]
100101
#![feature(core_intrinsics)]

library/alloc/src/string.rs

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

21072107
#[stable(feature = "rust1", since = "1.0.0")]
2108-
impl Default for String {
2108+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
2109+
impl const Default for String {
21092110
/// Creates an empty `String`.
21102111
#[inline]
21112112
fn default() -> String {

library/alloc/src/vec/mod.rs

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

27602760
#[stable(feature = "rust1", since = "1.0.0")]
2761-
impl<T> Default for Vec<T> {
2761+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
2762+
impl<T> const Default for Vec<T> {
27622763
/// Creates an empty `Vec<T>`.
27632764
fn default() -> Vec<T> {
27642765
Vec::new()

library/alloc/tests/const_fns.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
// Test several functions can be used for constants
2-
// 1. Vec::new()
3-
// 2. String::new()
4-
// 3. BTreeMap::new()
5-
// 4. BTreeSet::new()
1+
// Test const functions in the library
62

7-
#[allow(dead_code)]
8-
pub const MY_VEC: Vec<usize> = Vec::new();
9-
10-
#[allow(dead_code)]
11-
pub const MY_STRING: String = String::new();
3+
use core::cmp::Ordering;
124

13-
// FIXME(fee1-dead) remove this struct once we put `K: ?const Ord` on BTreeMap::new.
5+
// FIXME remove this struct once we put `K: ?const Ord` on BTreeMap::new.
146
#[derive(PartialEq, Eq, PartialOrd)]
157
pub struct MyType;
168

@@ -32,7 +24,12 @@ impl const Ord for MyType {
3224
}
3325
}
3426

35-
use core::cmp::Ordering;
27+
pub const MY_VEC: Vec<usize> = Vec::new();
28+
pub const MY_VEC2: Vec<usize> = Default::default();
29+
30+
pub const MY_STRING: String = String::new();
31+
pub const MY_STRING2: String = Default::default();
32+
3633
use std::collections::{BTreeMap, BTreeSet};
3734

3835
pub const MY_BTREEMAP: BTreeMap<MyType, MyType> = BTreeMap::new();
@@ -47,7 +44,10 @@ pub const SET_IS_EMPTY: bool = SET.is_empty();
4744

4845
#[test]
4946
fn test_const() {
47+
assert_eq!(MY_VEC, MY_VEC2);
48+
assert_eq!(MY_STRING, MY_STRING2);
49+
5050
assert_eq!(MAP_LEN, 0);
5151
assert_eq!(SET_LEN, 0);
52-
assert!(MAP_IS_EMPTY && SET_IS_EMPTY)
52+
assert!(MAP_IS_EMPTY && SET_IS_EMPTY);
5353
}

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#![feature(vec_spare_capacity)]
2525
#![feature(string_remove_matches)]
2626
#![feature(const_btree_new)]
27+
#![feature(const_default_impls)]
2728
#![feature(const_trait_impl)]
2829

2930
use std::collections::hash_map::DefaultHasher;

library/core/src/array/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ macro_rules! array_impl_default {
280280
};
281281
{$n:expr,} => {
282282
#[stable(since = "1.4.0", feature = "array_default")]
283-
impl<T> Default for [T; $n] {
283+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
284+
impl<T> const Default for [T; $n] {
284285
fn default() -> [T; $n] { [] }
285286
}
286287
};

library/core/src/default.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ pub macro Default($item:item) {
171171
macro_rules! default_impl {
172172
($t:ty, $v:expr, $doc:tt) => {
173173
#[stable(feature = "rust1", since = "1.0.0")]
174-
impl Default for $t {
174+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
175+
impl const Default for $t {
175176
#[inline]
176177
#[doc = $doc]
177178
fn default() -> $t {

library/core/src/hash/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ impl<H> Clone for BuildHasherDefault<H> {
599599
}
600600

601601
#[stable(since = "1.7.0", feature = "build_hasher")]
602-
impl<H> Default for BuildHasherDefault<H> {
602+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
603+
impl<H> const Default for BuildHasherDefault<H> {
603604
fn default() -> BuildHasherDefault<H> {
604605
BuildHasherDefault(marker::PhantomData)
605606
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ impl<T> Clone for Empty<T> {
8585
// not #[derive] because that adds a Default bound on T,
8686
// which isn't necessary.
8787
#[stable(feature = "iter_empty", since = "1.2.0")]
88-
impl<T> Default for Empty<T> {
88+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
89+
impl<T> const Default for Empty<T> {
8990
fn default() -> Empty<T> {
9091
Empty(marker::PhantomData)
9192
}

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
#![feature(const_type_id)]
104104
#![feature(const_type_name)]
105105
#![feature(const_unreachable_unchecked)]
106+
#![feature(const_default_impls)]
106107
#![feature(duration_consts_2)]
107108
#![feature(ptr_metadata)]
108109
#![feature(slice_ptr_get)]

library/core/src/marker.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ macro_rules! impls {
528528
}
529529

530530
#[stable(feature = "rust1", since = "1.0.0")]
531-
impl<T: ?Sized> Default for $t<T> {
531+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
532+
impl<T: ?Sized> const Default for $t<T> {
532533
fn default() -> Self {
533534
Self
534535
}

library/core/src/option.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,8 @@ impl<T: Clone> Clone for Option<T> {
16421642
}
16431643

16441644
#[stable(feature = "rust1", since = "1.0.0")]
1645-
impl<T> Default for Option<T> {
1645+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
1646+
impl<T> const Default for Option<T> {
16461647
/// Returns [`None`][Option::None].
16471648
///
16481649
/// # Examples

library/core/src/slice/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -3501,15 +3501,17 @@ where
35013501
}
35023502

35033503
#[stable(feature = "rust1", since = "1.0.0")]
3504-
impl<T> Default for &[T] {
3504+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
3505+
impl<T> const Default for &[T] {
35053506
/// Creates an empty slice.
35063507
fn default() -> Self {
35073508
&[]
35083509
}
35093510
}
35103511

35113512
#[stable(feature = "mut_slice_default", since = "1.5.0")]
3512-
impl<T> Default for &mut [T] {
3513+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
3514+
impl<T> const Default for &mut [T] {
35133515
/// Creates a mutable empty slice.
35143516
fn default() -> Self {
35153517
&mut []

library/core/src/str/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2442,7 +2442,8 @@ impl AsRef<[u8]> for str {
24422442
}
24432443

24442444
#[stable(feature = "rust1", since = "1.0.0")]
2445-
impl Default for &str {
2445+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
2446+
impl const Default for &str {
24462447
/// Creates an empty str
24472448
#[inline]
24482449
fn default() -> Self {

library/core/src/sync/atomic.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ pub struct AtomicBool {
138138

139139
#[cfg(target_has_atomic_load_store = "8")]
140140
#[stable(feature = "rust1", since = "1.0.0")]
141-
impl Default for AtomicBool {
141+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
142+
impl const Default for AtomicBool {
142143
/// Creates an `AtomicBool` initialized to `false`.
143144
#[inline]
144145
fn default() -> Self {
@@ -168,7 +169,8 @@ pub struct AtomicPtr<T> {
168169

169170
#[cfg(target_has_atomic_load_store = "ptr")]
170171
#[stable(feature = "rust1", since = "1.0.0")]
171-
impl<T> Default for AtomicPtr<T> {
172+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
173+
impl<T> const Default for AtomicPtr<T> {
172174
/// Creates a null `AtomicPtr<T>`.
173175
fn default() -> AtomicPtr<T> {
174176
AtomicPtr::new(crate::ptr::null_mut())
@@ -1351,7 +1353,8 @@ macro_rules! atomic_int {
13511353
pub const $atomic_init: $atomic_type = $atomic_type::new(0);
13521354

13531355
#[$stable]
1354-
impl Default for $atomic_type {
1356+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
1357+
impl const Default for $atomic_type {
13551358
#[inline]
13561359
fn default() -> Self {
13571360
Self::new(Default::default())

library/std/src/lazy.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for SyncOnceCell<T> {}
8686
impl<T: UnwindSafe> UnwindSafe for SyncOnceCell<T> {}
8787

8888
#[unstable(feature = "once_cell", issue = "74465")]
89-
impl<T> Default for SyncOnceCell<T> {
89+
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
90+
impl<T> const Default for SyncOnceCell<T> {
9091
/// Creates a new empty cell.
9192
///
9293
/// # Example

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@
255255
#![feature(const_ipv6)]
256256
#![feature(const_raw_ptr_deref)]
257257
#![feature(const_socketaddr)]
258+
#![feature(const_trait_impl)]
258259
#![feature(container_error_extra)]
259260
#![feature(core_intrinsics)]
260261
#![feature(custom_test_frameworks)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This tests feature gates for const impls in the standard library.
2+
3+
// revisions: stock gated
4+
//[gated] run-pass
5+
6+
#![cfg_attr(gated, feature(const_trait_impl, const_default_impls))]
7+
8+
fn non_const_context() -> Vec<usize> {
9+
Default::default()
10+
}
11+
12+
const fn const_context() -> Vec<usize> {
13+
Default::default()
14+
//[stock]~^ ERROR calls in constant functions are limited
15+
}
16+
17+
fn main() {
18+
const VAL: Vec<usize> = const_context();
19+
20+
assert_eq!(VAL, non_const_context());
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2+
--> $DIR/std-impl-gate.rs:13:5
3+
|
4+
LL | Default::default()
5+
| ^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)