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

Adds "unstable-exhaustive-types" as new feature. #328

Open
wants to merge 1 commit into
base: current
Choose a base branch
from

Conversation

TheCataliasTNT2k
Copy link

This feature deactivates all "#[non_exhaustive]" added into the code. This can be used by developers to ensure, that they match all enum variants or struct fields during pattern matching.

This does not break any existing code.

Copy link
Member

@jamesbt365 jamesbt365 left a comment

Choose a reason for hiding this comment

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

Your little find and replace has changed the doc comments.

@TheCataliasTNT2k
Copy link
Author

Now it should be fixed.

This feature deactivates all "#[non_exhaustive]" added into the code. This can be used by developers to ensure, that they match all enum variants or struct fields during pattern matching.
@kangalio
Copy link
Collaborator

This can be used by developers to ensure, that they match all enum variants or struct fields during pattern matching.

You can already add a match case for __NonExhaustive to do exactly this, no? That approach also has the advantage of marking the precise spot in your codebase that may break on semver-equivalent version updates

@TheCataliasTNT2k
Copy link
Author

You can already add a match case for __NonExhaustive to do exactly this, no?

No.
This variant can not be matched, so you are forced to use a wild card match arm.
That will compile, but I want my code to not compile, if new variants are added.

Using my solution, if I wanted my code to compile nonetheless, I can still add a wild card arm, and everything is fine.

But as long as enums or structs are marked as non_exhaustive or have a variant like you mentioned, I have to have a wild card match arm (which matches any added variants as well), which I do not want.

@kangalio
Copy link
Collaborator

kangalio commented Nov 24, 2024

This is what I meant

image

It does work for those __non_exhaustive struct fields and __NonExhaustive enum variants. It does not work for the #[non_exhaustive] attribute. I avoid that attribute in my personal projects for that reason. I wonder why poise uses the attribute in some places, and the manual struct field / enum variant in other places... If the manual way was used everywhere, the problem addressed by this PR addresses would be solved too...

But I just wanted to throw that in. Y'all can decide if banishing #[non_exhaustive] in favor of the configurable manual alternative, or making #[non_exhaustive]'s feature-togglable like currently implemented in the PR, is the better solution

@TheCataliasTNT2k
Copy link
Author

I do not know, why serenity and poise use the macro.
I asked some time ago, and got the answer, that it is done to not have breaking changes when not needed. I did not add them.

But I want a way to let my code fail, when new variants / struct fields are added, and this is actually intended by rust.

So I created this solution to get this behaviour.

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.

3 participants