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

Suggest impl<T: Bound> Trait<T> for Foo when finding impl Trait<T: Bound> for Foo #113073

Closed
TheOnlyMrCat opened this issue Jun 26, 2023 · 1 comment · Fixed by #126054
Closed
Assignees
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

@TheOnlyMrCat
Copy link

TheOnlyMrCat commented Jun 26, 2023

Code

trait Foo<T> {}
impl Foo<T: Default> for u32 {}

Current output

error[E0658]: associated type bounds are unstable
 --> src/lib.rs:2:10
  |
2 | impl Foo<T: Default> for u32 {}
  |          ^^^^^^^^^^
  |
  = note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information

error[E0107]: trait takes 1 generic argument but 0 generic arguments were supplied
 --> src/lib.rs:2:6
  |
2 | impl Foo<T: Default> for u32 {}
  |      ^^^ expected 1 generic argument
  |
note: trait defined here, with 1 generic parameter: `T`
 --> src/lib.rs:1:7
  |
1 | trait Foo<T> {}
  |       ^^^ -
help: add missing generic argument
  |
2 | impl Foo<T, T: Default> for u32 {}
  |          ++

error[E0229]: associated type bindings are not allowed here
 --> src/lib.rs:2:10
  |
2 | impl Foo<T: Default> for u32 {}
  |          ^^^^^^^^^^ associated type not allowed here

Desired output

error[E0658]: associated type bounds are unstable
 --> src/lib.rs:2:10
  |
2 | impl Foo<T: Default> for u32 {}
  |          ^^^^^^^^^^
  |
  = note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information

error[E0229]: associated type bindings are not allowed here
 --> src/lib.rs:2:10
  |
2 | impl Foo<T: Default> for u32 {}
  |          ^^^^^^^^^^ associated type not allowed here
help: declare the type parameter first
  |
2 | impl<T: Default> Foo<T> for u32 {}
  |     ++++++++++++

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Tasks

No tasks being tracked yet.
@TheOnlyMrCat TheOnlyMrCat 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 Jun 26, 2023
@veera-sivarajan
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in bfe0323 Jun 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 14, 2024
Rollup merge of rust-lang#126054 - veera-sivarajan:bugfix-113073-bound-on-generics-2, r=fee1-dead

`E0229`: Suggest Moving Type Constraints to Type Parameter Declaration

Fixes rust-lang#113073

This PR suggests  `impl<T: Bound> Trait<T> for Foo` when finding `impl Trait<T: Bound> for Foo`. Tangentially, it also improves a handful of other error messages.

It accomplishes this in two steps:
1. Check if constrained arguments and parameter names appear in the same order and delay emitting "incorrect number of generic arguments" error because it can be confusing for the programmer to see `0 generic arguments provided` when there are `n` constrained generic arguments.

2. Inside `E0229`, suggest declaring the type parameter right after the `impl` keyword by finding the relevant impl block's span for type parameter declaration. This also handles lifetime declarations correctly.

Also, the multi part suggestion doesn't use the fluent error mechanism because translating all the errors to fluent style feels outside the scope of this PR. I will handle it in a separate PR if this gets approved.
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

Successfully merging a pull request may close this issue.

2 participants