Skip to content

Commit

Permalink
Unrolled build for rust-lang#132503
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rust-timer authored Nov 3, 2024
2 parents 59ae5eb + 5266623 commit 8498263
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
12 changes: 4 additions & 8 deletions library/core/src/hash/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,17 @@ 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)
}

/// Creates a `SipHasher` that is keyed off the provided keys.
#[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) })
}
}
Expand All @@ -169,17 +167,15 @@ 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)
}

/// Creates a `SipHasher13` that is keyed off the provided keys.
#[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) }
}
}
Expand Down
1 change: 0 additions & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
26 changes: 23 additions & 3 deletions library/std/src/collections/hash/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::Entry::{Occupied, Vacant};
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
Expand Down Expand Up @@ -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);
}
3 changes: 1 addition & 2 deletions library/std/src/hash/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
//
Expand Down

0 comments on commit 8498263

Please sign in to comment.