rust-lang / rust-lang/rust

unrelated trait bound being reported as not satisfied

Open
#130,949 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 (6c6d21008 2024-09-22)
binary: rustc
commit-hash: 6c6d210089e4589afee37271862b9f88ba1d7755
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Rust code example on the listed stable or nightly versions and compare the current and desired diagnostics. Trace the compiler's trait-bound diagnostic handling to ensure the associated-type lookup reports the missing Foo implementation rather than the Bar obligation, including the on_unimplemented case; done when both examples produce the requested output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.