scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Spurious non-exhaustive pattern match warning on type alias unapply #12693

Closed Daenyth closed 2 years ago

Daenyth commented 2 years ago

Reproduction steps

Scala version: 2.13.10

case class Foo[A, B](a: A, b: B)

type Bar[A] = Foo[Int, A]
object Bar {
  def apply[B](a: Int, b: B) = Foo(a, b)
  def unapply[B](bar: Bar[B]): Option[(Int, B)] = Foo.unapply(bar)
}

def blah(x: Bar[Int]): (Int, Int) =
 x match {
   case Bar(a, b) => (a, b)
 }

Scastie

Problem

The x match line emits a "non-exhaustive match" warning.

The code previously worked in 2.12

Workaround

Define unapply to return Some((bar.a, bar.b)) explicitly

som-snytt commented 2 years ago

Warns since 2.13.4. https://github.com/scala/scala/releases/tag/v2.13.4

Alias is not pertinent?

som-snytt commented 2 years ago

Behavior is as expected and documented.

for custom extractors: demarking irrefutable extractors as such, by defining the return type as Some

(where "demarking" is a Britishism for "marking"?) (I guess a return type is a kind of demarcation.)

Daenyth commented 2 years ago

The part of this that confuses me is that Foo.unapply returns Option, not Some, so you can't invoke it and have the return type of Bar.unapply be Some

som-snytt commented 2 years ago

Scala 3 is simpler for this case. I don't know why Scala 2 doesn't emit Some for case unapply.