google / google/zerocopy

FromZeros doesn't support non-literal enum discriminants

Open
#362 1 comment 2 reactions 0 assignees View on GitHub
compatibility-nonbreaking
Dominant language
Rust
Stars
2.6k
Forks
179
Avg merge
1d 19h
Merged PRs (30d)
29

Description

The following is valid Rust syntax, and this type satisfies the requirements to be `FromZeros`:

```rust
#[repr(u8)]
enum Foo {
Bar = 0 + 0,
}
```

However, our `FromZeros` custom derive checks for the literal `0`, so doesn't support this. It's very unlikely to be a problem because it's almost certainly a rare if nonexistent pattern in the ecosystem.

It shouldn't be too hard to support this. In fact, it might even simplify our existing implementation. All we need to do is emit a const bound like `Enum::A as u8 == 0 || Enum::B as u8 == 0 || Enum::C as u8 == 0`. This would automatically support expressions without having to reason about them, and it would also allow us to get rid of our logic which needs to special-case both a leading variant with no explicit discriminant and a variant with an explicit `0` discriminant.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.