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

Derived PartialEq cannot be used in a const function #77695

Closed
mbartlett21 opened this issue Oct 8, 2020 · 5 comments
Closed

Derived PartialEq cannot be used in a const function #77695

mbartlett21 opened this issue Oct 8, 2020 · 5 comments
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-feature-request Category: A feature request, i.e: not implemented / a PR. F-const_trait_impl `#![feature(const_trait_impl)]` T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@mbartlett21
Copy link
Contributor

The derived PartialEq implementation cannot be used in a const context.

The code below gives error E0015

#[derive(PartialEq)]
struct A(u8);

const fn is_eq(left: &A, right: &A) -> bool {
    left == right
}
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
 --> derive_eq_partialeq.rs:5:2
  |
5 |     left == right
  |     ^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0015`.

Meta

rustc --version --verbose:

rustc 1.49.0-nightly (91a79fb29 2020-10-07)
binary: rustc
commit-hash: 91a79fb29ac78d057d04dbe86be13d5dcc64309a
commit-date: 2020-10-07
host: x86_64-pc-windows-msvc
release: 1.49.0-nightly
LLVM version: 11.0
@mbartlett21 mbartlett21 added the C-bug Category: This is a bug. label Oct 8, 2020
@matthewjasper matthewjasper added A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-enhancement Category: An issue proposing an enhancement or a PR with one. F-const_trait_impl `#![feature(const_trait_impl)]` and removed C-bug Category: This is a bug. labels Oct 8, 2020
@jonas-schievink
Copy link
Contributor

I'm not sure this will ever work. It would require the macro to generate a const impl (#67792), but there is no way it can know that that is possible at expansion time.

@leonardo-m
Copy link

What's the solution? A #[derive(ConstPartialEq)]? :-)

@mbartlett21
Copy link
Contributor Author

What I may have to do for the moment is make a const fn on the struct.

I had previously implemented const PartialEq manually, but when I tried to use it a const_generic parameter, it said that they had to be derived.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 17, 2020

yea, this will require opt-in, so schemes like #[derive(const PartialEq)] could be possible.

@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 21, 2020
@eminence
Copy link
Contributor

Now that #102049 has landed, it's possible to do const derive PartialEq in the latest nightly:

#![feature(const_cmp)]
#![feature(const_trait_impl)]
#![feature(derive_const)]

#[derive_const(PartialEq)]
struct A(u8);

const fn is_eq(left: &A, right: &A) -> bool {
    left == right
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-feature-request Category: A feature request, i.e: not implemented / a PR. F-const_trait_impl `#![feature(const_trait_impl)]` T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants