Skip to content

Match type reduction does not fail even if selector matches none of the cases #23822

@NovaMage

Description

@NovaMage

Compiler version

3.6.2, 3.7.1

Minimized code

object MatchTypes {

  type ConstituentPartOf[A] = A match {
    case BigInt => Int
    case String => Char
  }

  def lastPartOf[A](thing: A): ConstituentPartOf[A] = thing match {
    case number: BigInt => (number % 10).toInt
    case string: String => string.charAt(string.length - 1)
  }

  def main(args: Array[String]): Unit = {
    val lastPartOfSomethingElse = lastPartOf(BigDecimal("10"))
    println(lastPartOfSomethingElse)
  }
}

Output

Program compiles succesfully (which it shouldn't), and throws an exception at runtime:

Exception in thread "main" scala.MatchError: 10 (of class scala.math.BigDecimal)
	at MatchTypes$.lastPartOf(MatchTypes.scala:10)
	at MatchTypes$.main(MatchTypes.scala:14)
	at MatchTypes.main(MatchTypes.scala)

Expectation

It should throw a compilation error as it does in scala 3.3.6

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions