Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking Issue for const_collections_with_hasher #102575

Closed
2 of 3 tasks
aDotInTheVoid opened this issue Oct 2, 2022 · 13 comments · Fixed by #133696
Closed
2 of 3 tasks

Tracking Issue for const_collections_with_hasher #102575

aDotInTheVoid opened this issue Oct 2, 2022 · 13 comments · Fixed by #133696
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@aDotInTheVoid
Copy link
Member

aDotInTheVoid commented Oct 2, 2022

Feature gate: #![feature(const_collections_with_hasher)]

This is a tracking issue for making HashMap::with_hasher and HashSet::with_hasher const.

Public API

// std::collections
impl<K, V, S> HashMap<K, V, S> {
    pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> { ... }
}

impl<T, S> HashSet<T, S> {
    pub const fn with_hasher(hasher: S) -> HashSet<T, S> { ... }
}

Steps / History

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@aDotInTheVoid aDotInTheVoid added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 2, 2022
notriddle added a commit to notriddle/rust that referenced this issue Oct 5, 2022
…th_hasher, r=oli-obk,fee1-dead

Make Hash{Set,Map}::with_hasher unstably const

Makes  [`HashMap::with_hasher`](https://doc.rust-lang.org/stable/std/collections/hash_map/struct.HashMap.html#method.with_hasher) and [`HashSet::with_hasher`](https://doc.rust-lang.org/stable/std/collections/hash_set/struct.HashSet.html#method.with_hasher) `const`.

This allows

```rust
static GlobalState: Mutex<HashMap<i32, i32, SomeHasher>> = Mutex::new(HashMap::with_hasher(SomeHasher::new()))
```

Tracking issue: rust-lang#102575
@GoldsteinE
Copy link
Contributor

What blocks this from stabilization? Can I do something to move it forward?

@aDotInTheVoid
Copy link
Member Author

@GoldsteinE: see https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

Main questions for stabilization are do we want to add the constraint that an empty hashmap can be const constructed (ie doesn't alloc and more). This is true for the current hashbrown design, but might not be true for other alternatives. I'm not framiliar enough with hashmap design to say.

@Amanieu
Copy link
Member

Amanieu commented Aug 3, 2023

This is fairly straightforward, we are essentially guaranteeing that a newly created HashMap does not allocate any memory.

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Aug 3, 2023

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Aug 3, 2023
@rfcbot
Copy link

rfcbot commented Aug 3, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Aug 13, 2023
@rfcbot
Copy link

rfcbot commented Aug 13, 2023

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Aug 18, 2023
@aDotInTheVoid aDotInTheVoid self-assigned this Aug 20, 2023
@aDotInTheVoid aDotInTheVoid removed their assignment Sep 2, 2023
@aDotInTheVoid
Copy link
Member Author

Trying to stabilize this, I run into this error:

error: `hashbrown::HashMap::<K, V, S>::with_hasher` is not yet stable as a const fn
   --> library/std/src/collections/hash/map.rs:292:25
    |
292 |         HashMap { base: base::HashMap::with_hasher(hash_builder) }
    |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |

This code here

// FIXME(ecstaticmorse); For compatibility, we consider `unstable` callees that
// have no `rustc_const_stable` attributes to be const-unstable as well. This
// should be fixed later.
let callee_is_unstable_unmarked = tcx.lookup_const_stability(callee).is_none()
&& tcx.lookup_stability(callee).is_some_and(|s| s.is_unstable());
if callee_is_unstable_unmarked {
trace!("callee_is_unstable_unmarked");
// We do not use `const` modifiers for intrinsic "functions", as intrinsics are
// `extern` functions, and these have no way to get marked `const`. So instead we
// use `rustc_const_(un)stable` attributes to mean that the intrinsic is `const`
if self.ccx.is_const_stable_const_fn() || tcx.is_intrinsic(callee) {
self.check_op(ops::FnCallUnstable(callee, None));

seems to think it's unstable, despite hashbrown having no stability annotations. CC @ecstatic-morse

@aDotInTheVoid
Copy link
Member Author

aDotInTheVoid commented Nov 28, 2023

Solved in #118423 by changing the compiler to respect rustc_allow_const_fn_unstable here. This requires new semantics as there's no feature gate on the hashbrown side, so I said any will work here. Not sure if this is OK.

This also means a load of ugly things are needed to be cfg_attr(bootstrap, ...)ed

@rustbot claim

rust-cloud-vms bot pushed a commit to aDotInTheVoid/rust that referenced this issue Nov 29, 2023
Because std's dependencies are build with `-Zforce-unstable-if-unmarked`
normally, std is unable to call them in const-stable function. However,
becuase they had no explicit feature gates, there was no way to use
`rustc_allow_const_fn_unstable` to allow this.

Therefor we add `#[rustc_allow_const_fn_unstable(any)]` to allow using
these functions.

The reson to do this is rust-lang#102575, which relies on calling hashbrow function in
const-stable std functions.
@zk4x
Copy link

zk4x commented Oct 24, 2024

Can we get an ETA how many years will this approximately take to stabilize? How can I speed this process? Or should I just write my own hashmap or use alloc::collections::BTreeMap? Thanks ahead for any info.

@aDotInTheVoid aDotInTheVoid removed their assignment Oct 24, 2024
@aDotInTheVoid
Copy link
Member Author

Sorry, I should have given an update to this. The FCP’s been approved, so the only thing remaining it to actually do the stabilisation. Unfortunately that’s actually tricky in this case, due to HashMap being implemented in an external crate (hashbrown), it has really painful interactions with the const-stability checker.

Those checks are currently being rewritten in #131349, so after that lands it should be possible to try again if you (or someone else) wants to work on it.

@RalfJung
Copy link
Member

RalfJung commented Nov 2, 2024

I am planning to make this possible as part of finishing my work on #129815. The external hashbrown crate will also need some changes.

However, you won't be able to construct a HashMap<K, V> in const since that one uses randomized keys. You will have to set a non-default hasher builder. See the test being added in #132503.

workingjubilee added a commit to workingjubilee/rustc that referenced this issue Nov 2, 2024
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
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Nov 3, 2024
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
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Nov 3, 2024
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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 3, 2024
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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 3, 2024
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
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 3, 2024
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
@RalfJung
Copy link
Member

RalfJung commented Nov 3, 2024

The PR that should enable us to safely expose const fn from hashbrown is up now: #132541.

(That's just the infrastructure for this, once it lands, a bit of wiring-up needs to happen in hashbrown.)

@RalfJung
Copy link
Member

Cc @rust-lang/wg-const-eval

RalfJung added a commit to RalfJung/rust that referenced this issue Nov 30, 2024
bump hashbrown version

This pulls in rust-lang/hashbrown#586, in preparation for rust-lang#102575.

Cc `@Amanieu`
RalfJung added a commit to RalfJung/rust that referenced this issue Nov 30, 2024
bump hashbrown version

This pulls in rust-lang/hashbrown#586, in preparation for rust-lang#102575.

Cc ``@Amanieu``
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 30, 2024
Rollup merge of rust-lang#133670 - RalfJung:hashbrown, r=Amanieu

bump hashbrown version

This pulls in rust-lang/hashbrown#586, in preparation for rust-lang#102575.

Cc ``@Amanieu``
@bors bors closed this as completed in 1ae6f47 Dec 3, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 3, 2024
Rollup merge of rust-lang#133696 - RalfJung:const-hashmap, r=cuviper

stabilize const_collections_with_hasher and build_hasher_default_const_new

After a lot of preparatory work, finally we can stabilize creating `HashMap` in const context. :)

FCP for const_collections_with_hasher passed in rust-lang#102575.
Fixes rust-lang#102575.

FCP for build_hasher_default_const_new passed in rust-lang#123197.
Fixes rust-lang#123197.

Cc `@Amanieu`
Release notes: rust-lang#133347
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Dec 4, 2024
stabilize const_collections_with_hasher and build_hasher_default_const_new

After a lot of preparatory work, finally we can stabilize creating `HashMap` in const context. :)

FCP for const_collections_with_hasher passed in rust-lang/rust#102575.
Fixes rust-lang/rust#102575.

FCP for build_hasher_default_const_new passed in rust-lang/rust#123197.
Fixes rust-lang/rust#123197.

Cc `@Amanieu`
Release notes: rust-lang/rust#133347
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
7 participants