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

Diagnostics hint for const generics #80506

Open
yoshuawuyts opened this issue Dec 30, 2020 · 3 comments
Open

Diagnostics hint for const generics #80506

yoshuawuyts opened this issue Dec 30, 2020 · 3 comments
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@yoshuawuyts
Copy link
Member

Current Behavior

Given the following code:

let mut combinations = (1..2).combinations();
assert_eq!(combinations.next(), None);

where Iterator::combinations is defined as:

fn combinations<const N: usize>(self) -> Combinations<Self, N>;

The following diagnostic is provided:

error[E0282]: type annotations needed
   --> src\lib.rs:144:32
    |
144 |         let mut combinations = (1..2).combinations();
    |                                ^^^^^^^^^^^^^^^^^^^^^
    |
    = note: unable to infer the value of a const parameter

Attempting a fix, and failing:

Naively I thought I could fix this by adding a turbofish, but that doesn't seem to help:

let mut combinations = (1..2).combinations::<[usize; 2]>();
assert_eq!(combinations.next(), None);
error[E0107]: wrong number of const arguments: expected 1, found 0
   --> src\lib.rs:144:39
    |
144 |         let mut combinations = (1..2).combinations::<[usize; 2]>();
    |                                       ^^^^^^^^^^^^ expected 1 const argument

error[E0107]: wrong number of type arguments: expected 0, found 1
   --> src\lib.rs:144:54
    |
144 |         let mut combinations = (1..2).combinations::<[usize; 2]>();
    |                                                      ^^^^^^^^^^ unexpected type argument

Expected behavior

Providing a hint on how to correctly define const generics would be immensely helpful. I'm sure I'll be able to figure out the syntax for this eventually (currently going through #78460 among others), but given we're set for a 1.51 stabilization of min_const_generics I suspect more people will be running into this soon.

Further Considerations

I was looking at how hints work for e.g. missing type params in collect, and noticed no hints are provided there either. It only explains what to do, not showing how to do it. For something that's notoriously tricky to learn that seems like it could benefit from hints as well.

let x = (1..5).collect();
error[E0282]: type annotations needed
 --> src/main.rs:2:9
  |
2 |     let x = (1..5).collect();
  |         ^ consider giving `x` a type
@yoshuawuyts yoshuawuyts changed the title Diagnostics hint for const generics missing Diagnostics hint for const generics Dec 30, 2020
@yoshuawuyts
Copy link
Member Author

Found the solution to my bug by the way. This did the trick:

let mut combinations = (1..2).combinations::<2>();
assert_eq!(combinations.next(), None);

I think it's fair to say that the diagnostics were not a great help here. And in particular providing the wrong param ([usize; 2]) yielded confusing diagnostics.

@lcnr
Copy link
Contributor

lcnr commented Dec 30, 2020

error[E0282]: type annotations needed
--> src\lib.rs:144:32
|
144 | let mut combinations = (1..2).combinations();
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: unable to infer the value of a const parameter

can you provide a full example for this? we should emit a suggestion to use a const arg here.
For example in https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=dd05e205a3af777ec388a4dba98e66e8

@varkor varkor added A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 30, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Dec 30, 2020
@yoshuawuyts
Copy link
Member Author

Oh wow, the diagnostics for array_windows is heaps better! A full implementation of const Iterator::combinations can be found here: https://github.com/yoshuawuyts/const-combinations/blob/main/src/lib.rs

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 5, 2021
Add check for `[T;N]`/`usize` mismatch in astconv

Helps clarify the issue in rust-lang#80506
by adding a specific check for mismatches between [T;N] and usize.

r? `@lcnr`
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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

4 participants