Closed
Description
Adapted from http://wouter.coekaerts.be/2018/java-type-system-broken which demonstrates the same thing in Java:
object Test {
var prev: Any = _
def test[T](x: T): T = {
class A(val elem: T)
if (prev == null) {
val a = new A(x)
prev = a
x
} else {
prev match {
case prev: A => // This should warn, but doesn't
prev.elem
case _ =>
x
}
}
}
def main(args: Array[String]): Unit = {
test(1)
val x: String = test("") // ClassCastException: java.lang.Integer cannot be cast to java.lang.String
}
}
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.