Skip to content

Improve E0599 for missing trait bounds (where clauses) #61661

Closed
@djc

Description

@djc

For the following example:

struct Foo<T> {
    t: T,
}

struct Inner {
    i: u8,
}

trait Bar {
    fn bar(&self);
}

impl<T> Bar for Foo<T> where T: Clone {
    fn bar(&self) {
        println!("x");
    }
}

fn main() {
    let foo = Foo { t: Inner { i: b'i' } };
    foo.bar();
}

The error message is:

error[E0599]: no method named `bar` found for type `Foo<Inner>` in the current scope
  --> src/main.rs:21:9
   |
1  | struct Foo<T> {
   | ------------- method `bar` not found for this
...
21 |     foo.bar();
   |         ^^^
   |
   = note: the method `bar` exists but the following trait bounds were not satisfied:
           `Foo<Inner> : Bar`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `bar`, perhaps you need to implement it:
           candidate #1: `Bar`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Foo<T> does actually implement Bar, and this makes the suggestions from the compiler misleading in this case: there is already an implementation for Foo<T> : Bar, and Bar is already in scope. It would be much more helpful if the compiler could suggest the trait bounds necessary to satisfy the requirement.

This seems very similar to what @estebank suggested in #9082 (comment).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-confusingDiagnostics: Confusing error or lint that should be reworked.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