Skip to content

Incorrect typing under match with alternatives #13090

Open
@szeiger

Description

@szeiger

Given

sealed trait A[T] { def x: T }
final case class B(x: String) extends A[String]
final case class C(x: Int) extends A[Int]

def f[T](a: A[T]): Unit = {
  def v: T = a match { case C(x) => 42 }
  a match {
    case B(_) | C(_) =>
      val v1: T = v
      val v2: T = a match { case C(x) => 42 }
      ()
  }
}

the definition of v1 typechecks correctly, but v2 fails:

$ ~/scala/scala-2.13.14/bin/scala adt.scala
adt.scala:10: error: type mismatch;
 found   : Int(42)
 required: T
      val v2: T = a match { case C(x) => 42 }
                                         ^

The failure is consistent in Scala 2.12, 2.13 and 3.6, but compiling with 3.6 gives a better hint at the problem:

$ ~/scala/scala3-3.6.3-aarch64-apple-darwin/bin/scala adt.scala
Compiling project (Scala 3.6.3, JVM (11))
[error] ./adt.scala:10:42
[error] Found:    (42 : Int)
[error] Required: T
[error]
[error] where:    T is a type in method f which is an alias of String
[error]       val v2: T = a match { case C(x) => 42 }
[error]                                          ^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions