Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 13 additions & 13 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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)]

0 commit comments

Comments
 (0)