rust-lang / rust-lang/rust

`mismatched types` instead of "type does not implement required traits"

Open
#144,670 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
struct Foo;

impl Foo {
    fn heck<T>(&self, rel: T)
    where
        Lock: Key<T>,
    {
        self.with(Junk);
    }

    fn with<T>(&self, _id: T)
    where
        Lock: Key<T>,
    {
        todo!()
    }
}

struct Junk;

trait Key<T> {}

struct Lock {}

impl<T> Key<T> for Lock {}
Current output
error[E0308]: mismatched types
  --> toybox\src\mismatched_types.rs:8:19
   |
4  |     fn heck<T>(&self, rel: T)
   |             - expected this type parameter
...
8  |         self.with(Junk);
   |              ---- ^^^^ expected type parameter `T`, found `Junk`
   |              |
   |              arguments to this method are incorrect
   |
   = note: expected type parameter `T`
                      found struct `Junk`
note: method defined here
  --> toybox\src\mismatched_types.rs:11:8
   |
11 |     fn with<T>(&self, _id: T)
   |        ^^^^           ------
Desired output
The error message should state that `with` cannot be called because `Lock: Key<Junk>` is not satisfied.
Rationale and extra context

I was stuck on this error message for a long time.. The compiler seems to determine that the only possible type which satisfies all constraints is T and then quite determined states to change Junk to T. This feels backwards as usually error messages indicate that a function cannot be called because required traits are not implemented. At the very least it should be a suggestion: "Did you mean "T" as it is the only type which works here? Otherwise you have to add more trait bounds."

Other cases

Rust Version
rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-pc-windows-msvc
release: 1.88.0
LLVM version: 20.1.5
Anything else?

No response

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

No repository file or test is named. Start by reproducing the supplied example with rustc 1.88.0, then trace the compiler diagnostic path for the failed generic call and trait obligation. Done means the message explains that Lock: Key<Junk> is unsatisfied rather than reporting only a mismatched type, with regression coverage for this example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.