Skip to content

Unreachable code error when matching on Scala 3 sealed trait #13106

Closed as not planned
@jadenPete

Description

@jadenPete

Reproduction steps

Scala 2 version: 2.13.16
Scala 3 version: 3.6.3

  1. Compile the following code with Scala 3:
sealed trait Pet

case class Dog(name: String) extends Pet
case class Cat(name: String) extends Pet
  1. Compile the following code with Scala 2:
class PetPrinter {
  def printPet(pet: Pet): Unit = println(
    pet match {
      case Dog(_) => "I'm a dog."
      case Cat(_) => "I'm a cat."
    },
  )
}
  1. Observe the following compiler error:
[Error] .../Scala2.scala:5:22: unreachable code
[Error] Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=other-match-analysis, site=PetPrinter.printPet
[Error]       case Cat(_) => "I'm a cat."
[Error] 

Problem

The Scala 2 code should've compiled successfully.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions