Skip to content

Provide const generics hints when a token is forgotten #84327

Closed

Description

@teor2345

Ideally, rustc should guide the user towards producing const generics code, even if they forget the const, the type, or both.

Given the following code:

// forgot type
impl<T, const N> From<[T; N]> for VecWrapper<T>
where
    T: Clone,
{
    fn from(slice: [T; N]) -> Self {
        VecWrapper(slice.to_vec())
    }
}

(All examples are in https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4a11f9dad9f8069e70f2aea6cb4084ad )

The current output is:

error: expected `:`, found `>`
  --> src/lib.rs:25:16
   |
25 | impl<T, const N> From<[T; N]> for VecWrapper<T>
   |                ^ expected `:`

Ideally the output should look like:

  • expected : Type
  • maybe a hint for the usize type, because that's the type of N in [T; N]

Similarly:

// Forgot const 
impl<T, N: usize> From<[T; N]> for VecWrapper<T>
where
    T: Clone,
{
    fn from(slice: [T; N]) -> Self {
        VecWrapper(slice.to_vec())
    }
}

Produces:

error[E0423]: expected value, found type parameter `N`
  --> src/lib.rs:15:28
   |
15 | impl<T, N: usize> From<[T; N]> for VecWrapper<T>
   |                            ^ not a value

error[E0404]: expected trait, found builtin type `usize`
  --> src/lib.rs:15:12
   |
15 | impl<T, N: usize> From<[T; N]> for VecWrapper<T>
   |            ^^^^^ not a trait

error[E0423]: expected value, found type parameter `N`
  --> src/lib.rs:19:24
   |
19 |     fn from(slice: [T; N]) -> Self {
   |                        ^ not a value

Ideally the output should look like:

  • expected const with a hint

And:

// forgot const and type
impl<T, N> From<[T; N]> for VecWrapper<T>
where
    T: Clone,
{
    fn from(slice: [T; N]) -> Self {
        VecWrapper(slice.to_vec())
    }
}

Produces:

error[E0423]: expected value, found type parameter `N`
  --> src/lib.rs:25:21
   |
25 | impl<T, N> From<[T; N]> for VecWrapper<T>
   |                     ^ not a value

error[E0423]: expected value, found type parameter `N`
  --> src/lib.rs:29:24
   |
29 |     fn from(slice: [T; N]) -> Self {
   |                        ^ not a value

Ideally the output should look like:

  • expected const N: Type
  • maybe a hint for the usize type, because that's the type of N in [T; N]
$ rustc --version
rustc 1.53.0-nightly (392ba2ba1 2021-04-17)

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 19, 2021
  2. teor2345 commented on Apr 19, 2021

    @teor2345
    Author
  3. rustbot commented on Apr 19, 2021

    @rustbot
  4. added
    A-parserArea: The lexing & parsing of Rust source code to an AST
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    on Jul 17, 2021
  5. fmease commented on Feb 29, 2024

    @fmease
    Member

    Triage:

    • First snippet (<const N>): No change
    • Second snippet (<N: usize>): Fixed on nightly by my PR #120570
    • Third snippet (<N>): No change
  6. estebank commented on Jan 13, 2026

    @estebank
    Contributor

    #151077 addresses the "third snippet" case (<N>).

  7. added 2 commits that reference this issue on Jan 14, 2026
  8. added a commit that references this issue on Jan 14, 2026
  9. added 2 commits that reference this issue on Jan 14, 2026
    4826ff1
    00501de
  10. added a commit that references this issue on Jan 15, 2026
    e50e1aa
  11. added a commit that references this issue on Jan 15, 2026
    23c11cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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