Skip to content

Spurious Match Exhaustivity Warning #5422

Closed
@adamgfraser

Description

@adamgfraser

I'm getting what appears to be a spurious pattern matching exhaustivity warning. I'm trying to define a function Either[X, A] => Int using pattern matching. My first match clause is for Left(Y), where Y is an object that is a subtype of X. My second match clause is a wildcard that should catch anything else. But I'm getting a warning that the match would fail for Left(_). Note that this compiles with Scala 2.12.7. Relatively minimized case below.

import scala.language.higherKinds

object Test {

  trait X
  case object Y extends X

  trait Foo[F[_], O] {
    def bar: Foo[F, O] = this match {
      case Qux(fa, f) => qux(fa) {
        case Left(Y) => ???
        case x => ???
      }
    }
  }

  case class Qux[F[_], A, O](fa: F[A], f: Either[X, A] => Int) extends Foo[F, O]

  def qux[F[_], A, O](fa: F[A])(f: Either[X, A] => Int): Foo[F, O] =
    Qux(fa, f)
}
[error] 11 |        case Left(Y) => ???
[error]    |        ^
[error]    |        match may not be exhaustive.
[error]    |
[error]    |        It would fail on pattern case: Left(_)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions