scala / bug

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

Warning "match may not be exhaustive" with Queue() and EmptyQueue on pattern matching with a Queue #12697

Closed azarzadavila closed 1 year ago

azarzadavila commented 1 year ago

Reproduction steps

Scala version: 2.13.10

import scala.collection.immutable.Queue

object Main {
  def main(args: Array[String]) = {
    val x: Queue[Int] = Queue()
    x match {
      case _ :+ _ => println("something")
      case Queue() => println("empty")
    }
  }
}

Problem

At compilation with sbt, the following warning appears:

match may not be exhaustive.
[warn] It would fail on the following inputs: EmptyQueue, Queue()
[warn]     x match {
[warn]     ^

I would expect no warning as the matching should be complete having tested both the non-empty and empty Queue. I tested the execution with both val x: Queue[Int] = Queue() and val: Queue[Int] = Queue.empty[Int] and both work fine.

azarzadavila commented 1 year ago

This is probably linked with #12252

som-snytt commented 1 year ago

collection.:+ has unapply that returns Option[(C, A)] for (init, last).

It is refutable. The question is whether to use "special knowledge" about this extractor when emitting warnings because it is so visible.

I wonder if there is a FAQ about Seq-extraction.

som-snytt commented 1 year ago

Maybe they need irrefutable :+_! ...