scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

wrong "match may not be exhaustive" with abstract type in prefix #12704

Closed lrytz closed 1 year ago

lrytz commented 1 year ago

Scala 2.13.10

class Out {
  sealed trait P
  case class K(x: Int) extends P
}
class C[O <: Out](val o: O) {
  import o._
  def foo(t: P) = t match {
    case _: K => 0
  }
}

leads to

Test.scala:8: warning: match may not be exhaustive.
It would fail on the following input: K(_)
  def foo(t: P) = t match {
                  ^
1 warning

The bug is in enumerateSealed, which calls approximateAbstracts, which has

case TypeRef(pre, sym, _) if sym.isAbstractType => WildcardType

This turns the prefix of C.this.o.P into wildcard, leading to ?#P, and enumerateSealed then returns Out.this.K instead of C.this.o.K.