Skip to content

False Unreachable case except for null warning in inline def with a match expression with type params #25239

@arainko

Description

@arainko

Compiler version

3.3.7, 3.8.1

Minimized code

//> using scala 3.8.1

case object Err
type Err = Err.type

enum Plan[+E <: Err] {
  case A()
  case B()
  case C()

  final inline def narrow[Sub <: Plan[Err]](inline fn: Sub => String): Option[String] =
    this match {
      case a: Sub => Some(fn(a))
      case other => None
    }
}

@main def main = {
  val plan = Plan.A()

  println(plan.narrow[Plan.A[Err]](_.toString)) // Some(A())
  println(plan.narrow[Plan.B[Err]](_.toString)) // None, therefore the 'other' case is actually reachable!
}

Output

$ scala run .
Compiling project (Scala 3.8.1, JVM (17))
[warn] ./Plan.scala:14:12
[warn] Unreachable case except for null (if this is intentional, consider writing case null => instead).
[warn]       case other => None
[warn]            ^^^^^
Compiled project (Scala 3.8.1, JVM (17))
Some(A())
None

Expectation

No warning

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