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

Compiler fails to report len for static A: [T; _] #135010

Closed
workingjubilee opened this issue Jan 2, 2025 · 1 comment · Fixed by #135044
Closed

Compiler fails to report len for static A: [T; _] #135010

workingjubilee opened this issue Jan 2, 2025 · 1 comment · Fixed by #135044
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-generic_arg_infer Using `_` as a const argument: #![feature(generic_arg_infer)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@workingjubilee
Copy link
Member

workingjubilee commented Jan 2, 2025

Code

static A: [u8; _] = [12, 23, 34, 45];

Current output

Compiling playground v0.0.1 (/playground)
error: in expressions, `_` can only be used on the left-hand side of an assignment
 --> src/lib.rs:1:16
  |
1 | static A: [u8; _] = [12, 23, 34, 45];
  |                ^ `_` not allowed here

error[E0658]: using `_` for array lengths is unstable
 --> src/lib.rs:1:16
  |
1 | static A: [u8; _] = [12, 23, 34, 45];
  |                ^
  |
  = note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information

For more information about this error, try `rustc --explain E0658`.
error: could not compile `playground` (lib) due to 2 previous errors

Desired output

Compiling playground v0.0.1 (/playground)
error: `_` is not a valid array length for a static item
 --> src/lib.rs:1:16
  |
1 | static A: [u8; _] = [12, 23, 34, 45];
  |                ^ `_` not allowed here
  |
  = help: you meant 4

For more information about this error, try `rustc --explain E0658`.
error: could not compile `playground` (lib) due to 2 previous errors

Rationale and extra context

rationale: @BoxyUwU expected it to do so.

Other cases

We can also get this:

  = note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
  = help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
  = note: this compiler was built on 2025-01-01; consider upgrading it if it is out of date

Rust Version

$ rustc --version --verbose
rustc 1.85.0-nightly (45d11e51b 2025-01-01)
binary: rustc
commit-hash: 45d11e51bb66c2deb63a006fe3953c4b6fbc50c2
commit-date: 2025-01-01
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Anything else?

No response

@workingjubilee workingjubilee 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. F-generic_arg_infer Using `_` as a const argument: #![feature(generic_arg_infer)]` labels Jan 2, 2025
@compiler-errors
Copy link
Member

I'll look into this. Seems pretty involved.

@compiler-errors compiler-errors self-assigned this Jan 2, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 3, 2025
…stions-in-const, r=oli-obk

Improve infer (`_`) suggestions in `const`s and `static`s

Fixes rust-lang#135010.

This PR does a few things to (imo) greatly improve the error message when users write something like `static FOO: [i32; _] = [1, 2, 3]`.

Firstly, it adapts the recovery code for when we encounter `_` in a const/static to work a bit more like `fn foo() -> _`, and removes the somewhat redundant query `diagnostic_only_typeck`.

Secondly, it changes the lowering for `[T; _]` to always lower under the `feature(generic_arg_infer)` logic to `ConstArgKind::Infer`. We still issue the feature error, so it's not doing anything *observable* on the good path, but it does mean that we no longer erroneously interpret `[T; _]`'s array length as a `_` **wildcard expression** (à la destructuring assignment, like `(_, y) = expr`).

Lastly it makes the suggestions verbose and fixes (well, suppresses) a bug with stashing and suggestions.

r? oli-obk
@bors bors closed this as completed in 12cc9b4 Jan 4, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 4, 2025
Rollup merge of rust-lang#135044 - compiler-errors:better-infer-suggestions-in-const, r=oli-obk

Improve infer (`_`) suggestions in `const`s and `static`s

Fixes rust-lang#135010.

This PR does a few things to (imo) greatly improve the error message when users write something like `static FOO: [i32; _] = [1, 2, 3]`.

Firstly, it adapts the recovery code for when we encounter `_` in a const/static to work a bit more like `fn foo() -> _`, and removes the somewhat redundant query `diagnostic_only_typeck`.

Secondly, it changes the lowering for `[T; _]` to always lower under the `feature(generic_arg_infer)` logic to `ConstArgKind::Infer`. We still issue the feature error, so it's not doing anything *observable* on the good path, but it does mean that we no longer erroneously interpret `[T; _]`'s array length as a `_` **wildcard expression** (à la destructuring assignment, like `(_, y) = expr`).

Lastly it makes the suggestions verbose and fixes (well, suppresses) a bug with stashing and suggestions.

r? oli-obk
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 F-generic_arg_infer Using `_` as a const argument: #![feature(generic_arg_infer)]` 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