Skip to content

mismatched types instead of "type does not implement required traits" #144670

@Danvil

Description

@Danvil

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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