-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Pattern matching on a local class is unsound, should emit an unchecked warning #4812
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
Alternatively, we could allow the match, but weaken the type of prev.elem (or really, the captured T) to an unknown skolem. Usually that’s expressed by matching against Test#A instead of A, but there isn’t a way to write the correct prefix. Your solution is probably reasonable as well tho, and I don’t expect many to care about this use case. |
@dwijnand Do you want to take a look at this one? |
Yep, I'll see what I can do. |
Getting that to fail is easy: @@ -152,6 +152,8 @@ object TypeTestsCasts {
case AnnotatedType(t, _) => recur(X, t)
case tp2: RefinedType => recur(X, tp2.parent) && TypeComparer.hasMatchingMember(tp2.refinedName, X, tp2)
case tp2: RecType => recur(X, tp2.parent)
+ case tp2
+ if tp2.typeSymbol.isLocal => false
case _ => true
}) but then that makes things like this fail: def foo: Unit =
object O:
sealed abstract class A
class B extends O.A
class C extends O.A
val x: O.A = ???
x match
case x: B => ???
case x: C => ??? Is there a way to tell if a class (and parents I guess too) is defined in terms of local scope (which can also nest) type parameters (which I guess could also be class type parameters...)? Seems like a whole lot of things, but maybe there's mechanics for this already somehow? |
(It would be nice if the error message gave some clue as to the specific nature of the problem. I wouldn't have guessed this — neither @WojciechMazur nor I guessed it at today at #16259.) |
It's better now with #16086 |
this produces warnings for perfectly safe code, eg:
|
How is the compiler to know that that |
@dwijnand i guess because |
Even without a type parameter it's not possible to tell if
Captured as |
@dwijnand hm that sounds unfortunate, thanks for the explanation |
Adapted from http://wouter.coekaerts.be/2018/java-type-system-broken which demonstrates the same thing in Java:
There's no way to distinguish instances of a local class coming from different method calls, so the type test is never safe and should always require an
@unchecked
annotation.The text was updated successfully, but these errors were encountered: