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

Error suggests implementing private trait instead of importing it. #57457

Closed
pierzchalski opened this issue Jan 8, 2019 · 2 comments · Fixed by #116862
Closed

Error suggests implementing private trait instead of importing it. #57457

pierzchalski opened this issue Jan 8, 2019 · 2 comments · Fixed by #116862
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system A-visibility Area: Visibility / privacy. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pierzchalski
Copy link
Contributor

If I implement a private trait on a public struct, then use a trait method elsewhere, the diagnostic message suggests implementing the already implemented private trait instead of making the trait public and importing it. For example, this code:

mod m {
    trait T {
        fn f(&self);
    }
    
    pub struct S;
    
    impl T for S {
        fn f(&self) {}
    }
}

fn main() {
    let s = m::S;
    s.f();
}

Results in this diagnostic:

error[E0599]: no method named `f` found for type `m::S` in the current scope
  --> src/main.rs:15:7
   |
6  |     pub struct S;
   |     ------------- method `f` not found for this
...
15 |     s.f();
   |       ^
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `f`, perhaps you need to implement it:
           candidate #1: `m::T`

The note should suggest making m::T public and useing it at the top level.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 9, 2019
@estebank estebank added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jan 6, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 27, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 28, 2020
bors added a commit that referenced this issue Feb 29, 2020
Add more context to E0599 errors

Point at the intermediary unfulfilled trait bounds.

Fix #52523, fix #61661, cc #36513, fix #68131, fix #64417, fix #61768, cc #57457, cc #9082, fix #57994, cc #64934, cc #65149.
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@estebank
Copy link
Contributor

Current output:

error[E0599]: no method named `f` found for struct `S` in the current scope
  --> src/main.rs:15:7
   |
6  |     pub struct S;
   |     ------------- method `f` not found for this
...
15 |     s.f();
   |       ^ method not found in `S`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `T` defines an item `f`, perhaps you need to implement it
  --> src/main.rs:2:5
   |
2  |     trait T {
   |     ^^^^^^^

We still don't check for accessibility.

@estebank estebank added A-traits Area: Trait system A-visibility Area: Visibility / privacy. labels Sep 10, 2021
@estebank
Copy link
Contributor

Triage: no change

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 30, 2023
Detect when trait is implemented for type and suggest importing it

Fix rust-lang#57457.
@bors bors closed this as completed in 162443b Oct 30, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 30, 2023
Rollup merge of rust-lang#116862 - estebank:issue-57457, r=oli-obk

Detect when trait is implemented for type and suggest importing it

Fix rust-lang#57457.
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-traits Area: Trait system A-visibility Area: Visibility / privacy. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants