rust-lang / rust-lang/rust

Unhelpful error message on mismatch between expression type and type annotation with defaulted generic parameter

Open
#142,628 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use fxhash::FxBuildHasher;
use std::collections::HashMap;

pub static X: HashMap<(), ()> = HashMap::with_hasher(FxBuildHasher::new());

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=685acb633567db20ff95170b3221a1bb

Current output
error[E0308]: mismatched types
   --> src/lib.rs:4:54
    |
4   | pub static X: HashMap<(), ()> = HashMap::with_hasher(FxBuildHasher::new());
    |                                 -------------------- ^^^^^^^^^^^^^^^^^^^^ expected `RandomState`, found `BuildHasherDefault<FxHasher>`
    |                                 |
    |                                 arguments to this function are incorrect
    |
    = note: expected struct `RandomState`
               found struct `BuildHasherDefault<FxHasher>`
note: associated function defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:312:18
    |
312 |     pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
    |                  ^^^^^^^^^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output

Something that highlights the default value of HashMaps third generic parameter. I have barely ever used custom hashers so it wasn't top of mind that with_hasher resulted in a different return type.

Rationale and extra context

I've been writing Rust for many years, but it still took me a minute before it realized what was wrong; a clearer error message would have immediately made the problem obvious :)

Rust Version
rustc 1.89.0-nightly (45acf54ee 2025-06-16)
binary: rustc
commit-hash: 45acf54eea118ed27927282b5e0bfdcd80b7987c
commit-date: 2025-06-16
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
Anything else?

@rustbot label +D-terse

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic with the Rust Playground example and inspect the compiler path that emits E0308 for the HashMap::with_hasher call. The issue names no repository files or tests; done means the error clearly identifies the default third generic parameter and explains why the expression's hasher type does not match it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.