Skip to content

Unhelpful error message with duplicate names for struct and trait method #43626

Closed
@nikolads

Description

@nikolads

If there is a private method implemented for a struct and a method with the same name from a trait impl for the struct, but the trait is not in scope, then there is no help section in the error message suggesting to add a use for the trait.

I tried this code:

mod inner {
    pub trait Bar {
        fn method(&self);
    }

    pub struct Foo;
    
    impl Foo {
        fn method(&self) {}
    }
    
    impl Bar for Foo {
        fn method(&self) {}
    }
}

fn main() {
    let foo = inner::Foo;
    foo.method();
}

It fails with the following error:

error: method `method` is private

The error message can be improved by adding a section like this:

   = help: items from traits can only be used if the trait is in scope
   = note: the following trait is implemented but not in scope, perhaps add a `use` for it:
           candidate #1: `use inner::Bar;`

Meta

Tried on rustc versions 1.19.0 and 1.21.0-nightly

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions