rust/tests/ui/const-generics/incorrect-const-param.rs
Esteban Küber 92db7b2b5a Recover parse gracefully from <const N>
When a const param doesn't have a `: Type`, recover the parser state and provide a structured suggestion. This not only provides guidance on what was missing, but it also makes subsuequent errors to be emitted that would otherwise be silenced.

```
error: expected `:`, found `>`
  --> $DIR/incorrect-const-param.rs:26:16
   |
LL | impl<T, const N> From<[T; N]> for VecWrapper<T>
   |                ^ expected `:`
   |
help: you might have meant to write the type of the const parameter here
   |
LL | impl<T, const N: /* Type */> From<[T; N]> for VecWrapper<T>
   |                ++++++++++++
```
2026-01-14 17:56:31 +00:00

45 lines
1,020 B
Rust

// #84327
struct VecWrapper<T>(Vec<T>);
// Correct
impl<T, const N: usize> From<[T; N]> for VecWrapper<T>
where
T: Clone,
{
fn from(slice: [T; N]) -> Self {
VecWrapper(slice.to_vec())
}
}
// Forgot const
impl<T, N: usize> From<[T; N]> for VecWrapper<T> //~ ERROR expected value, found type parameter `N`
where //~^ ERROR expected trait, found builtin type `usize`
T: Clone,
{
fn from(slice: [T; N]) -> Self { //~ ERROR expected value, found type parameter `N`
VecWrapper(slice.to_vec())
}
}
// Forgot type
impl<T, const N> From<[T; N]> for VecWrapper<T> //~ ERROR expected `:`, found `>`
where
T: Clone,
{
fn from(slice: [T; N]) -> Self {
VecWrapper(slice.to_vec())
}
}
// Forgot const and type
impl<T, N> From<[T; N]> for VecWrapper<T> //~ ERROR expected value, found type parameter `N`
where
T: Clone,
{
fn from(slice: [T; N]) -> Self { //~ ERROR expected value, found type parameter `N`
VecWrapper(slice.to_vec())
}
}
fn main() {}