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

Using PartialEq with empty c-like struct or tuple struct enum variant causes compiler parse error #33

Open
DavidAntliff opened this issue May 1, 2024 · 0 comments

Comments

@DavidAntliff
Copy link

DavidAntliff commented May 1, 2024

Using subenum 1.1.2 with Rust 1.77.2 (stable).

Please consider this, which compiles OK:

#[subenum(Fruit)]
pub enum Food {
    #[subenum(Fruit)]
    Apple {},
}

The addition of the PartialEq trait to the derive clause results in a compiler error:

#[subenum(Fruit)]
#[derive(PartialEq)]
pub enum Food {
    #[subenum(Fruit)]
    Apple {},
}

The error is:

error: expected expression, found `,`
  --> src/problem.rs:19:1
   |
19 | #[subenum(Fruit)]
   | ^^^^^^^^^^^^^^^^^
   | |
   | expected expression
   | while parsing the `match` arm starting here

The same error occurs for an empty tuple struct syntax as well:

#[subenum(Fruit)]
#[derive(PartialEq)]
pub enum Food {
    #[subenum(Fruit)]
    Apple (),  // <--- empty tuple struct variant
}
// Compile Error: expected expression, found `,`

Adding a dummy struct field, or removing the struct variant or tuple struct syntax, makes the code compile again:

#[subenum(Fruit)]
#[derive(PartialEq)]
pub enum Food {
    #[subenum(Fruit)]
    Apple {
        dummy: i32,   // <--- at least one field
    },
}
// Compiles OK

Or:

#[subenum(Fruit)]
#[derive(PartialEq)]
pub enum Food {
    #[subenum(Fruit)]
    Apple (()),
}
// Compiles OK

Or:

#[subenum(Fruit)]
#[derive(PartialEq)]
pub enum Food {
    #[subenum(Fruit)]
    Apple,  // <--- not an empty c-like struct or tuple struct variant
}
// Compiles OK

Changing the order of the macros also resolves the compiler error:

#[derive(PartialEq)]  // <--- swapped
#[subenum(Fruit)]     // <--- swapped
pub enum Food {
    #[subenum(Fruit)]
    Apple {},
}
// Compiles OK

But this seems to cause other problems, like the PartialEq not actually being implemented for the subenums.

I don't think it's too uncommon to use empty struct or tuple variants, and it's certainly part of the language to do so.

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

1 participant