Skip to content

unrelated trait bound being reported as not satisfied #130949

Open
@vxpm

Description

@vxpm

Code

trait Foo {
    type Assoc;
}

trait Bar {}

impl<T> Foo for T where T: Bar {
    type Assoc = ();
}

struct Test {
    field: <() as Foo>::Assoc, 
}

Current output

error[E0277]: the trait bound `(): Bar` is not satisfied
  --> src/lib.rs:12:12
   |
12 |     field: <() as Foo>::Assoc, 
   |            ^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `()`, which is required by `(): Foo`
   |
help: this trait has no implementations, consider adding one
  --> src/lib.rs:5:1
   |
5  | trait Bar {}
   | ^^^^^^^^^
note: required for `()` to implement `Foo`
  --> src/lib.rs:7:9
   |
7  | impl<T> Foo for T where T: Bar {
   |         ^^^     ^          --- unsatisfied trait bound introduced here

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

error[E0277]: the trait bound `(): Foo` is not satisfied
  --> src/lib.rs:12:12
   |
12 |     field: <() as Foo>::Assoc, 
   |            ^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `()`
   |
help: this trait has no implementations, consider adding one
  --> src/lib.rs:5:1
   |
5  | trait Foo {
   | ^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 1 previous error

Rationale and extra context

the problem is exceptionally bad when the error arises in macro generated code, where the as Foo cannot be seen in the diagnostic. the real trait bound is also hidden further when using diagnostic::on_unimplemented:

#[diagnostic::on_unimplemented(message = "foo", label = "not foo")]
trait Foo {
    type Assoc;
}

#[diagnostic::on_unimplemented(message = "bar", label = "not bar")]
trait Bar {}

impl<T> Foo for T where T: Bar {
    type Assoc = ();
}

struct Test {
    field: <() as Foo>::Assoc, 
}

gives:

error[E0277]: bar
  --> src/lib.rs:14:12
   |
14 |     field: <() as Foo>::Assoc, 
   |            ^^^^^^^^^^^^^^^^^^ not bar
   |
   = help: the trait `Bar` is not implemented for `()`, which is required by `(): Foo`
help: this trait has no implementations, consider adding one
  --> src/lib.rs:7:1
   |
7  | trait Bar {}
   | ^^^^^^^^^
note: required for `()` to implement `Foo`
  --> src/lib.rs:9:9
   |
9  | impl<T> Foo for T where T: Bar {
   |         ^^^     ^          --- unsatisfied trait bound introduced here

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 1 previous error

Other cases

No response

Rust Version

latest stable and nightly, but noticed in my own machine using the following version:
rustc 1.83.0-nightly (6c6d210 2024-09-22)
binary: rustc
commit-hash: 6c6d210
commit-date: 2024-09-22
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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

    No branches or pull requests

    Issue actions