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

Incorrect suggestion to define associated type in type parameter list #116464

Closed
detly opened this issue Oct 6, 2023 · 1 comment · Fixed by #116553
Closed

Incorrect suggestion to define associated type in type parameter list #116464

detly opened this issue Oct 6, 2023 · 1 comment · Fixed by #116553
Assignees
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@detly
Copy link

detly commented Oct 6, 2023

Code

pub trait One<T> {
    type Assoc;
}

impl<T, S> One<T, S> for () {}

Current output

error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied
 --> src/lib.rs:5:12
  |
5 | impl<T, S> One<T, S> for () {}
  |            ^^^ expected 1 generic argument
  |
note: trait defined here, with 1 generic parameter: `T`
 --> src/lib.rs:1:11
  |
1 | pub trait One<T> {
  |           ^^^ -
help: replace the generic bound with the associated type
  |
5 | impl<T, S> One<T, Assoc = S> for () {}
  |                   +++++++

For more information about this error, try `rustc --explain E0107`.

Desired output

error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied
 --> src/lib.rs:5:12
  |
5 | impl<T, S> One<T, S> for () {}
  |            ^^^ expected 1 generic argument
  |
note: trait defined here, with 1 generic parameter: `T`
 --> src/lib.rs:1:11
  |
1 | pub trait One<T> {
  |           ^^^ -
help: define the associated type in the implementation body
  |
5 | impl<T> One<T> for () {
  |     type Assoc = SomeType;
  |     ++++++++++++++++++++++
  | }

For more information about this error, try `rustc --explain E0107`.

Rationale and extra context

The suggestion is not valid syntax. Applying the original suggestion like so:

pub trait One<T> {
    type Assoc;
}

impl<T, S> One<T, Assoc = S> for () {}

...leads to this error instead:

 --> src/lib.rs:5:19
  |
5 | impl<T, S> One<T, Assoc = S> for () {}
  |                   ^^^^^^^^^ associated type not allowed here

For more information about this error, try `rustc --explain E0229`.

Note also that a more naive suggestion to keep the S parameter like so:

pub trait One<T> {
    type Assoc;
}

impl<T, S> One<T> for () {
    type Assoc = S;
}

...would fail because it's unconstrained:

error[E0207]: the type parameter `S` is not constrained by the impl trait, self type, or predicates
 --> src/lib.rs:5:9
  |
5 | impl<T, S> One<T> for () {
  |         ^ unconstrained type parameter

For more information about this error, try `rustc --explain E0207`.

Assoc needs to be a concrete type or an associated type of an input type parameter.

The diagnostic is identical in beta and nightly.

@detly detly 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 Oct 6, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 6, 2023
@fmease fmease added A-associated-items Area: Associated items (types, constants & functions) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 6, 2023
@gurry
Copy link
Contributor

gurry commented Oct 9, 2023

@rustbot claim

@bors bors closed this as completed in 824dbb5 Oct 26, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 26, 2023
Rollup merge of rust-lang#116553 - gurry:116464-assoc-type-invalid-suggestion, r=compiler-errors

Do not suggest 'Trait<Assoc=arg>' when in trait impl

Fixes rust-lang#116464

We now skip the suggestion if we're in an impl of the trait.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

4 participants