Rollup merge of #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 (https://github.com/rust-lang/rust/issues/123197), so we also have a test that does not involve that type.

The second commit removes the last remnants of https://github.com/rust-lang/rust/issues/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 #104061
Related to https://github.com/rust-lang/rust/issues/102575
This commit is contained in:
Matthias Krüger 2024-11-03 12:08:52 +01:00 committed by GitHub
commit e9379382f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 29 additions and 16 deletions

View File

@ -147,9 +147,8 @@ impl SipHasher {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
#[must_use]
pub const fn new() -> SipHasher {
pub fn new() -> SipHasher {
SipHasher::new_with_keys(0, 0)
}
@ -157,9 +156,8 @@ pub const fn new() -> SipHasher {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
#[must_use]
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
SipHasher(SipHasher24 { hasher: Hasher::new_with_keys(key0, key1) })
}
}
@ -169,8 +167,7 @@ impl SipHasher13 {
#[inline]
#[unstable(feature = "hashmap_internals", issue = "none")]
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
pub const fn new() -> SipHasher13 {
pub fn new() -> SipHasher13 {
SipHasher13::new_with_keys(0, 0)
}
@ -178,8 +175,7 @@ pub const fn new() -> SipHasher13 {
#[inline]
#[unstable(feature = "hashmap_internals", issue = "none")]
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
SipHasher13 { hasher: Hasher::new_with_keys(key0, key1) }
}
}

View File

@ -120,7 +120,6 @@
#![feature(const_eval_select)]
#![feature(const_exact_div)]
#![feature(const_float_methods)]
#![feature(const_hash)]
#![feature(const_heap)]
#![feature(const_nonnull_new)]
#![feature(const_option_ext)]

View File

@ -19,7 +19,6 @@
#![feature(const_align_offset)]
#![feature(const_black_box)]
#![feature(const_eval_select)]
#![feature(const_hash)]
#![feature(const_heap)]
#![feature(const_nonnull_new)]
#![feature(const_option_ext)]

View File

@ -5,7 +5,7 @@
use super::HashMap;
use crate::assert_matches::assert_matches;
use crate::cell::RefCell;
use crate::hash::RandomState;
use crate::hash::{BuildHasher, BuildHasherDefault, DefaultHasher, RandomState};
use crate::test_helpers::test_rng;
// https://github.com/rust-lang/rust/issues/62301
@ -1124,6 +1124,26 @@ fn from_array() {
#[test]
fn const_with_hasher() {
const X: HashMap<(), (), ()> = HashMap::with_hasher(());
assert_eq!(X.len(), 0);
const X: HashMap<(), (), BuildHasherDefault<DefaultHasher>> =
HashMap::with_hasher(BuildHasherDefault::new());
let mut x = X;
assert_eq!(x.len(), 0);
x.insert((), ());
assert_eq!(x.len(), 1);
// It *is* possible to do this without using the `BuildHasherDefault` type.
struct MyBuildDefaultHasher;
impl BuildHasher for MyBuildDefaultHasher {
type Hasher = DefaultHasher;
fn build_hasher(&self) -> Self::Hasher {
DefaultHasher::new()
}
}
const Y: HashMap<(), (), MyBuildDefaultHasher> = HashMap::with_hasher(MyBuildDefaultHasher);
let mut y = Y;
assert_eq!(y.len(), 0);
y.insert((), ());
assert_eq!(y.len(), 1);
}

View File

@ -105,9 +105,8 @@ impl DefaultHasher {
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
#[inline]
#[allow(deprecated)]
#[rustc_const_unstable(feature = "const_hash", issue = "104061")]
#[must_use]
pub const fn new() -> DefaultHasher {
pub fn new() -> DefaultHasher {
DefaultHasher(SipHasher13::new_with_keys(0, 0))
}
}

View File

@ -328,6 +328,7 @@
// Library features (core):
// tidy-alphabetical-start
#![feature(array_chunks)]
#![feature(build_hasher_default_const_new)]
#![feature(c_str_module)]
#![feature(char_internals)]
#![feature(clone_to_uninit)]
@ -415,7 +416,6 @@
// Only for const-ness:
// tidy-alphabetical-start
#![feature(const_collections_with_hasher)]
#![feature(const_hash)]
#![feature(thread_local_internals)]
// tidy-alphabetical-end
//