Skip to content

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Jan 2, 2025

Fixes #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

@compiler-errors compiler-errors marked this pull request as ready for review January 2, 2025 19:27
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 2, 2025
@compiler-errors compiler-errors force-pushed the better-infer-suggestions-in-const branch from a4e65fa to 6c5c5ce Compare January 2, 2025 19:32
@compiler-errors compiler-errors force-pushed the better-infer-suggestions-in-const branch from 6c5c5ce to 0fd64ef Compare January 2, 2025 23:39
@oli-obk
Copy link
Contributor

oli-obk commented Jan 3, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 3, 2025

📌 Commit 0fd64ef has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 3, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request 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 added a commit to rust-lang-ci/rust that referenced this pull request Jan 3, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#133420 (Switch rtems target to panic unwind)
 - rust-lang#134965 (Make Boxy UwU)
 - rust-lang#135007 (Some type-outlives computation tweaks)
 - rust-lang#135036 (run-make-support: adjust assertion printing, add some basic sanity checks)
 - rust-lang#135043 (rustdoc: treat `allowed_through_unstable_modules` as deprecation)
 - rust-lang#135044 (Improve infer (`_`) suggestions in `const`s and `static`s)
 - rust-lang#135058 (refactor bootstrap path resolution)
 - rust-lang#135061 (crashes: add latest batch of tests)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 3, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#133420 (Switch rtems target to panic unwind)
 - rust-lang#134965 (Make Boxy UwU)
 - rust-lang#135007 (Some type-outlives computation tweaks)
 - rust-lang#135036 (run-make-support: adjust assertion printing, add some basic sanity checks)
 - rust-lang#135043 (rustdoc: treat `allowed_through_unstable_modules` as deprecation)
 - rust-lang#135044 (Improve infer (`_`) suggestions in `const`s and `static`s)
 - rust-lang#135058 (refactor bootstrap path resolution)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 12cc9b4 into rust-lang:master Jan 4, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 4, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request 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
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler fails to report len for static A: [T; _]
4 participants