gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Disallow discriminant variants in enums with tuples or structs

Open CohenArthur opened this issue 3 years ago • 3 comments

I tried this code:

enum WithVariants {
    Discri = 1,
    Simple,
    Tuple(i32),
    Structure { a: i32, b: i32 },
}

I expected to see this happen: rustc errors out with the following message

error[E0658]: custom discriminant values are not allowed in enums with tuple or struct variants
 --> test.rs:2:14
  |
2 |     Discri = 1,
  |              ^ disallowed custom discriminant
3 |     Simple,
4 |     Tuple(i32),
  |     ---------- tuple variant defined here
5 |     Structure { a: i32, b: i32 },
  |     ---------------------------- struct variant defined here
  |

Instead, this happened: The code compiled fine

CohenArthur avatar Apr 14 '22 08:04 CohenArthur

I never realised this was an error case, I tried to put code in to make this work but kept having the conclusion that it wouldn't work. Great find.

philberty avatar Apr 15 '22 21:04 philberty

Isn't there an unstable feature that allows this?

bjorn3 avatar Apr 15 '22 22:04 bjorn3

Isn't there an unstable feature that allows this?

Oh fair point I didn't think of checking. I found arbitrary_enum_discriminant which implements it on top of adding discriminants to tuple and struct variants.

I think this is something that we can think about after implementing the stable behavior.

CohenArthur avatar Apr 16 '22 09:04 CohenArthur