Skip to content

Manual PartialEq impl not found for const generics. #94661

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

Closed
jafioti opened this issue Mar 6, 2022 · 4 comments
Closed

Manual PartialEq impl not found for const generics. #94661

jafioti opened this issue Mar 6, 2022 · 4 comments

Comments

@jafioti
Copy link

jafioti commented Mar 6, 2022

I tried this code:

#[derive(Eq)]
pub enum DimType {
    Static(usize),
    Dynamic
}

impl PartialEq for DimType {
    fn eq(&self, other: &Self) -> bool {
        match self {
            DimType::Static(s) => match other {
                DimType::Static(t) => s == t,
                DimType::Dynamic => true,
            },
            DimType::Dynamic => true,
        }
    }
}

I expected to see this happen: I expected to be able to use DimType as a const generic type. I am able to use it when I derive PartialEq and Eq, but when I manually derive PartialEq it doesn't work.

Instead, this happened: I get an error: tensor::DimType must be annotated with #[derive(PartialEq, Eq)] to be used as the type of a const parameter

Meta

rustc --version --verbose:

rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.61.0-nightly (c274e4969 2022-03-05)`
@jafioti jafioti added the C-bug Category: This is a bug. label Mar 6, 2022
@ChayimFriedman2
Copy link
Contributor

This is not a bug, this is intended. PartialEq and Eq has to be derived so the compiler can ensure comparing types is sound.

See https://github.com/rust-lang/rfcs/blob/master/text/1445-restrict-constants-in-patterns.md.

@jafioti
Copy link
Author

jafioti commented Mar 7, 2022

@ChayimFriedman2 Is there any way to manually implement PartialEq like I have it then? I am trying to get DimType::Dynamic to be equal to DimType::Static

@wwylele
Copy link
Contributor

wwylele commented Mar 7, 2022

Your PartialEq implementation doesn't satisfy the transitivity requirement ( Static(1) == Dynamic == Static(2) => Static(1) == Static(2) ). Exactly because of the existence of this kind of ill-formed PartialEq implementation, the compiler cannot trust a custom PartialEq implementation for const generic dispatch.

@fmease
Copy link
Member

fmease commented Jan 24, 2024

Closing as works as intended.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants