-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
#[non_exhaustive]
stucts can be matched exhaustively using constants as patterns
#119264
Comments
I'm not sure whether I'd call this a bug or just a weird set of mechanics combining together in a way that is very counter-intuitive. The purpose of But if you construct values for them, there's no problem. And if, in fact, you construct all possible values for them, there's nothing stopping them from exhaustively matching with them. In fact, based upon the original However, I can definitely see the argument that |
The RFC states that they shouldn't be exhaustively matchable. From RFC 2008 Summary:
This is clearly violated here as adding fields to |
Right, I see where the ambiguity is, here. The point is that you can't exhaustively match the fields of a struct labelled with
I certainly don't think that the RFC is clear enough we should break the status quo since, for better or worse, this is the behaviour the compiler has chosen since consts in patterns were allowed. It would make the most sense to change things on an edition boundary, and I'm very open to it; it's just that I don't think that we should necessarily retroactively change what current editions are doing on this particular technicality. |
I think that quote is taken out of context,
Directly after that it states the following for users/downstream crates
The examples here may not be helpfull in the case that is problematic here as the (implied) So this problem almost exclusively effects I noticed it also states:
But as we can see in this issue, this contradicts the already cited statement from the summary.
Also while my given example has private fields, Comming back to the next part of your comment:
While disagreeing with the RFC author on this seams a bit silly, I don't find the RFC to be ambiguouse. The consitent way to resolve the contradiction caused by the interaction with constants as patterns would be to treat private fields the same as public fields, to presereve the goal of I agree that breaking existing code is bad and this might need an editon to fix. To be clear I also wouldn't forbid constant as patterns for |
Right, like I said, I think that we can agree that changing this going forward is good. The main reason why I suggest making it an edition change is because it wasn't really specified in the RFC, is technically breaking, and it doesn't feel like it's compelling to break now. But I guess we could just run crater to see if it breaks anything. Will leave that decision up to the folks implementing. I can draft up an RFC for this if people think that it would be good to have, or lang-team can decide if this would be reasonable to just implement without an RFC. |
Huh this is tricky: exhaustiveness today cannot distinguish the |
In rust-lang/rfcs#3535 (comment) it was noticed that it is possible to match a
#[non_exhaustive]
struct exhaustively outside the originating crate,if constants for all posible states are accessible or definable in the other crate.
As a result adding fields such that the struct has new states is a breaking change, as the existing constants won't be able to cover all states anymore.
Which makes
#[non_exhaustive]
ineffective.For Example:
Changing the bar crate to the following is then a breaking change:
This breaks the foo crate, eventhough Bar was marked
#[non_exhaustive]
to allow expanding.Reproduction Repo
Meta
The text was updated successfully, but these errors were encountered: