Skip to content

Wrong suggestion for const generic type #89013

Closed
@CGMossa

Description

@CGMossa

Given the following code: (see play.rust-lang.org link).

trait Foo<const N: usize> {
    fn do_x(&self) -> [u8; N];
}

struct Bar;

impl Foo<N = const 3> for Bar {
    fn do_x(&self) -> [u8; N] {
        [0u8; N]
    }
}

The current output is:

Compiling playground v0.0.1 (/playground)
error: missing type to the right of `=`
 --> src/lib.rs:7:13
  |
7 | impl Foo<N = const 3> for Bar {
  |             ^
  |
help: to constrain the associated type, add a type after `=`
  |
7 | impl Foo<N = TheTypeconst 3> for Bar {
  |              +++++++
help: remove the `=` if `N` is a type
  |
7 - impl Foo<N = const 3> for Bar {
7 + impl Foo<N const 3> for Bar {
  | 

error: expected one of `,`, `>`, a const expression, lifetime, or type, found keyword `const`
 --> src/lib.rs:7:14
  |
7 | impl Foo<N = const 3> for Bar {
  |              ^^^^^ expected one of `,`, `>`, a const expression, lifetime, or type

error: could not compile `playground` due to 2 previous errors

Notice the TheTypeconst in the above error message.

I've checked if this is the case on nightly, and at the time of this filing, this
is also an issue on nightly.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions