scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.72k stars 1.04k forks source link

Incorrect "the type test for A cannot be checked at runtime because it's a local class" warning #20741

Open uosis opened 1 week ago

uosis commented 1 week ago

Compiler version

Tested 3.4.2, 3.4.1, 3.4.0, 3.3.3, all with the same issue.

2.13.14 works fine and does not emit warning.

Minimized example

def f: Unit = {
  class A {
    override def equals(x: Any): Boolean = x match {
      case a: A => true
      case _ => false
    }
  }
}

Output Error/Warning message

1 warning found
def f: Unit
-- [E092] Pattern Match Unchecked Warning: -------------------------------------
4 |      case a: A => true
  |           ^
  |the type test for A cannot be checked at runtime because it's a local class
  |
  | longer explanation available when compiling with `-explain`

Why this Error/Warning was not helpful

The code seems correct and runs correctly, i.e. the type test executes correctly at runtime, contradictory to the warning.

Suggested improvement

No warning should be emitted.

som-snytt commented 1 week ago

The following with member A prints false twice

class B:
  class A {
    override def equals(x: Any): Boolean = x match {
      case a: A => true
      case _ => false
    }
  }
  def f(x: Any): Any = {
    val y = A()
    println(y == x)
    y
  }

@main def test() =
  val b0 = B()
  val f = b0.f(null)
  val b1 = B()
  b1.f(f)

but with local A, it is false (of course) then true. It's merely testing the implementation class.

Not sure if more words from the code comment would be elucidating:

8. if `P` is a local class which is not statically reachable from the scope where `X` is defined, "it's a local class"
clavigne commented 5 days ago

Can I try my hand at this one? Sounds interesting