diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 4b649e43371d..70a838a35f9d 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -233,9 +233,7 @@ impl Clone for BTreeMap { } if self.is_empty() { - // Ideally we'd call `BTreeMap::new` here, but that has the `K: - // Ord` constraint, which this method lacks. - BTreeMap { root: None, length: 0 } + BTreeMap::new() } else { clone_subtree(self.root.as_ref().unwrap().reborrow()) // unwrap succeeds because not empty } @@ -499,10 +497,7 @@ impl BTreeMap { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] - pub const fn new() -> BTreeMap - where - K: Ord, - { + pub const fn new() -> BTreeMap { BTreeMap { root: None, length: 0 } } @@ -522,7 +517,7 @@ impl BTreeMap { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn clear(&mut self) { - *self = BTreeMap { root: None, length: 0 }; + *self = BTreeMap::new(); } /// Returns a reference to the value corresponding to the key. @@ -1957,7 +1952,7 @@ impl Hash for BTreeMap { } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for BTreeMap { +impl Default for BTreeMap { /// Creates an empty `BTreeMap`. fn default() -> BTreeMap { BTreeMap::new() diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index 17e538483431..a99d6c49ab7b 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -1745,7 +1745,7 @@ fn test_send() { } } -#[allow(dead_code)] +#[test] fn test_ord_absence() { fn map(mut map: BTreeMap) { map.is_empty(); @@ -1784,6 +1784,12 @@ fn test_ord_absence() { fn map_clone(mut map: BTreeMap) { map.clone_from(&map.clone()); } + + #[derive(Debug, Clone)] + struct NonOrd; + map(BTreeMap::::new()); + map_debug(BTreeMap::::new()); + map_clone(BTreeMap::::default()); } #[test] diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 0c268ad32b26..ff0db22e0cc2 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -246,10 +246,7 @@ impl BTreeSet { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] - pub const fn new() -> BTreeSet - where - T: Ord, - { + pub const fn new() -> BTreeSet { BTreeSet { map: BTreeMap::new() } } @@ -1192,7 +1189,7 @@ impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BTreeSet { } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for BTreeSet { +impl Default for BTreeSet { /// Creates an empty `BTreeSet`. fn default() -> BTreeSet { BTreeSet::new() diff --git a/library/alloc/src/collections/btree/set/tests.rs b/library/alloc/src/collections/btree/set/tests.rs index 5d590a26281d..0a87ae12d61a 100644 --- a/library/alloc/src/collections/btree/set/tests.rs +++ b/library/alloc/src/collections/btree/set/tests.rs @@ -607,7 +607,7 @@ fn test_send() { } } -#[allow(dead_code)] +#[test] fn test_ord_absence() { fn set(mut set: BTreeSet) { set.is_empty(); @@ -626,6 +626,12 @@ fn test_ord_absence() { fn set_clone(mut set: BTreeSet) { set.clone_from(&set.clone()); } + + #[derive(Debug, Clone)] + struct NonOrd; + set(BTreeSet::::new()); + set_debug(BTreeSet::::new()); + set_clone(BTreeSet::::default()); } #[test] diff --git a/library/alloc/tests/const_fns.rs b/library/alloc/tests/const_fns.rs index da58ae92e112..f448b3eb7c30 100644 --- a/library/alloc/tests/const_fns.rs +++ b/library/alloc/tests/const_fns.rs @@ -1,29 +1,5 @@ // Test const functions in the library -use core::cmp::Ordering; - -// FIXME remove this struct once we put `K: ?const Ord` on BTreeMap::new. -#[derive(PartialEq, Eq, PartialOrd)] -pub struct MyType; - -impl const Ord for MyType { - fn cmp(&self, _: &Self) -> Ordering { - Ordering::Equal - } - - fn max(self, _: Self) -> Self { - Self - } - - fn min(self, _: Self) -> Self { - Self - } - - fn clamp(self, _: Self, _: Self) -> Self { - Self - } -} - pub const MY_VEC: Vec = Vec::new(); pub const MY_VEC2: Vec = Default::default(); @@ -32,13 +8,13 @@ pub const MY_STRING2: String = Default::default(); use std::collections::{BTreeMap, BTreeSet}; -pub const MY_BTREEMAP: BTreeMap = BTreeMap::new(); -pub const MAP: &'static BTreeMap = &MY_BTREEMAP; +pub const MY_BTREEMAP: BTreeMap = BTreeMap::new(); +pub const MAP: &'static BTreeMap = &MY_BTREEMAP; pub const MAP_LEN: usize = MAP.len(); pub const MAP_IS_EMPTY: bool = MAP.is_empty(); -pub const MY_BTREESET: BTreeSet = BTreeSet::new(); -pub const SET: &'static BTreeSet = &MY_BTREESET; +pub const MY_BTREESET: BTreeSet = BTreeSet::new(); +pub const SET: &'static BTreeSet = &MY_BTREESET; pub const SET_LEN: usize = SET.len(); pub const SET_IS_EMPTY: bool = SET.is_empty();