Skip to content

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

Open
@jplatte

Description

@jplatte

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions