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

Confusing notes with per-method where-bounds when bounds are not satisfied #75222

Open
nagisa opened this issue Aug 6, 2020 · 1 comment
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nagisa
Copy link
Member

nagisa commented Aug 6, 2020

The following code:

trait Fruit {
    type Error;
}

struct Peach;
impl Fruit for Peach {
    type Error = ();
}

struct Banana<F>(F);
impl<F> Banana<F> {
    fn peel(&self) -> Result<(), F::Error> where F: Fruit {
        unimplemented!()
    }
}

fn pineapple() -> Result<(), ()> {
    let banana: Banana<&Peach> = Banana(&Peach);
    banana.peel()?;
    Ok(())
}

playground

outputs the following error:

error[E0599]: no method named `peel` found for struct `Banana<&Peach>` in the current scope
  --> src/lib.rs:19:12
   |
10 | struct Banana<F>(F);
   | -------------------- method `peel` not found for this
...
19 |     banana.peel()?;
   |            ^^^^ method not found in `Banana<&Peach>`
   |
   = note: the method `peel` exists but the following trait bounds were not satisfied:
           `<&Peach as Fruit>::Error = _`

The note in this error is confusing as it only leads further away from the solution and is also difficult to read. In particular it is talking about trait bounds, but the <&Peach as Fruit>::Error = _ presented is not even a trait bound. The real problem here is that F: Fruit is not satisfied.

The error message improves significantly when the bound is moved to the impl generic instead as such:

impl<F: Fruit> Banana<F> {
    fn peel(&self) -> Result<(), F::Error> {
        unimplemented!()
    }
}

We should strive for a similar note when the where-bound is applied to method like in the original example.

@nagisa nagisa added A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system C-bug Category: This is a bug. labels Aug 6, 2020
@nagisa nagisa changed the title Confusing type hints with per-method where-bounds Confusing notes with per-method where-bounds when bounds are not satisfied Aug 6, 2020
@JohnTitor JohnTitor added D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Aug 7, 2020
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

error[E0599]: the method `peel` exists for struct `Banana<&Peach>`, but its trait bounds were not satisfied
  --> src/lib.rs:19:12
   |
10 | struct Banana<F>(F);
   | ---------------- method `peel` not found for this struct
...
19 |     banana.peel()?;
   |            ^^^^ method cannot be called on `Banana<&Peach>` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `&Peach: Fruit`

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

3 participants