-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Prevent matching into or out of an opaque type #7314
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
Comments
I agree they should give unchecked warnings. |
See also #5467, which may be counted as a duplicate. |
In fact, matching against an
but you cannot flag
So, what's the point? You can check against |
Good point. The abstraction is really leaky.
I think it's still useful to flag both of the specific examples I showed above. The ability to silence warnings by upcasting to Also, coding guidelines like scalazzi discourage using |
So, this is much harder than it looks at first. If one of the two types in the comparison is an opaque type (or has one in a union or intersection), and the other is a different type, we have to figure out whether the match is already implied by the types, or whether it needs runtime information. This looks like a fairly hard problem, similar to the problem of deciding whether a cast can fail. I think the problem should be solved, by not by myself. I have too much else on my plate. |
It sounds very similar to what we need to do when inferring GADT constraints - I'll take a look at it when I have the time. |
Note that the second half of this ticket ("conversion out of the opaque type") is fixed under Converting into an opaque type is still an issue. Let's call this one fixed, and use #5467 for the part about emitting unchecked warnings when one uses an opaque type in a type pattern. |
One of the stated goals of opaque types is to statically enforce invariants about some values without wrapping them. To achieve this, we must control what values are allowed to get converted into and out of the opaque type.
Matching on opaque types is currently allowed, and break this. For instance, it is possible to convert an immutable array into a mutable one by matching on it, which defeats its purpose.
minimized code
https://scastie.scala-lang.org/hV46dfNWR6iTP2uLIMfS2g
expectation
Both matches should be rejected, or at the very least give warnings.
The text was updated successfully, but these errors were encountered: