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

Part of the error for lack of type annotations can give a hint that doesn't work #83606

Closed
glandium opened this issue Mar 28, 2021 · 0 comments · Fixed by #83654
Closed

Part of the error for lack of type annotations can give a hint that doesn't work #83606

glandium opened this issue Mar 28, 2021 · 0 comments · Fixed by #83654
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@glandium
Copy link
Contributor

Given the following code:

fn foo<const N: usize>(_: impl std::fmt::Display) -> [usize; N] {
    [0; N]
}

fn main() {
    let f = foo("foo");
}

The current output is:

error[E0282]: type annotations needed for `[usize; _]`
 --> src/main.rs:6:13
  |
6 |     let f = foo("foo");
  |         -   ^^^ cannot infer the value of const parameter `N` declared on the function `foo`
  |         |
  |         consider giving `f` the explicit type `[usize; _]`, where the type parameter `N` is specified
  |
help: consider specifying the const argument
  |
6 |     let f = foo::<N>("foo");
  |             ^^^^^^^^

The first half is correct, but the problem is that final suggestion. If you try it:

fn foo<const N: usize>(_: impl std::fmt::Display) -> [usize; N] {
    [0; N]
}

fn main() {
    let f = foo::<2>("foo");
}

you get another error because the suggestion can't work because of the impl std::fmt::Display:

error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position
 --> src/main.rs:6:19
  |
6 |     let f = foo::<2>("foo");
  |                   ^ explicit generic argument not allowed
@glandium glandium 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 Mar 28, 2021
@JohnTitor JohnTitor added C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Mar 29, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 30, 2021
Do not emit a suggestion that causes the E0632 error

Fixes rust-lang#83606
@bors bors closed this as completed in 7d888d1 Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants