Skip to content

Compiler error messages leads to wrong usage of const generics #91119

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
matheus-consoli opened this issue Nov 22, 2021 · 4 comments
Open

Compiler error messages leads to wrong usage of const generics #91119

matheus-consoli opened this issue Nov 22, 2021 · 4 comments
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

@matheus-consoli
Copy link

matheus-consoli commented Nov 22, 2021

Hi, folks!

Misusing const generics generates errors messages that lead the user to a wrong usage of the feature, let's follow an example:

Given this very simple (and wrong) definition:

playground

struct Foo<const AHA: usize>{}

impl Foo {}

The compiler generates this error message:

error[E0107]: missing generics for struct `Foo`
 --> src/lib.rs:3:6
  |
3 | impl Foo {}
  |      ^^^ expected 1 generic argument
  |
note: struct defined here, with 1 generic parameter: `AHA`
 --> src/lib.rs:1:8
  |
1 | struct Foo<const AHA: usize>{}
  |        ^^^       ---
help: add missing generic argument
  |
3 | impl Foo<AHA> {}
  |      ~~~~~~~~

Following the compiler suggestion:

struct Foo<const AHA: usize>{}

impl Foo<AHA> {}

We have a new error:

error[E0412]: cannot find type `AHA` in this scope
 --> src/lib.rs:3:10
  |
3 | impl Foo<AHA> {}
  |     -    ^^^ not found in this scope
  |     |
  |     help: you might be missing a type parameter: `<AHA>`

error[E0747]: unresolved item provided when a constant was expected
 --> src/lib.rs:3:10
  |
3 | impl Foo<AHA> {}
  |          ^^^
  |
help: if this generic argument was intended as a const parameter, surround it with braces
  |
3 | impl Foo<{ AHA }> {}
  |          +     +

Applying the suggestion:

struct Foo<const AHA: usize>{}

impl Foo<{ AHA }> {}

Leads to a new error:

error[E0425]: cannot find value `AHA` in this scope
 --> src/lib.rs:3:12
  |
3 | impl Foo<{ AHA }> {}
  |     -      ^^^ not found in this scope
  |     |
  |     help: you might be missing a type parameter: `<AHA>`

And applying again:

struct Foo<const AHA: usize>{}

impl<AHA> Foo<{ AHA }> {}

Leads the user to a not intuitive point:

error[E0423]: expected value, found type parameter `AHA`
 --> src/lib.rs:3:17
  |
3 | impl<AHA> Foo<{ AHA }> {}
  |                 ^^^ not a value

error[E0207]: the type parameter `AHA` is not constrained by the impl trait, self type, or predicates
 --> src/lib.rs:3:6
  |
3 | impl<AHA> Foo<{ AHA }> {}
  |      ^^^ unconstrained type parameter

Ideally, we should not guide the user to such a long and erroneous path, and give them the correct answer that may look like this:

error[E???]: missing const generics for struct `Foo`
 --> src/lib.rs:3:6
  |
3 | impl Foo {}
  |      ^^^ expected 1 const generic argument
  |
note: struct defined here, with 1 const generic parameter: `AHA`
 --> src/lib.rs:1:8
  |
1 | struct Foo<const AHA: usize>{}
  |        ^^^       ---
help: add missing generic argument
  |
3 | impl<const AHA: usize> Foo<AHA> {}
  |      ~~~~~~~~
@matheus-consoli matheus-consoli 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 Nov 22, 2021
@matheus-consoli
Copy link
Author

Also, I would love to help fix this, with some guidance

@estebank
Copy link
Contributor

You'll want to look at this method

fn suggest_adding_type_and_const_args(&self, err: &mut DiagnosticBuilder<'_>) {

and potentially to this PR for some inspiration on parts of this #85346.

I'll be trying to land that PR shortly.

@matheus-consoli
Copy link
Author

@rustbot claim

@inquisitivecrystal inquisitivecrystal added A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Nov 30, 2021
@estebank
Copy link
Contributor

estebank commented Jan 8, 2023

Triage: no change.

bors added a commit to rust-lang-ci/rust that referenced this issue Apr 9, 2023
… r=compiler-errors

Suggest defining const parameter when appropriate

Helps a bit with rust-lang#91119.
Following rust-lang#105523's lead, I use placeholder `/* Type */` instead of `_` in the suggestion.
It should be easier for newcomers to parse.

`@rustbot` label A-diagnostics
r? diagnostics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

No branches or pull requests

3 participants