Skip to content

Exhaustiveness checker needs special rules for singleton types #4227

@LPTK

Description

@LPTK

The following code (here on Scastie) generates an exhaustivity warning that seems unjustified:

sealed abstract class Maybe[A]
final case class Just[A](a: A) extends Maybe[A]
class Empty[A] extends Maybe[A]
object Empty {
  def apply[A](): Maybe[A] = new Empty[A]
  def unapply[A](e: Empty[A]): Some[Unit] = Some(())
}

object Test {
  val a: Maybe[Int] = Just(2)
  def main(args: Array[String]): Unit = a match {
    case Just(_) => 
    // case Empty(_) => // ok
    case Empty(()) => // match may not be exhaustive. It would fail on: Empty(_)
  }
}

Also (regardless of the fact that the warning is spurious), the error message says it would "fail on Empty(_)," but this is not even a valid expression (as in, I can't write val x = Empty(y)). If warnings are supposed to refer to patterns as opposed to expressions, maybe the message should read something like "pattern case Empty(_) not handled" instead.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions