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

Adds S to HashMap/HashSet impls of Contains #33973

Merged
merged 1 commit into from
Nov 8, 2023

Conversation

brooksprumo
Copy link
Contributor

Problem

When working on switch a HashMap to an IntMap within shrink, the build failed. Here's what one of the compiler errors was:

error[E0599]: the method `contains` exists for struct `HashMap<u64, Arc<AccountStorageEntry>, BuildHasherDefault<NoHashHasher<u64>>>`, but its trait bounds were not satisfied
     --> accounts-db/src/accounts_db.rs:13530:45
      |
13530 |                 assert!(selected_candidates.contains(&slot2));
      |                                             ^^^^^^^^
      |
     ::: /Users/brooks/.rustup/toolchains/1.73.0-x86_64-apple-darwin/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:216:1
      |
216   | pub struct HashMap<K, V, S = RandomState> {
      | -----------------------------------------
      | |
      | doesn't satisfy `_: Contains<'_, HashMap<u64, Arc<AccountStorageEntry>, BuildHasherDefault<NoHashHasher<u64>>>>`
      | doesn't satisfy `_: Copy`
      | doesn't satisfy `_: Eq`
      | doesn't satisfy `_: Hash`
      | doesn't satisfy `_: Iterator`
      | doesn't satisfy `_: Itertools`
      |
note: the following trait bounds were not satisfied:
      `std::collections::HashMap<u64, std::sync::Arc<accounts_db::AccountStorageEntry>, BuildHasherDefault<NoHashHasher<u64>>>: std::cmp::Eq`
      `std::collections::HashMap<u64, std::sync::Arc<accounts_db::AccountStorageEntry>, BuildHasherDefault<NoHashHasher<u64>>>: std::hash::Hash`
      `std::collections::HashMap<u64, std::sync::Arc<accounts_db::AccountStorageEntry>, BuildHasherDefault<NoHashHasher<u64>>>: std::marker::Copy`
     --> accounts-db/src/contains.rs:39:18
      |
39    | impl<'a, T: 'a + Eq + Hash + Copy> Contains<'a, T> for T {
      |                  ^^   ^^^^   ^^^^  ---------------     -
      |                  |    |      |
      |                  |    |      unsatisfied trait bound introduced here
      |                  |    unsatisfied trait bound introduced here
      |                  unsatisfied trait bound introduced here
      = note: the following trait bounds were not satisfied:
              `std::collections::HashMap<u64, std::sync::Arc<accounts_db::AccountStorageEntry>, BuildHasherDefault<NoHashHasher<u64>>>: Iterator`
              which is required by `std::collections::HashMap<u64, std::sync::Arc<accounts_db::AccountStorageEntry>, BuildHasherDefault<NoHashHasher<u64>>>: itertools::Itertools`

Basically, the Contains trait wasn't matching the impl for HashMap. This is because the impl did not specify the HashMap's
S generic, so the default RandomState was used. Since IntMap uses a different S, this impl didn't match, and thus a fall-through to the impl on T, which failed with the above errors.

Since the HashMap/HashSet impls don't use S, we can widen the impls so that IntMap and IntSet work with Contains.

Summary of Changes

Specify S in the HashMap/HashSet impls of Contains.

@brooksprumo brooksprumo self-assigned this Nov 7, 2023
@brooksprumo brooksprumo marked this pull request as ready for review November 7, 2023 18:04
Copy link

codecov bot commented Nov 7, 2023

Codecov Report

Merging #33973 (7ac5bde) into master (b8115b4) will decrease coverage by 0.1%.
Report is 2 commits behind head on master.
The diff coverage is 100.0%.

@@            Coverage Diff            @@
##           master   #33973     +/-   ##
=========================================
- Coverage    81.9%    81.9%   -0.1%     
=========================================
  Files         811      811             
  Lines      219354   219354             
=========================================
- Hits       179699   179698      -1     
- Misses      39655    39656      +1     

@brooksprumo brooksprumo requested a review from HaoranYi November 8, 2023 14:27
Copy link
Contributor

@yhchiang-sol yhchiang-sol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG!

@brooksprumo brooksprumo merged commit 7cb83bc into solana-labs:master Nov 8, 2023
17 checks passed
@brooksprumo brooksprumo deleted the contains/impls branch November 8, 2023 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants