Skip to content

Incorrect const parameter declaration on an impl block yields an uninformative syntax error #84946

Closed
@PatchMixolydic

Description

@PatchMixolydic

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> {}
  |     ----------------       -

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions