Skip to content

Provide const generics hints when a token is forgotten #84327

Open
@teor2345

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)

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.const-generics-bad-diagnosticsAn error is correctly emitted, but is confusing, for `min_const_generics`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions