Skip to content

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

Closed
@glandium

Description

@glandium

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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