Skip to content

"Unreachable case" is, in fact, reachable #23113

Open
@durban

Description

@durban

Compiler version

3.3.5

Minimized code

package com.example

object Repro {

  sealed abstract class Listener[A]

  final case class Empty[A]()
    extends Listener[A]

  final case class Waiting[A](next: Promise[A])
    extends Listener[A]

  def foo[A](l: Listener[A]): Unit = {
    l match {
      case Empty() => println("empty")
      case w @ Waiting(_) => println(s"waiting: ${w.next}") // <-- HERE
    }
  }

  def main(args: Array[String]): Unit = {
    foo(Waiting(Promise()))
  }
}

sealed trait Promise[A]

object Promise {

  def apply[A](): Promise[A] = new PromiseImpl[A](null)

  private[this] abstract class PromiseBase[A] extends Promise[A]

  private[this] final class PromiseImpl[A](val st: Waiting[A]) extends PromiseBase[A]

  private[this] final class Waiting[A]
}

Output

[warn] -- [E030] Match case Unreachable Warning: /home/.../Repro.scala:... 
[warn] 33 |      case w @ Waiting(_) => println(s"waiting: ${w.next}")
[warn]    |           ^^^^^^^^^^^^^^
[warn]    |           Unreachable case
[warn] Matching filters for @nowarn or -Wconf:
[warn]   - id=E30
[warn]   - name=MatchCaseUnreachable
[warn] one warning found
[info] running com.example.Repro 
waiting: com.example.Promise$PromiseImpl@d3995d

Expectation

I'd expect no "Unreachable case" warning for a case which is reachable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:pattern-matchingarea:reportingError reporting including formatting, implicit suggestions, etcbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions