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

E0277 message is misleading when there's a blanket impl #40120

Closed
aldanor opened this issue Feb 26, 2017 · 8 comments · Fixed by #120469
Closed

E0277 message is misleading when there's a blanket impl #40120

aldanor opened this issue Feb 26, 2017 · 8 comments · Fixed by #120469
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@aldanor
Copy link

aldanor commented Feb 26, 2017

trait Foo { fn foo() {} }
trait Bar {}
impl<T: Bar> Foo for T {}

fn main() {
    <i32 as Foo>::foo();
}

Reported error tries to be too smart:

error[E0277]: the trait bound `i32: Bar` is not satisfied
  |
  |     <i32 as Foo>::foo();
  |     ^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `i32`
  |
  = note: required because of the requirements on the impl of `Foo` for `i32`
  = note: required by `Foo::foo`

... but is misleading since the real error is that i32: Foo is not satisfied; i32 may happen to implement Foo and not implement Bar, so the error message is actually just wrong.

@aldanor
Copy link
Author

aldanor commented Feb 26, 2017

Hypothetical alternative error message:

error[E0277]: the trait bound `i32: Foo` is not satisfied
  |
  |     <i32 as Foo>::foo();
  |     ^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `i32`
  |
  = note: the trait bound could be satisfied by implementing `Bar` for `i32`
  = note: required by `Foo::foo`

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Mar 1, 2017
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

Current output:

error[E0277]: the trait bound `i32: Bar` is not satisfied
 --> src/main.rs:6:5
  |
1 | trait Foo { fn foo() {} }
  |             -------- required by `Foo::foo`
...
6 |     <i32 as Foo>::foo();
  |     ^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `i32`
  |
  = note: required because of the requirements on the impl of `Foo` for `i32`

It should be closer to

error[E0277]: the trait bound `i32: Bar` is not satisfied
 --> src/main.rs:6:5
  |
3 | impl<T: Bar> Foo for T {}
  |      ------ required by this bound
...
6 |     <i32 as Foo>::foo();
  |     ^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `i32`
  |
  = note: required because of the requirements on the impl of `Foo` for `i32`

@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Jan 25, 2020
@jplatte
Copy link
Contributor

jplatte commented Jul 7, 2020

This should be tagged A-traits, no?

@estebank estebank added the A-traits Area: Trait system label Jul 7, 2020
@estebank
Copy link
Contributor

estebank commented Jan 5, 2023

Current output:

error[E0277]: the trait bound `i32: Bar` is not satisfied
 --> src/main.rs:6:5
  |
6 |     <i32 as Foo>::foo();
  |     ^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `i32`
  |
note: required for `i32` to implement `Foo`
 --> src/main.rs:3:14
  |
3 | impl<T: Bar> Foo for T {}
  |              ^^^     ^

@estebank estebank added the D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. label Jan 5, 2023
@estebank
Copy link
Contributor

estebank commented Oct 3, 2023

Current output:

error[E0277]: the trait bound `i32: Bar` is not satisfied
 --> src/main.rs:6:6
  |
6 |     <i32 as Foo>::foo();
  |      ^^^ the trait `Bar` is not implemented for `i32`
  |
help: this trait has no implementations, consider adding one
 --> src/main.rs:2:1
  |
2 | trait Bar {}
  | ^^^^^^^^^
note: required for `i32` to implement `Foo`
 --> src/main.rs:3:14
  |
3 | impl<T: Bar> Foo for T {}
  |         ---  ^^^     ^
  |         |
  |         unsatisfied trait bound introduced here

@estebank
Copy link
Contributor

I believe the current output is good enough to explain what's going on, pointing at the blanket impl that matches but that introduces an unmet requirement.

@jplatte
Copy link
Contributor

jplatte commented Nov 21, 2023

I don't like that the current output still mentions Bar first, as if the user had written <i32 as Bar>. IMHO the output should be something along the lines of

error[E0277]: the trait bound `i32: Foo` is not satisfied
<snippet>
note: there is a generic implementation, but `i32` does not satisfy it
<snippet>

@estebank
Copy link
Contributor

Sure, we could potentially try to do that.

@estebank estebank reopened this Nov 21, 2023
@estebank estebank removed D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Nov 21, 2023
estebank added a commit to estebank/rust that referenced this issue Jan 29, 2024
Expand the primary span of E0277 when the immediate unmet bound is not what the user wrote:

```
error[E0277]: the trait bound `i32: Bar` is not satisfied
 --> f100.rs:6:6
  |
6 |     <i32 as Foo>::foo();
  |      ^^^ the trait `Bar` is not implemented for `i32`, which is required by `i32: Foo`
  |
help: this trait has no implementations, consider adding one
 --> f100.rs:2:1
  |
2 | trait Bar {}
  | ^^^^^^^^^
note: required for `i32` to implement `Foo`
 --> f100.rs:3:14
  |
3 | impl<T: Bar> Foo for T {}
  |         ---  ^^^     ^
  |         |
  |         unsatisfied trait bound introduced here
```

Fix rust-lang#40120.
Nadrieril added a commit to Nadrieril/rust that referenced this issue Jan 31, 2024
Provide more context on derived obligation error primary label

Expand the primary span of E0277 when the immediate unmet bound is not what the user wrote:

```
error[E0277]: the trait bound `i32: Bar` is not satisfied
 --> f100.rs:6:6
  |
6 |     <i32 as Foo>::foo();
  |      ^^^ the trait `Bar` is not implemented for `i32`, which is required by `i32: Foo`
  |
help: this trait has no implementations, consider adding one
 --> f100.rs:2:1
  |
2 | trait Bar {}
  | ^^^^^^^^^
note: required for `i32` to implement `Foo`
 --> f100.rs:3:14
  |
3 | impl<T: Bar> Foo for T {}
  |         ---  ^^^     ^
  |         |
  |         unsatisfied trait bound introduced here
```

Fix rust-lang#40120.
Nadrieril added a commit to Nadrieril/rust that referenced this issue Jan 31, 2024
Provide more context on derived obligation error primary label

Expand the primary span of E0277 when the immediate unmet bound is not what the user wrote:

```
error[E0277]: the trait bound `i32: Bar` is not satisfied
 --> f100.rs:6:6
  |
6 |     <i32 as Foo>::foo();
  |      ^^^ the trait `Bar` is not implemented for `i32`, which is required by `i32: Foo`
  |
help: this trait has no implementations, consider adding one
 --> f100.rs:2:1
  |
2 | trait Bar {}
  | ^^^^^^^^^
note: required for `i32` to implement `Foo`
 --> f100.rs:3:14
  |
3 | impl<T: Bar> Foo for T {}
  |         ---  ^^^     ^
  |         |
  |         unsatisfied trait bound introduced here
```

Fix rust-lang#40120.
@bors bors closed this as completed in 6efddac Jan 31, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 31, 2024
Rollup merge of rust-lang#120469 - estebank:issue-40120, r=TaKO8Ki

Provide more context on derived obligation error primary label

Expand the primary span of E0277 when the immediate unmet bound is not what the user wrote:

```
error[E0277]: the trait bound `i32: Bar` is not satisfied
 --> f100.rs:6:6
  |
6 |     <i32 as Foo>::foo();
  |      ^^^ the trait `Bar` is not implemented for `i32`, which is required by `i32: Foo`
  |
help: this trait has no implementations, consider adding one
 --> f100.rs:2:1
  |
2 | trait Bar {}
  | ^^^^^^^^^
note: required for `i32` to implement `Foo`
 --> f100.rs:3:14
  |
3 | impl<T: Bar> Foo for T {}
  |         ---  ^^^     ^
  |         |
  |         unsatisfied trait bound introduced here
```

Fix rust-lang#40120.
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. 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.

5 participants