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

In impl trait, E0229 "associated type bindings are not allowed here" could suggest a solution or at least be less confusing #122162

Closed
marshrayms opened this issue Mar 8, 2024 · 0 comments · Fixed by #122591
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@marshrayms
Copy link

Code

struct S;
struct T;

// error[E0229]: associated type bindings are not allowed here
// --> src/lib.rs:9:26
//  |
//9 | impl std::cmp::PartialEq<Rhs = T> for S {
//  |                          ^^^^^^^ associated type not allowed here
impl std::cmp::PartialEq<Rhs = T> for S {
    fn eq(&self, _other: &T) -> bool {
        true
    }
}

// Works
/*impl std::cmp::PartialEq<T> for S {
    fn eq(&self, _other: &T) -> bool {
        true
    }
}*/

Current output

error[E0229]: associated type bindings are not allowed here
9 | impl std::cmp::PartialEq<Rhs = T> for S {
  |                          ^^^^^^^ associated type not allowed here

Desired output

error[E0229]: associated type bindings are not allowed here
9 | impl std::cmp::PartialEq<Rhs = T> for S {
  |                          ^^^^^^^ associated type not allowed here,
  | `std::cmp::PartialEq` has a generic parameter, not an associated type, so the `Rhs =` syntax is not appropriate to use here.
  | Did you mean `impl std::cmp::PartialEq<T>`?

Rationale and extra context

I have been writing Rust for years and still get the syntax for trait generic type params and trait associated types mixed up. In this case, I copy-and-pasted the trait signature from the documentation and put impl in front of it.

In my mind, I never had any intent to bind an associated type. My goal was to impl PartialEq.

I now see my mistake, but in this case the error messages did not help me get there and perhaps could be improved.

Other cases

No response

Rust Version

1.76.0 Stable
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=64a8a896091da005a5a8ddfce5b5ea77

Anything else?

No response

@marshrayms marshrayms 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 Mar 8, 2024
@jieyouxu jieyouxu added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Mar 8, 2024
@bors bors closed this as completed in 68939f7 Apr 23, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 23, 2024
Rollup merge of rust-lang#122591 - gurry:122162-impl-type-binding-suggestion, r=fmease

Suggest using type args directly instead of equality constraint

When type arguments are written erroneously using an equality constraint we suggest specifying them directly without the equality constraint.

Fixes rust-lang#122162

Changes the diagnostic in the issue from:
```rust
error[E0229]: associated type bindings are not allowed here
9 | impl std::cmp::PartialEq<Rhs = T> for S {
  |                          ^^^^^^^ associated type not allowed here
  |
```
to
```rust
error[E0229]: associated type bindings are not allowed here
9 | impl std::cmp::PartialEq<Rhs = T> for S {
  |                          ^^^^^^^ associated type not allowed here
  |
help: to use `T` as a generic argument specify it directly
  |
  |      impl std::cmp::PartialEq<T> for S {
  |                               ~
```
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 D-confusing Diagnostics: Confusing error or lint that should be reworked. 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.

2 participants