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

Support non-literal discriminant values #96

Merged
merged 2 commits into from
Jan 15, 2023
Merged

Support non-literal discriminant values #96

merged 2 commits into from
Jan 15, 2023

Conversation

illicitonion
Copy link
Owner

@illicitonion illicitonion commented Jan 14, 2023

While preserving favouring literal values where possible.

Fixes #94

The story here is:
#89 tried to simplify the generated code (which, among other things, made rustc not stack overflow when codegening for large enums).
Unfortunately, it assumed that all explicit discriminants were literals (and non-negative ones, as -123 parses as a UnaryExpr not a Literal).
And further unfortunately, because of a typo in a #[cfg(feature)] attached to the only tests we had for non-literal enums, we weren't running those tests (this PR re-enables them and adds some which aren't feature gated, and #95 will ensure we don't regress in this way again).

This PR attempts to preserve the "prefer just using literals rather than large chains of wrapping adds" property of #89, while also supporting non-literal cases.

While preserving favouring literal values where possible.
Copy link
Collaborator

@danielhenrymantilla danielhenrymantilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

num_enum/tests/try_from_primitive.rs Show resolved Hide resolved
num_enum_derive/src/lib.rs Outdated Show resolved Hide resolved
num_enum_derive/src/lib.rs Outdated Show resolved Hide resolved
@illicitonion
Copy link
Owner Author

@danielhenrymantilla Thanks for the feedback! I think I've addressed it all - PTAL!

Copy link
Collaborator

@danielhenrymantilla danielhenrymantilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@illicitonion illicitonion merged commit 6604e2b into main Jan 15, 2023
@illicitonion illicitonion deleted the non-literals branch January 15, 2023 00:01
@kevinaboos
Copy link

Thanks for the fix @illicitonion!

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

Successfully merging this pull request may close these issues.

Cannot use constants as enum values
3 participants