Skip to content

Tweak "use array's length as const param" suggestion - #163040

Open
estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:array-length-when-usize-wanted
Open

estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:array-length-when-usize-wanted

Conversation

@estebank

Copy link
Copy Markdown
Contributor
  • Don't suggest braces unnecessarily for numeric literals
  • Use verbose suggestion
  • Tweak messages
error[E0747]: type provided when a constant was expected
  --> $DIR/suggest_const_for_array.rs:6:15
   |
LL |     example::<[usize; 3]>();
   |               ^^^^^^^^^^ array type provided where a `usize` was expected
   |
help: you might have meant to use the array's length's value
   |
LL -     example::<[usize; 3]>();
LL +     example::<3>();
   |

@rustbot

rustbot commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

HIR ty lowering was modified

cc @fmease

@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 Sep 19, 2026
@rustbot

rustbot commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

r? @adwinwhite

rustbot has assigned @adwinwhite.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 20 candidates

@fmease fmease left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me some nitpicks & some optional suggestions

View changes since this review

Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs
Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs Outdated
Comment on lines +103 to +104
let sugg = if let hir::ConstArgKind::Anon(hir::AnonConst { body, .. }) = len.kind
&& let hir::ExprKind::Lit(..) = tcx.hir_body(*body).value.kind

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really up to speed with (m)GCA. Does this check fail if feature min_generic_const_args / generic_const_args is enabled since the length is no longer an AnonConst but a direct ConstArgKind::Lit?

If so, we might want to account for that, too. Of course, not high priority so feel free to ignore.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added revisions for all three cases, as well as the additional check, but the check doesn't trigger today (yet?).

GenericParamDefKind::Const { .. },
) if tcx.type_of(param.def_id).skip_binder() == tcx.types.usize => {
err.span_label(arg.span(), "array type provided where a `usize` was expected");
let snippet = sess.source_map().span_to_snippet(tcx.hir_span(len.hir_id));

@fmease fmease Sep 19, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(preexisting) It should be possible to eliminate the use of span_to_snippet by using a multi-part suggestion, Span::{until,to,between,…} and the span of the entire HIR type above. 🤷 Feel free to ignore

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just concerned about spans through multiple macro scopes.

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 19, 2026
@fmease fmease assigned fmease and unassigned adwinwhite Sep 19, 2026
@fmease fmease changed the title Tweak "use array's lenght as const param" suggestion Tweak "use array's length as const param" suggestion Sep 19, 2026
@estebank
estebank force-pushed the array-length-when-usize-wanted branch from 56a01b2 to f14d55b Compare September 20, 2026 00:21
- Don't suggest braces unnecessarily for numeric literals
- Use verbose suggestion
- Tweak messages

```
error[E0747]: type provided when a constant was expected
  --> $DIR/suggest_const_for_array.rs:6:15
   |
LL |     example::<[usize; 3]>();
   |               ^^^^^^^^^^ array type provided where a `usize` was expected
   |
help: you might have meant to use the array's length's value
   |
LL -     example::<[usize; 3]>();
LL +     example::<3>();
   |
```
@estebank
estebank force-pushed the array-length-when-usize-wanted branch from f14d55b to 5f33ac3 Compare September 20, 2026 00:22
@estebank

Copy link
Copy Markdown
Contributor Author

@bors r=fmease

@rust-bors

rust-bors Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5f33ac3 has been approved by fmease

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 20, 2026
…anted, r=fmease

Tweak "use array's length as const param" suggestion

- Don't suggest braces unnecessarily for numeric literals
- Use verbose suggestion
- Tweak messages

```
error[E0747]: type provided when a constant was expected
  --> $DIR/suggest_const_for_array.rs:6:15
   |
LL |     example::<[usize; 3]>();
   |               ^^^^^^^^^^ array type provided where a `usize` was expected
   |
help: you might have meant to use the array's length's value
   |
LL -     example::<[usize; 3]>();
LL +     example::<3>();
   |
```
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 20, 2026
…anted, r=fmease

Tweak "use array's length as const param" suggestion

- Don't suggest braces unnecessarily for numeric literals
- Use verbose suggestion
- Tweak messages

```
error[E0747]: type provided when a constant was expected
  --> $DIR/suggest_const_for_array.rs:6:15
   |
LL |     example::<[usize; 3]>();
   |               ^^^^^^^^^^ array type provided where a `usize` was expected
   |
help: you might have meant to use the array's length's value
   |
LL -     example::<[usize; 3]>();
LL +     example::<3>();
   |
```
rust-bors Bot pushed a commit that referenced this pull request Sep 20, 2026
…uwer

Rollup of 15 pull requests

Successful merges:

 - #162726 (std: fix unix socket address panic on a full sun_path)
 - #163016 (Don't claim that escaping value is a reference in diagnostics)
 - #163040 (Tweak "use array's length as const param" suggestion)
 - #163060 (Point to fields that introduce trait requirements)
 - #163066 (don't mark `f128` as reliable on AIX)
 - #162098 (Tweak `Infallible` docs)
 - #162854 (Add safety section for atomic_load/store)
 - #163015 (add `minicore::ffi::VaList`)
 - #163021 (`va_arg`: pass in `TyAndLayout`)
 - #163036 ([rustdoc] Correctly handle `dyn` trait methods linking for jump to def feature)
 - #163042 (Remove redundant output from suggestion)
 - #163046 (Use verbose suggestion for `const _`)
 - #163050 (Use verbose suggestion for similarly named label suggestion)
 - #163052 (Use verbose suggestion for wrong primitive type names)
 - #163055 (Use the full path of `bug_impl` to avoid bogus errors in rust-analyzer)
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.

4 participants