Skip to content

Inaccurate error message for const operations in type parameters #79429

Closed
@Nemo157

Description

@Nemo157

I tried this code:

#![feature(min_const_generics)]

struct X<const S: usize>;

impl<const S: usize> X<S> {
    const LEN: usize = S + 1;
}

struct Y<const S: usize> {
  stuff: [u8; { S + 1 }],
}

This gave an error message:

error: generic parameters may not be used in const operations
  --> src/lib.rs:10:17
   |
10 |   stuff: [u8; { S + 1 }],
   |                 ^ cannot perform const operation using `S`
   |
   = help: const parameters may only be used as standalone arguments, i.e. `S`
   = help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions

But you can see in X::LEN that you can perform const operations using S, the restriction this hits is more subtle in that you cannot perform const operations using generic parameters when these will impact the type system (as I understand it, I may be wrong). This also comes up if a generic parameter is used in defining an associated type.

Metadata

Metadata

Assignees

Labels

A-const-genericsArea: const generics (parameters and arguments)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-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