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 const parameter declaration on an impl block yields an uninformative syntax error #84946

Closed
PatchMixolydic opened this issue May 5, 2021 · 0 comments · Fixed by #85346
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. 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

@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented May 5, 2021

Given the following code (playground):

struct NInts<const N: usize>([u8; N]);
impl NInts<const N: usize> {}

The current output is fairly uninformative:

error: expected one of `>`, a const expression, lifetime, or type, found keyword `const`
 --> src/lib.rs:2:11
  |
2 | impl NInts<const N: usize> {}
  |           ^^^^^ expected one of `>`, a const expression, lifetime, or type

Ideally the output should look something like this (perhaps with slightly clearer wording 😅):

error: generic parameter declarations on `impl` blocks must be attached to `impl`
 --> src/lib.rs:2:11
  |
2 | impl NInts<const N: usize> {}
  |            ^^^^^^^^^^^^^^ cannot declare a const parameter here
help: move the generic parameter declaration
  |
2 | impl<const N: usize> NInts<N> {}
  |     ----------------       -

@PatchMixolydic PatchMixolydic 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 May 5, 2021
@PatchMixolydic PatchMixolydic changed the title Incorrect const generic declaration on impl block yields an uninformative syntax error Incorrect const parameter declaration on impl block yields an uninformative syntax error May 5, 2021
@PatchMixolydic PatchMixolydic changed the title Incorrect const parameter declaration on impl block yields an uninformative syntax error Incorrect const parameter declaration on an impl block yields an uninformative syntax error May 5, 2021
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST. D-confusing Diagnostics: Confusing error or lint that should be reworked. A-const-generics Area: const generics (parameters and arguments) labels May 7, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue May 13, 2021
Suggest adding a type parameter for impls

Add a new suggestion upon encountering an unknown type in a `impl` that suggests adding a new type parameter. This diagnostic suggests to add a new type parameter even though it may be a const parameter, however after adding the parameter and running rustc again a follow up error steers the user to change the type parameter to a const parameter.

```rust
struct X<const C: ()>();
impl X<C> {}
```
suggests
```
error[E0412]: cannot find type `C` in this scope
 --> bar.rs:2:8
  |
1 | struct X<const C: ()>();
  | ------------------------ similarly named struct `X` defined here
2 | impl X<C> {}
  |        ^
  |
help: a struct with a similar name exists
  |
2 | impl X<X> {}
  |        ^
help: you might be missing a type parameter
  |
2 | impl<C> X<C> {}
  |     ^^^
```
After adding a type parameter the code now becomes
```rust
struct X<const C: ()>();
impl<C> X<C> {}
```
and the error now fully steers the user towards the correct code
```
error[E0747]: type provided when a constant was expected
 --> bar.rs:2:11
  |
2 | impl<C> X<C> {}
  |           ^
  |
help: consider changing this type parameter to be a `const` generic
  |
2 | impl<const C: ()> X<C> {}
  |      ^^^^^^^^^^^
```
r? `@estebank`
Somewhat related rust-lang#84946
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 25, 2021
Account for incorrect `impl Foo<const N: ty> {}` syntax

Fix rust-lang#84946
@bors bors closed this as completed in 7190bc3 Nov 25, 2021
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 A-parser Area: The parsing of Rust source code to an AST. 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