Skip to content

add a note if a type implements a trait with the same name as the required trait #133123

@lolbinarycat

Description

@lolbinarycat

Code

trait Trait {}

mod m {
    pub trait Trait {}
    pub struct St;
    impl Trait for St {}
}

fn func<T: Trait>(_: T) {}

fn main() {
    func(m::St);
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `St: Trait` is not satisfied
  --> src/main.rs:12:10
   |
12 |     func(m::St);
   |     ---- ^^^^^ the trait `Trait` is not implemented for `St`
   |     |
   |     required by a bound introduced by this call
   |
help: this trait has no implementations, consider adding one
  --> src/main.rs:1:1
   |
1  | trait Trait {}
   | ^^^^^^^^^^^
note: required by a bound in `func`
  --> src/main.rs:9:12
   |
9  | fn func<T: Trait>(_: T) {}
   |            ^^^^^ required by this bound in `func`

Desired output

Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `St: Trait` is not satisfied
  --> src/main.rs:12:10
   |
12 |     func(m::St);
   |     ---- ^^^^^ the trait `Trait` is not implemented for `St`
   |     |
   |     required by a bound introduced by this call
   |
note: `St` implements similarly named `crate::m::Trait`, but not `crate::Trait`
help: this trait has no implementations, consider adding one
  --> src/main.rs:1:1
   |
1  | trait Trait {}
   | ^^^^^^^^^^^
note: required by a bound in `func`
  --> src/main.rs:9:12
   |
9  | fn func<T: Trait>(_: T) {}
   |            ^^^^^ required by this bound in `func`

Rationale and extra context

This can easily happen if you accidentally have two different versions of the same crate in your dependency tree (see this URLO thread), or have two dependencies that use different trait for the same thing (eg. tokio::io::AsyncRead and futures_io::AsyncRead)

Other cases

Should also probably trigger if the implemented trait has a small enough edit distance between it and the desired trait, similar to the help messages for misspelled methods and types.

Rust Version

1.82.0

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemE-help-wantedCall for participation: Help is requested to fix this issue.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