question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No warning on uncheckable isInstanceOf call

See original GitHub issue

Minimized 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:open
  • Created 3 years ago
  • Comments:25 (25 by maintainers)

github_iconTop GitHub Comments

1reaction
oderskycommented, Mar 19, 2022

Perhaps it’s just not doable with baseType and I need to look to do something like paramInfoAsSeenFrom? Any tips or advice welcome.

I think we are all still scratching our heads way too much to be able to give advice on specifics like that. 🤯

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found