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

Avoid 'not found' terminology when there is only one potentially applicable method. #76267

Closed
Aaron1011 opened this issue Sep 3, 2020 · 0 comments · Fixed by #81149
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked.

Comments

@Aaron1011
Copy link
Member

The following code:

struct Foo<T>(T);
trait MyTrait {
    fn my_fn(&self) {}
}
impl<T: Copy> MyTrait for Foo<T> {}

fn main() {
    Foo(String::new()).my_fn();
}

gives the following error:

error[E0599]: no method named `my_fn` found for struct `Foo<std::string::String>` in the current scope
   --> src/main.rs:8:24
    |
1   |   struct Foo<T>(T);
    |   -----------------
    |   |
    |   method `my_fn` not found for this
    |   doesn't satisfy `Foo<std::string::String>: MyTrait`
...
8   |       Foo(String::new()).my_fn();
    |                          ^^^^^ method not found in `Foo<std::string::String>`
    |
    = note: the method `my_fn` exists but the following trait bounds were not satisfied:
            `std::string::String: std::marker::Copy`
            which is required by `Foo<std::string::String>: MyTrait`

If we change the call to my_fn() to call some completely nonexistent method (e.g. missing_method()), we get the following error.

   Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `missing_method` found for struct `Foo<std::string::String>` in the current scope
 --> src/main.rs:8:24
  |
1 | struct Foo<T>(T);
  | ----------------- method `missing_method` not found for this
...
8 |     Foo(String::new()).missing_method();
  |                        ^^^^^^^^^^^^^^ method not found in `Foo<std::string::String>`

These two error messages are quite similar - however, they really mean two completely different things. The first error is due to a trait bound error - there's only one in-scope method that the user could possibly be trying to call, but the receiver is not valid. The second error is caused by there being no methods that could even possibly apply - other than suggesting similarly-named methods or additional trait imports, there's really nothing else for the error message to say.

I think using the 'no method found' terminology is the first case is very misleading. It requires the user to read further into the error message to determine if the receiver is invalid, or if they made a typo/tried to call a method that was removed in a newer release of a crate.

I propose that we move the "method foo exists but ..." note to become the main error message. In the case of multiple possibilities, we could say something like "cannot call method foo on base type bar due to ...".

@Aaron1011 Aaron1011 added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Sep 3, 2020
@Aaron1011 Aaron1011 changed the title Avoid 'not found' terminology when there is only one potentialy applicable method. Avoid 'not found' terminology when there is only one potentially applicable method. Oct 10, 2020
Aaron1011 added a commit to Aaron1011/rust that referenced this issue Jan 18, 2021
Fixes rust-lang#76267

When there is a single applicable method candidate, but its trait bounds
are not satisfied, we avoid saying that the method is "not found".
Insted, we update the error message to directly mention which bounds are
not satisfied, rather than mentioning them in a note.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jan 27, 2021
…found-err, r=estebank

Avoid describing a method as 'not found' when bounds are unsatisfied

Fixes rust-lang#76267

When there is a single applicable method candidate, but its trait bounds
are not satisfied, we avoid saying that the method is "not found".
Insted, we update the error message to directly mention which bounds are
not satisfied, rather than mentioning them in a note.
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 28, 2021
…und-err, r=estebank

Avoid describing a method as 'not found' when bounds are unsatisfied

Fixes rust-lang#76267

When there is a single applicable method candidate, but its trait bounds
are not satisfied, we avoid saying that the method is "not found".
Insted, we update the error message to directly mention which bounds are
not satisfied, rather than mentioning them in a note.
@bors bors closed this as completed in dea8a16 Jan 28, 2021
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant