Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lolbinarycat opened this issue Nov 16, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lolbinarycat
Copy link
Contributor

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

@lolbinarycat lolbinarycat added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-trait-system Area: Trait system labels Nov 16, 2024
@lolbinarycat lolbinarycat added the E-help-wanted Call for participation: Help is requested to fix this issue. label Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant