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

Open
2 of 3 tasks
aDotInTheVoid opened this issue Oct 2, 2022 · 8 comments
Open
2 of 3 tasks

Tracking Issue for const_collections_with_hasher #102575

aDotInTheVoid opened this issue Oct 2, 2022 · 8 comments
Assignees
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. 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: A tracking issue for an RFC or an unstable feature. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. 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
Development

Successfully merging a pull request may close this issue.

5 participants