Skip to content

Commit bfeeb74

Browse files
authored
Rollup merge of rust-lang#132503 - RalfJung:const-hash-map, r=Amanieu
better test for const HashMap; remove const_hash leftovers The existing `const_with_hasher` test is kind of silly since the HashMap it constructs can never contain any elements. So this adjusts the test to construct a usable HashMap, which is a bit non-trivial since the default hash builder cannot be built in `const`. `BuildHasherDefault::new()` helps but is unstable (rust-lang#123197), so we also have a test that does not involve that type. The second commit removes the last remnants of rust-lang#104061, since they aren't actually useful -- without const traits, you can't do any hashing in `const`. Cc ``@rust-lang/libs-api`` ``@rust-lang/wg-const-eval`` Closes rust-lang#104061 Related to rust-lang#102575
2 parents a42fc21 + 261c5b9 commit bfeeb74

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

core/src/hash/sip.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,17 @@ impl SipHasher {
147147
#[inline]
148148
#[stable(feature = "rust1", since = "1.0.0")]
149149
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
150-
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
151150
#[must_use]
152-
pub const fn new() -> SipHasher {
151+
pub fn new() -> SipHasher {
153152
SipHasher::new_with_keys(0, 0)
154153
}
155154

156155
/// Creates a `SipHasher` that is keyed off the provided keys.
157156
#[inline]
158157
#[stable(feature = "rust1", since = "1.0.0")]
159158
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
160-
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
161159
#[must_use]
162-
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
160+
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
163161
SipHasher(SipHasher24 { hasher: Hasher::new_with_keys(key0, key1) })
164162
}
165163
}
@@ -169,17 +167,15 @@ impl SipHasher13 {
169167
#[inline]
170168
#[unstable(feature = "hashmap_internals", issue = "none")]
171169
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
172-
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
173-
pub const fn new() -> SipHasher13 {
170+
pub fn new() -> SipHasher13 {
174171
SipHasher13::new_with_keys(0, 0)
175172
}
176173

177174
/// Creates a `SipHasher13` that is keyed off the provided keys.
178175
#[inline]
179176
#[unstable(feature = "hashmap_internals", issue = "none")]
180177
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
181-
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
182-
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
178+
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
183179
SipHasher13 { hasher: Hasher::new_with_keys(key0, key1) }
184180
}
185181
}

core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
#![feature(const_eval_select)]
121121
#![feature(const_exact_div)]
122122
#![feature(const_float_methods)]
123-
#![feature(const_hash)]
124123
#![feature(const_heap)]
125124
#![feature(const_nonnull_new)]
126125
#![feature(const_option_ext)]

core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#![feature(const_align_offset)]
2020
#![feature(const_black_box)]
2121
#![feature(const_eval_select)]
22-
#![feature(const_hash)]
2322
#![feature(const_heap)]
2423
#![feature(const_nonnull_new)]
2524
#![feature(const_option_ext)]

std/src/collections/hash/map/tests.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::Entry::{Occupied, Vacant};
55
use super::HashMap;
66
use crate::assert_matches::assert_matches;
77
use crate::cell::RefCell;
8-
use crate::hash::RandomState;
8+
use crate::hash::{BuildHasher, BuildHasherDefault, DefaultHasher, RandomState};
99
use crate::test_helpers::test_rng;
1010

1111
// https://github.com/rust-lang/rust/issues/62301
@@ -1124,6 +1124,26 @@ fn from_array() {
11241124

11251125
#[test]
11261126
fn const_with_hasher() {
1127-
const X: HashMap<(), (), ()> = HashMap::with_hasher(());
1128-
assert_eq!(X.len(), 0);
1127+
const X: HashMap<(), (), BuildHasherDefault<DefaultHasher>> =
1128+
HashMap::with_hasher(BuildHasherDefault::new());
1129+
let mut x = X;
1130+
assert_eq!(x.len(), 0);
1131+
x.insert((), ());
1132+
assert_eq!(x.len(), 1);
1133+
1134+
// It *is* possible to do this without using the `BuildHasherDefault` type.
1135+
struct MyBuildDefaultHasher;
1136+
impl BuildHasher for MyBuildDefaultHasher {
1137+
type Hasher = DefaultHasher;
1138+
1139+
fn build_hasher(&self) -> Self::Hasher {
1140+
DefaultHasher::new()
1141+
}
1142+
}
1143+
1144+
const Y: HashMap<(), (), MyBuildDefaultHasher> = HashMap::with_hasher(MyBuildDefaultHasher);
1145+
let mut y = Y;
1146+
assert_eq!(y.len(), 0);
1147+
y.insert((), ());
1148+
assert_eq!(y.len(), 1);
11291149
}

std/src/hash/random.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ impl DefaultHasher {
105105
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
106106
#[inline]
107107
#[allow(deprecated)]
108-
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
109108
#[must_use]
110-
pub const fn new() -> DefaultHasher {
109+
pub fn new() -> DefaultHasher {
111110
DefaultHasher(SipHasher13::new_with_keys(0, 0))
112111
}
113112
}

std/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@
328328
// Library features (core):
329329
// tidy-alphabetical-start
330330
#![feature(array_chunks)]
331+
#![feature(build_hasher_default_const_new)]
331332
#![feature(c_str_module)]
332333
#![feature(char_internals)]
333334
#![feature(clone_to_uninit)]
@@ -415,7 +416,6 @@
415416
// Only for const-ness:
416417
// tidy-alphabetical-start
417418
#![feature(const_collections_with_hasher)]
418-
#![feature(const_hash)]
419419
#![feature(thread_local_internals)]
420420
// tidy-alphabetical-end
421421
//

0 commit comments

Comments
 (0)