Skip to content

Diagnostic for const with slice type should suggest converting to array type #102390

Open
@jruderman

Description

@jruderman

Given the following code (playground):

const _SEASONS : [&str] = ["Winter", "Spring", "Summer", "Fall"];

The current output is:

error[E0277]: the size for values of type `[&'static str]` cannot be known at compilation time
 --> src/lib.rs:1:18
  |
1 | const _SEASONS : [&str] = ["Winter", "Spring", "Summer", "Fall"];
  |                  ^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[&'static str]`

Ideally the output should make it clearer why this is an error, and suggest a fix:

error: constant items must have a size known at compile-time
help: provide a length for the constant array: `[&str; 4]`

Note that if I omit the type entirely (playground):

const _SEASONS = ["Winter", "Spring", "Summer", "Fall"];

I do get an error message that suggests the correct array type:

error: missing type for `const` item
 --> src/lib.rs:1:15
  |
1 | const _SEASONS = ["Winter", "Spring", "Summer", "Fall"];
  |               ^ help: provide a type for the constant: `: [&str; 4]`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-sliceArea: `[T]`D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.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