summary refs log tree commit diff
path: root/library/alloc/tests
diff options
context:
space:
mode:
authorDeadbeef <ent3rm4n@gmail.com>2021-08-14 16:35:12 +0000
committerDeadbeef <ent3rm4n@gmail.com>2021-08-17 07:15:54 +0000
commitb5afa6807b868bc8bb2e6f972629769b150ffd41 (patch)
tree4abfdf049ab0964aa14b4137fe4446d019169939 /library/alloc/tests
parent3b5df014390dcef66cc35f968fe51e9558e6ca13 (diff)
downloadrust-b5afa6807b868bc8bb2e6f972629769b150ffd41.tar.gz
rust-b5afa6807b868bc8bb2e6f972629769b150ffd41.zip
Constified `Default` implementations
The libs-api team agrees to allow const_trait_impl to appear in the
standard library as long as stable code cannot be broken (they are
properly gated) this means if the compiler teams thinks it's okay, then
it's okay.

My priority on constifying would be:

	1. Non-generic impls (e.g. Default) or generic impls with no
	   bounds
	2. Generic functions with bounds (that use const impls)
	3. Generic impls with bounds
	4. Impls for traits with associated types

For people opening constification PRs: please cc me and/or oli-obk.
Diffstat (limited to 'library/alloc/tests')
-rw-r--r--library/alloc/tests/const_fns.rs26
-rw-r--r--library/alloc/tests/lib.rs1
2 files changed, 14 insertions, 13 deletions
diff --git a/library/alloc/tests/const_fns.rs b/library/alloc/tests/const_fns.rs
index b6ef3eee291..da58ae92e11 100644
--- a/library/alloc/tests/const_fns.rs
+++ b/library/alloc/tests/const_fns.rs
@@ -1,16 +1,8 @@
-// Test several functions can be used for constants
-// 1. Vec::new()
-// 2. String::new()
-// 3. BTreeMap::new()
-// 4. BTreeSet::new()
+// Test const functions in the library
 
-#[allow(dead_code)]
-pub const MY_VEC: Vec<usize> = Vec::new();
-
-#[allow(dead_code)]
-pub const MY_STRING: String = String::new();
+use core::cmp::Ordering;
 
-// FIXME(fee1-dead) remove this struct once we put `K: ?const Ord` on BTreeMap::new.
+// FIXME remove this struct once we put `K: ?const Ord` on BTreeMap::new.
 #[derive(PartialEq, Eq, PartialOrd)]
 pub struct MyType;
 
@@ -32,7 +24,12 @@ impl const Ord for MyType {
     }
 }
 
-use core::cmp::Ordering;
+pub const MY_VEC: Vec<usize> = Vec::new();
+pub const MY_VEC2: Vec<usize> = Default::default();
+
+pub const MY_STRING: String = String::new();
+pub const MY_STRING2: String = Default::default();
+
 use std::collections::{BTreeMap, BTreeSet};
 
 pub const MY_BTREEMAP: BTreeMap<MyType, MyType> = BTreeMap::new();
@@ -47,7 +44,10 @@ pub const SET_IS_EMPTY: bool = SET.is_empty();
 
 #[test]
 fn test_const() {
+    assert_eq!(MY_VEC, MY_VEC2);
+    assert_eq!(MY_STRING, MY_STRING2);
+
     assert_eq!(MAP_LEN, 0);
     assert_eq!(SET_LEN, 0);
-    assert!(MAP_IS_EMPTY && SET_IS_EMPTY)
+    assert!(MAP_IS_EMPTY && SET_IS_EMPTY);
 }
diff --git a/library/alloc/tests/lib.rs b/library/alloc/tests/lib.rs
index 7284c05d5ff..5767108d423 100644
--- a/library/alloc/tests/lib.rs
+++ b/library/alloc/tests/lib.rs
@@ -24,6 +24,7 @@
 #![feature(vec_spare_capacity)]
 #![feature(string_remove_matches)]
 #![feature(const_btree_new)]
+#![feature(const_default_impls)]
 #![feature(const_trait_impl)]
 
 use std::collections::hash_map::DefaultHasher;