Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unreachable case false-positive for type-parametered boolean extractors #21829

Open
ivan-klass opened this issue Oct 22, 2024 · 0 comments
Open

Comments

@ivan-klass
Copy link

ivan-klass commented Oct 22, 2024

Compiler version

3.5.1

Minimized code

import scala.reflect.ClassTag

opaque type EntryName = String

object EntryName:
  def apply[T](using cls: ClassTag[T]): EntryName =
    cls.runtimeClass.getSimpleName.replaceAll("([a-z])([A-Z]+)", "$1 $2")

  def unapply[T](s: EntryName)(using ct: ClassTag[T]): Boolean = s == apply[T]


sealed abstract class Shape

case class Square(side: Int) extends Shape
case class Circle(radius: Int) extends Shape
case class Rectangle(xSide: Int, ySide: Int) extends Shape

// make groups by EntryName while ordering
given Ordering[(EntryName, Int)] = Ordering.by { case (n, size) => n match 
  case EntryName[Circle]() => (0, size)
  case EntryName[Square]() => (1, size) // reachable!
  case EntryName[Rectangle]() => (2, size)  // reachable!
}

println(
  Vector(
  EntryName[Circle] -> 4,
  EntryName[Rectangle] -> 8,
  EntryName[Square] -> 12,
  EntryName[Circle] -> 1
).sorted == Vector(
    EntryName[Circle] -> 1,
    EntryName[Circle] -> 4,
    EntryName[Square] -> 12,
    EntryName[Rectangle] -> 8,
  )
)

Output

[E030]  Match case Unreachable Warning:
  case EntryName[Square]() => (1, size)

[E030]  Match case Unreachable Warning:
  case EntryName[Rectangle]() => (2, size)

Expectation

No warnings issued

Note: also reproducible without opaque type

@ivan-klass ivan-klass added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 22, 2024
@dwijnand dwijnand added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 22, 2024
@dwijnand dwijnand self-assigned this Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants