Pattern matching on a local class is unsound, should emit an unchecked warning
See original GitHub issueAdapted 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.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Unsound implementation of singleton type matching #9359
#8430 - I was hoping that we could just emit a warning along the lines of "singleton type patterns are unsound with the...
Read more >Warning about an unchecked type argument in this Scala ...
Scala version 2.9.0.1. I don't see how an erased type parameter is needed to perform the match. That first case clause is meant...
Read more >Recommended Scalac Flags for 2.12 - tpolecat
scalacOptions ++= Seq( "-deprecation", // Emit warning and location for ... "-Xlint:unsound-match", // Pattern match may not be typesafe.
Read more >Chapter 8. Classes - Oracle Help Center
Class declarations define new reference types and describe how they are implemented (§8.1). A top level class is a class that is not...
Read more >A tour of the Dart language
This page shows you how to use each major Dart feature, from variables and operators to classes and libraries, with the assumption that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s better now with https://github.com/lampepfl/dotty/pull/16086
(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.)