Skip to content

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

@aldanor

Description

@aldanor
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.

Activity

aldanor

aldanor commented on Feb 26, 2017

@aldanor
Author

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`
added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 9, 2017
estebank

estebank commented on Jan 25, 2020

@estebank
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`
added
D-confusingDiagnostics: Confusing error or lint that should be reworked.
on Jan 25, 2020
jplatte

jplatte commented on Jul 7, 2020

@jplatte
Contributor

This should be tagged A-traits, no?

estebank

estebank commented on Jan 5, 2023

@estebank
Contributor

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 {}
  |              ^^^     ^
added
D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
on Jan 5, 2023
estebank

estebank commented on Oct 3, 2023

@estebank
Contributor

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

estebank commented on Oct 19, 2023

@estebank
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

jplatte commented on Nov 21, 2023

@jplatte
Contributor

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

estebank commented on Nov 21, 2023

@estebank
Contributor

Sure, we could potentially try to do that.

removed
D-confusingDiagnostics: Confusing error or lint that should be reworked.
D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
on Nov 21, 2023
added a commit that references this issue on Jan 29, 2024
19728a9
added a commit that references this issue on Jan 30, 2024
6efddac
added 2 commits that reference this issue on Jan 31, 2024
ef9dd13
0313eb2
added a commit that references this issue on Jan 31, 2024
3ef5291
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @steveklabnik@jplatte@estebank@aldanor@Mark-Simulacrum

      Issue actions

        E0277 message is misleading when there's a blanket impl · Issue #40120 · rust-lang/rust