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

Provide const generics hints when a token is forgotten #84327

Open
teor2345 opened this issue Apr 19, 2021 · 3 comments
Open

Provide const generics hints when a token is forgotten #84327

teor2345 opened this issue Apr 19, 2021 · 3 comments
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. const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@teor2345
Copy link
Contributor

teor2345 commented Apr 19, 2021

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)
@teor2345 teor2345 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 Apr 19, 2021
@teor2345

This comment was marked as resolved.

@rustbot

This comment was marked as resolved.

@crlf0710 crlf0710 added A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. labels Apr 19, 2021
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jul 17, 2021
@BoxyUwU BoxyUwU removed the F-const_generics `#![feature(const_generics)]` label Jun 28, 2022
@fmease
Copy link
Member

fmease commented Feb 29, 2024

Triage:

  • First snippet (<const N>): No change
  • Second snippet (<N: usize>): Fixed on nightly by my PR #120570
  • Third snippet (<N>): No change

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. const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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

6 participants