Skip to content

Inaccurate error message for const operations in type parameters #79429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Nemo157 opened this issue Nov 25, 2020 · 2 comments · May be fixed by #142157
Open

Inaccurate error message for const operations in type parameters #79429

Nemo157 opened this issue Nov 25, 2020 · 2 comments · May be fixed by #142157
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nemo157
Copy link
Member

Nemo157 commented Nov 25, 2020

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.

@Nemo157 Nemo157 added the C-bug Category: This is a bug. label Nov 25, 2020
@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints labels Nov 25, 2020
@varkor varkor added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Dec 30, 2020
@XrXr
Copy link
Contributor

XrXr commented Dec 22, 2021

Maybe "generic parameters may not be used in const operations in this context" would be better?

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
@Enselic
Copy link
Member

Enselic commented Jun 7, 2025

Maybe "generic parameters may not be used in const operations in this context" would be better?

Should be fixed by #142157. I went with just adding here to the error message

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants