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

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

Open
jruderman opened this issue Sep 28, 2022 · 0 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-slice Area: `[T]` D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jruderman
Copy link
Contributor

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]`
@jruderman jruderman 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 Sep 28, 2022
@fmease fmease added A-slice Area: `[T]` D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. labels Mar 20, 2025
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 A-slice Area: `[T]` D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. 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

2 participants