Skip to content

Incorrect MatchCaseUnreachable warning on Coproduct #3443

Closed
@OlivierBlanvillain

Description

@OlivierBlanvillain

Here is an example where Dotty incorrectly reports an unreachable case in pattern marching:

object Test {
  // shapeless.Coproduct
  sealed trait Coproduct extends Product with Serializable
  sealed trait :+:[+H, +T <: Coproduct] extends Coproduct
  final case class Inl[+H, +T <: Coproduct](head : H) extends :+:[H, T]
  final case class Inr[+H, +T <: Coproduct](tail : T) extends :+:[H, T]
  sealed trait CNil extends Coproduct

  // Note that this only appears when T is a type parameter. Replaying T with
  // a concrete type (CNil or another :+:) leads to accurate warnnings
  def f[T <: Coproduct](fa: Int :+: T) =
    fa match {
      case Inl(x) => 1
      case Inr(x) => 2 // Dotty thinks this unreachable, but it is!
    }

  def main(args: Array[String]): Unit =
    assert(f(Inr(Inl(-1))) == 2) // Example of reachability
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions