Skip to content

Tweak output on misused const parameter to provide more context and more accurate suggestion - #164775

Merged
rust-bors[bot] merged 7 commits into
rust-lang:mainfrom
estebank:missing-const-param
Nov 7, 2026
Merged

rust-bors[bot] merged 7 commits into
rust-lang:mainfrom
estebank:missing-const-param

Conversation

@estebank

@estebank estebank commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

When missing const param in impl, provide complete suggestion:

error[E0107]: missing generics for struct `a::Foo`
  --> $DIR/impl-block-missing-const-generics.rs:6:10
   |
LL |     impl Foo {}
   |          ^^^ expected 1 generic argument
   |
note: struct defined here, with 1 generic parameter: `AHA`
  --> $DIR/impl-block-missing-const-generics.rs:4:12
   |
LL |     struct Foo<const AHA: usize>{}
   |            ^^^ ----------------
help: add missing generic argument
   |
LL |     impl<const AHA: usize> Foo<AHA> {}
   |         ++++++++++++++++++    +++++

Previously we only suggested Foo<AHA> without the impl<const AHA: usize>.

Fix #91119.

Point at const param declaration when constant is not expected type:

error: the constant `N` is not of type `usize`
  --> $DIR/const-in-impl-fn-return-type.rs:12:32
   |
LL |     fn func<const N: u32>() -> [(); N];
   |             ------------       ^^^^^^^ expected `usize`, found `u32`
   |             |
   |             found this const parameter
   |
   = note: the length of array `[(); N]` must be type `usize`

@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 Oct 23, 2026
@rustbot

rustbot commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
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

@rust-log-analyzer

This comment has been minimized.

@estebank
estebank force-pushed the missing-const-param branch 2 times, most recently from aa81524 to d5fa09c Compare October 23, 2026 17:40
@rustbot rustbot added the WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) label Oct 23, 2026
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@estebank
estebank force-pushed the missing-const-param branch 2 times, most recently from 69d8e14 to 8cb71cd Compare October 26, 2026 03:04
@rustbot

This comment has been minimized.

| ^^^^^^^^^^^^^^^^ expected `u32`, found `u64`
| ------------ ^^^^^^^^^^^^^^^^ expected `u32`, found `u64`
| |
| found this const parameter

@wesleywiser wesleywiser Oct 28, 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.

Is there a better message we can give than this? I'm not sure exactly what this note is trying to tell me 🙂

Maybe?

error: the constant `N` is not of type `u32`
  --> $DIR/mismatched-types-with-generic-in-ace.rs:15:29
   |
LL | fn bar<const N: u64, T: Foo<ASSOC<N> = { N }>>() {}
   |        ------------         ^^^^^^^^^^^^^^^^ expected `u32`, found `u64`
   |        |
   |        `N` is defined here

View changes since the review

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.

Changed

@estebank
estebank force-pushed the missing-const-param branch from 8cb71cd to ff0444a Compare November 4, 2026 19:24
@rustbot

This comment has been minimized.

```
error[E0107]: missing generics for struct `a::Foo`
  --> $DIR/impl-block-missing-const-generics.rs:6:10
   |
LL |     impl Foo {}
   |          ^^^ expected 1 generic argument
   |
note: struct defined here, with 1 generic parameter: `AHA`
  --> $DIR/impl-block-missing-const-generics.rs:4:12
   |
LL |     struct Foo<const AHA: usize>{}
   |            ^^^ ----------------
help: add missing generic argument
   |
LL |     impl<const AHA: usize> Foo<AHA> {}
   |         ++++++++++++++++++    +++++
```

Previously we only suggested `Foo<AHA>` without the `impl<const AHA: usize>`.
```
error: the constant `N` is not of type `usize`
  --> $DIR/const-in-impl-fn-return-type.rs:12:32
   |
LL |     fn func<const N: u32>() -> [(); N];
   |             ------------       ^^^^^^^ expected `usize`, found `u32`
   |             |
   |             found this const parameter
   |
   = note: the length of array `[(); N]` must be type `usize`
```
```
error: the constant `B` is not of type `usize`
  --> $DIR/check_const_arg_type_in_free_alias.rs:15:30
   |
LL | type ArrLen<const B: bool> = [(); B];
   |             -------------    ^^^^^^^ expected `usize`, found `bool`
   |             |
   |             found this const parameter
   |
   = note: the length of array `[(); B]` must be type `usize`
```
```
error: the constant `N` is not of type `usize`
  --> $DIR/bad-subst-const-kind.rs:8:26
   |
LL | impl<const N: u64> Q for [u8; N] {
   |      ------------        ^^^^^^^ expected `usize`, found `u64`
   |      |
   |      `N` defined here
   |
   = note: the length of array `[u8; N]` must be type `usize`
```
@wesleywiser

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ff0444a has been approved by wesleywiser

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-review Status: Awaiting review from the assignee but also interested parties. labels Nov 5, 2026
Comment on lines +17 to +18
LL | impl<T, 'a> Reborrow for PhantomData<'a, T> {}
| ++ +++

@estebank estebank Nov 5, 2026 •

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.

Note to self: need to follow up and suggest <'a, T> instead of <T, 'a>. Applying the suggestion should provide a appropriate suggestion already, but better to handle eagerly if possible. This bug is likely present in more than one suggestion.

View changes since the review

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.

The fix is a single line. I'll push to this PR and take your reapproval as a given @wesleywiser :)

@estebank
estebank force-pushed the missing-const-param branch from ff0444a to ca9e035 Compare November 5, 2026 18:54
@rust-bors rust-bors Bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 5, 2026
@rust-bors

rust-bors Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

⚠️ A new commit ca9e03561c43015eae0c7b9df9a6ae2679ef509e was pushed.

This pull request was unapproved.

@rustbot

rustbot commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@estebank

estebank commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

@bors r=wesleywiser

@rust-bors

rust-bors Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ca9e035 has been tentatively approved by wesleywiser

It will be put into the queue for this repository once PR CI succeeds.

@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 Nov 5, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 5, 2026
…sleywiser

Tweak output on misused `const` parameter to provide more context and more accurate suggestion

When missing `const` param in `impl`, provide complete suggestion:

```
error[E0107]: missing generics for struct `a::Foo`
  --> $DIR/impl-block-missing-const-generics.rs:6:10
   |
LL |     impl Foo {}
   |          ^^^ expected 1 generic argument
   |
note: struct defined here, with 1 generic parameter: `AHA`
  --> $DIR/impl-block-missing-const-generics.rs:4:12
   |
LL |     struct Foo<const AHA: usize>{}
   |            ^^^ ----------------
help: add missing generic argument
   |
LL |     impl<const AHA: usize> Foo<AHA> {}
   |         ++++++++++++++++++    +++++
```

Previously we only suggested `Foo<AHA>` without the `impl<const AHA: usize>`.

Fix rust-lang#91119.

Point at `const` param declaration when constant is not expected type:

```
error: the constant `N` is not of type `usize`
  --> $DIR/const-in-impl-fn-return-type.rs:12:32
   |
LL |     fn func<const N: u32>() -> [(); N];
   |             ------------       ^^^^^^^ expected `usize`, found `u32`
   |             |
   |             found this const parameter
   |
   = note: the length of array `[(); N]` must be type `usize`
```
rust-bors Bot pushed a commit that referenced this pull request Nov 5, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #164775 (Tweak output on misused `const` parameter to provide more context and more accurate suggestion)
 - #165448 (Update `Vec::retain_unstable` docs to say that this method is generally faster than `Vec::retain_mut`)
 - #165503 (update Enzyme submodule)
 - #165505 (DFV: Reject `#[non_exhaustive]` on enum variants with default field values)
 - #165514 (Remove unused `WhereEqPredicate` and `ScopeId` types)
 - #165519 (Update books)
rust-bors Bot pushed a commit that referenced this pull request Nov 6, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #164775 (Tweak output on misused `const` parameter to provide more context and more accurate suggestion)
 - #165448 (Update `Vec::retain_unstable` docs to say that this method is generally faster than `Vec::retain_mut`)
 - #165503 (update Enzyme submodule)
 - #165505 (DFV: Reject `#[non_exhaustive]` on enum variants with default field values)
 - #165514 (Remove unused `WhereEqPredicate` and `ScopeId` types)
 - #165519 (Update books)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 6, 2026
…sleywiser

Tweak output on misused `const` parameter to provide more context and more accurate suggestion

When missing `const` param in `impl`, provide complete suggestion:

```
error[E0107]: missing generics for struct `a::Foo`
  --> $DIR/impl-block-missing-const-generics.rs:6:10
   |
LL |     impl Foo {}
   |          ^^^ expected 1 generic argument
   |
note: struct defined here, with 1 generic parameter: `AHA`
  --> $DIR/impl-block-missing-const-generics.rs:4:12
   |
LL |     struct Foo<const AHA: usize>{}
   |            ^^^ ----------------
help: add missing generic argument
   |
LL |     impl<const AHA: usize> Foo<AHA> {}
   |         ++++++++++++++++++    +++++
```

Previously we only suggested `Foo<AHA>` without the `impl<const AHA: usize>`.

Fix rust-lang#91119.

Point at `const` param declaration when constant is not expected type:

```
error: the constant `N` is not of type `usize`
  --> $DIR/const-in-impl-fn-return-type.rs:12:32
   |
LL |     fn func<const N: u32>() -> [(); N];
   |             ------------       ^^^^^^^ expected `usize`, found `u32`
   |             |
   |             found this const parameter
   |
   = note: the length of array `[(); N]` must be type `usize`
```
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 6, 2026
…sleywiser

Tweak output on misused `const` parameter to provide more context and more accurate suggestion

When missing `const` param in `impl`, provide complete suggestion:

```
error[E0107]: missing generics for struct `a::Foo`
  --> $DIR/impl-block-missing-const-generics.rs:6:10
   |
LL |     impl Foo {}
   |          ^^^ expected 1 generic argument
   |
note: struct defined here, with 1 generic parameter: `AHA`
  --> $DIR/impl-block-missing-const-generics.rs:4:12
   |
LL |     struct Foo<const AHA: usize>{}
   |            ^^^ ----------------
help: add missing generic argument
   |
LL |     impl<const AHA: usize> Foo<AHA> {}
   |         ++++++++++++++++++    +++++
```

Previously we only suggested `Foo<AHA>` without the `impl<const AHA: usize>`.

Fix rust-lang#91119.

Point at `const` param declaration when constant is not expected type:

```
error: the constant `N` is not of type `usize`
  --> $DIR/const-in-impl-fn-return-type.rs:12:32
   |
LL |     fn func<const N: u32>() -> [(); N];
   |             ------------       ^^^^^^^ expected `usize`, found `u32`
   |             |
   |             found this const parameter
   |
   = note: the length of array `[(); N]` must be type `usize`
```
rust-bors Bot pushed a commit that referenced this pull request Nov 6, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #164784 (Introduce OptimizationObjective and fix the entry shim's size attributes)
 - #165037 (Drop array elements in order after a partial move)
 - #165078 (Use the `2.38` binutils version in CI)
 - #159374 (Lower #[no_mangle] to #[symbol_name]/#[link_name])
 - #163284 (feat(Reborrow): Allow multiple lifetimes)
 - #164775 (Tweak output on misused `const` parameter to provide more context and more accurate suggestion)
 - #165194 (name elided lifetimes in `use<...>` suggestion when `impl Trait` params are present)
 - #165222 (Anonymize inner binders when checking refining)
 - #165335 (Fallible type ops)
 - #165388 (Accept variadic SysV64, Win64, and Aapcs FnAbis in rustc_public)
 - #165448 (Update `Vec::retain_unstable` docs to say that this method is generally faster than `Vec::retain_mut`)
 - #165494 (fix Miri ICE on exit with () return type)
 - #165503 (update Enzyme submodule)
 - #165505 (DFV: Reject `#[non_exhaustive]` on enum variants with default field values)
 - #165512 (Fix warnings for the L4Re targets)
 - #165514 (Remove unused `WhereEqPredicate` and `ScopeId` types)
 - #165518 (style-guide: Remove ABI explicitness prescription for extern items)
 - #165519 (Update books)
 - #165534 (rename MIR_borrow_checking timing scope to less confusing name)
rust-bors Bot pushed a commit that referenced this pull request Nov 7, 2026
Rollup of 15 pull requests

Successful merges:

 - #165037 (Drop array elements in order after a partial move)
 - #165550 (Make `Literal::cstr_value` return a `CString`)
 - #159374 (Lower #[no_mangle] to #[symbol_name]/#[link_name])
 - #163284 (feat(Reborrow): Allow multiple lifetimes)
 - #164775 (Tweak output on misused `const` parameter to provide more context and more accurate suggestion)
 - #165194 (name elided lifetimes in `use<...>` suggestion when `impl Trait` params are present)
 - #165222 (Anonymize inner binders when checking refining)
 - #165335 (Fallible type ops)
 - #165388 (Accept variadic SysV64, Win64, and Aapcs FnAbis in rustc_public)
 - #165448 (Update `Vec::retain_unstable` docs to say that this method is generally faster than `Vec::retain_mut`)
 - #165494 (fix Miri ICE on exit with () return type)
 - #165505 (DFV: Reject `#[non_exhaustive]` on enum variants with default field values)
 - #165512 (Fix warnings for the L4Re targets)
 - #165514 (Remove unused `WhereEqPredicate` and `ScopeId` types)
 - #165518 (style-guide: Remove ABI explicitness prescription for extern items)
@rust-bors
rust-bors Bot merged commit 29e9edf into rust-lang:main Nov 7, 2026
14 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Nov 7, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 7, 2026
Rollup merge of #164775 - estebank:missing-const-param, r=wesleywiser

Tweak output on misused `const` parameter to provide more context and more accurate suggestion

When missing `const` param in `impl`, provide complete suggestion:

```
error[E0107]: missing generics for struct `a::Foo`
  --> $DIR/impl-block-missing-const-generics.rs:6:10
   |
LL |     impl Foo {}
   |          ^^^ expected 1 generic argument
   |
note: struct defined here, with 1 generic parameter: `AHA`
  --> $DIR/impl-block-missing-const-generics.rs:4:12
   |
LL |     struct Foo<const AHA: usize>{}
   |            ^^^ ----------------
help: add missing generic argument
   |
LL |     impl<const AHA: usize> Foo<AHA> {}
   |         ++++++++++++++++++    +++++
```

Previously we only suggested `Foo<AHA>` without the `impl<const AHA: usize>`.

Fix #91119.

Point at `const` param declaration when constant is not expected type:

```
error: the constant `N` is not of type `usize`
  --> $DIR/const-in-impl-fn-return-type.rs:12:32
   |
LL |     fn func<const N: u32>() -> [(); N];
   |             ------------       ^^^^^^^ expected `usize`, found `u32`
   |             |
   |             found this const parameter
   |
   = note: the length of array `[(); N]` must be type `usize`
```
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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler error messages leads to wrong usage of const generics

4 participants