Skip to content
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

Const generics: T doesn't derive both PartialEq and Eq #70790

Closed
Aloso opened this issue Apr 4, 2020 · 3 comments · Fixed by #70845
Closed

Const generics: T doesn't derive both PartialEq and Eq #70790

Aloso opened this issue Apr 4, 2020 · 3 comments · Fixed by #70845
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. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aloso
Copy link
Contributor

Aloso commented Apr 4, 2020

I tried this code:

#![allow(incomplete_features)]
#![feature(const_generics)]

pub trait GT<T: Eq + PartialEq, const N: T> {}

This produces the following error on rustc 1.44.0-nightly:

error[E0741]: the types of const generic parameters must derive `PartialEq` and `Eq`
 --> src/lib.rs:4:42
  |
4 | pub trait GT<T: Eq + PartialEq, const N: T> {}
  |                                          ^ `T` doesn't derive both `PartialEq` and `Eq`

error: aborting due to previous error

I guess that const generics with a generic type isn't supported/allowed. If that's the case, this should be mentioned in the error message.

Meta

rustc --version --verbose:

rustc 1.44.0-nightly (74bd074ee 2020-04-03)
binary: rustc
commit-hash: 74bd074eefcf4915c73d1ab91bc90859664729e6
commit-date: 2020-04-03
host: x86_64-unknown-linux-gnu
release: 1.44.0-nightly
LLVM version: 9.0
@Aloso Aloso added the C-bug Category: This is a bug. label Apr 4, 2020
@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 4, 2020
@varkor
Copy link
Member

varkor commented Apr 6, 2020

The type needs to derive PartialEq + Eq, not just implement them. This is because only structural_match types can be used for const generics at the moment, but we don't expose that to the user.

Maybe we could improve the diagnostic here by actually mentioning #[derive(PartialEq, Eq)].

@Aloso
Copy link
Contributor Author

Aloso commented Apr 13, 2020

@varkor thanks for fixing this so quickly!

Is there a chance that this restriction will be lifted, so the above code compiles?

@varkor
Copy link
Member

varkor commented Apr 13, 2020

We would like to lift the restriction, but probably not in the first stabilised version of const generics, as it's not easy to get right: we need to be very careful in making sure that we don't accidentally allow unsoundness to sneak in, and there are many subtleties.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 23, 2020
…gnostic, r=estebank

Make the `structural_match` error diagnostic for const generics clearer

The previous diagnostic caused confusion (rust-lang#70790), so this changes the message to be closer to the message for using non-`structural_match` constants in patterns, explicitly mentioning `#[derive(PartialEq, Eq)]`.

Fixes rust-lang#70790.

r? @estebank
@bors bors closed this as completed in 45e04fe Apr 24, 2020
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. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. 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.

3 participants