Skip to content

Type inference on nested GADTs #2104

Closed
Closed
@OlivierBlanvillain

Description

@OlivierBlanvillain

Type inference fails to infer i: Int in the following pattern matching:

case class Pair[A, B](_1: A, _2: B)

trait Cons[+H, +T]

object Cons {
  def apply[H, T](h: H, t: T): Cons[H, T] = ???
  def unapply[H, T](t: Cons[H, T]): Option[Pair[H, T]] = ???
}

object Test {
  def main(args: Array[String]): Unit = {
    Cons(Option(1), None) match {
      case Cons(Some(i), None) =>
        i: Int // error: found: Any(i), requires: Int
        assert(i == 1)
    }
  }
}

-Xprint:frontend shows a union type in the unapply:

case Cons.unapply[Option[Int] | Some[Any], None$](
  Some.unapply[Any](i @ _): Some[Any], None
)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions