No warning on uncheckable isInstanceOf call
See original GitHub issueMinimized code
import scala.Option
object Test extends App {
trait A[+X]
class B[+X](val x: X) extends A[X]
object B {
def unapply[X](b: B[X]): Option[X] = Some(b.x)
}
class C[+X](x: Any) extends B[Any](x) with A[X]
def f(a: A[Int]): Int =
if a.isInstanceOf[B[Int]] then
a.asInstanceOf[B[Int]].x
else
0
f(new C[Int]("foo"))
}
Output
Compiles without warning, explodes at runtime.
Expectation
A warning about the .isInstanceOf
test should be emitted.
Adapted from https://github.com/lampepfl/dotty/blob/master/tests/neg/i3989c.scala#L1.
/cc @liufengyun it seems that additional tweaking of how GADTs are used for checking .isInstanceOf
is necessary.
Issue Analytics
- State:
- Created 3 years ago
- Comments:25 (25 by maintainers)
Top Results From Across the Web
How do I address unchecked cast warnings? - Stack Overflow
The obvious answer, of course, is not to do the unchecked cast. If it's absolutely necessary, then at least try to limit the...
Read more >Service "s3" not yet available, retrying... · Issue #808 - GitHub
Hello there After installing localstack and trying to start several services on my machine, s3 always failed to start The command I am...
Read more >Java static code analysis: Operator "instanceof" should be ...
The method isInstance() from java.lang.Class works differently and does type check at runtime only, incompatible types will therefore not be detected early in ......
Read more >465085 – [null] False positive potential null warning/error ...
The first example with 'instanceof' is the expected result: No error/warning is shown because 'mayBeNull' cannot be null. In the second example, ...
Read more >List of Java inspections | IntelliJ IDEA Documentation - JetBrains
Disabled. Warning icon Warning. Chain of instanceof checks. Disabled ... public method not exposed in interface. Disabled ... equals() called on Enum value....
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 Free
Top 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
@abgruszecki discovered that Kotlin also requires “invariant refinements”, in the following gist covariant type parameter
T
(“out” means covariant) is refined from Any to Int, and that is a compilation error:https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS42LjIxIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiLyoqXG4gKiBZb3UgY2FuIGVkaXQsIHJ1biwgYW5kIHNoYXJlIHRoaXMgY29kZS5cbiAqIHBsYXkua290bGlubGFuZy5vcmdcbiAqL1xuaW50ZXJmYWNlIEJhc2UgPG91dCBUPjtcbmludGVyZmFjZSBCYXNlQW55IDogQmFzZTxBbnk+O1xuaW50ZXJmYWNlIEJhc2VJbnQgOiBCYXNlPEludD47XG5vcGVuIGNsYXNzIERlcml2ZWQxKCkgOiBCYXNlIDxBbnk+O1xuY2xhc3MgRGVyaXZlZDIoKSA6IERlcml2ZWQxKCkgLCBCYXNlSW50OyJ9
I think we are all still scratching our heads way too much to be able to give advice on specifics like that. 🤯