Skip to content

unrelated trait bound being reported as not satisfied #130949

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

Open
vxpm opened this issue Sep 27, 2024 · 1 comment
Open

unrelated trait bound being reported as not satisfied #130949

vxpm opened this issue Sep 27, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@vxpm
Copy link
Contributor

vxpm commented Sep 27, 2024

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

@vxpm vxpm added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 27, 2024
@vxpm
Copy link
Contributor Author

vxpm commented Oct 10, 2024

stumbled upon #51992, which seems made to solve exactly this kind of issue.

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

1 participant